PHP redirect othern than Header Location

Posted on 2011-10-17
Last Modified: 2013-11-19

in my PHP scripts I need to send people to different pages depending on the results of my script, I've been using

header('Location: /yougohere.php');

but I was wondering if there was a better / cleaner solution to sending people to different pages?

Question by:Teshket
    LVL 13

    Accepted Solution

    That solution is usually best, so long as your code is clean and any exceptions are handled, you won't run into any issues.

    The other way to redirect is with HTML meta tags, and I would NOT recommend using those.

    Unless you want to dynamically load content into your main window (different content for each user), you are using the cleanest way.

    If you have a specific concern, post a new question and I will try to address it.

    So ya, simple answer is: you found the correct key :)

    Although the code you are using to determine placement may need improvement, I can't see it from here.
    LVL 13

    Expert Comment

    I would probably use  a SQL database of user urls tied to user names, so psuedo code would be: (using PEAR::DB)
    $sql = "SELECT `url` FROM `users` WHERE `username` = '$username';";
    header('Location: ' . $result['url']);

    Open in new window

    LVL 48

    Expert Comment

    Instead od redirecting you could use the FrontController-Pattern so there is no need for a redirect
    LVL 3

    Expert Comment

    In addition to previous answers/solutions you can use following ways (i will note previous posts,too):
    + using header (which you said in your question), and clear header before using it for clear errors or set error_responding to zero.
    + using javascript code which should echo and use die(); in next line.
    + using redirect html tag which should echo and use die(); in next line.
    + using Front Controller Pattern (as hernst42 said in post #36984127)
    + using apache mod_rewriting and in_line editing.

    if you need additional information on each ways let me know.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Better Security Awareness With Threat Intelligence

    See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

    Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
    Envision that you are chipping away at another e-business site with a team of pundit developers and designers. Everything seems, by all accounts, to be going easily.
    The viewer will learn how to count occurrences of each item in an array.
    The viewer will get a basic understanding of what section 508 compliance can entail, learn about skip navigation links, alt text, transcripts, and font size controls.

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    22 Experts available now in Live!

    Get 1:1 Help Now