PHP - Login Include redirect to initial page

Posted on 2009-02-16
Last Modified: 2013-12-13
Dear Experts,

I've implemented a login include at the top of each of my pages.
When a user tries to open this pages it will first check whether he has been logged in, using $_SESSION's,
and if not, the user is directed to the login page.

This is working as expected up till here. But what I would like to have is when the user tries to open a page the login include must direct the user to the page he was initially trying to open after the login was successful.

Hope this makes sense..

Kind regards
Question by:Marius0188
    LVL 8

    Assisted Solution

    You could use $referer = $_SERVER['HTTP_REFERER']; to store the referring page and then redirect to it.

    This can also be done with JavaScript using the history code.
    LVL 28

    Assisted Solution

    header('Location: '.$_SERVER['HTTP_REFERER']
    LVL 8

    Assisted Solution

    In that case you need to give the redirect a variable location. You can usually find the last page visited in $_SERVER['HTTP_REFERER']. This is not complety reliabe and there are other methods, but could you post the corresponden code please ? Than I could help you further.
    LVL 5

    Accepted Solution

    Alternative method is passing the origin url in a GET variable.
    I answered a question with this method some days ago here
    LVL 5

    Expert Comment

    Improved code regarding the answer above (this is, passing the referrer URL as a GET variable)

    // Code for redirecting an non logged in user to login page
    $loginurl = 'login.php';
    header('Location: '. $loginurl . "?from=" . urlencode($_SERVER['PHP_SELF']));
    // Code for redirecting a logged in user to referrer page
    header('Location: ' . urldecode($_GET['from']));

    Open in new window


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Highfive + Dolby Voice = No More Audio Complaints!

    Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

    As this topic comes over and over again in different forms, I've finally decided to write a short (yea, right...) article / tutorial about pagination with PHP with MySQL database. There are dozens of these kind of tutorials, I know - I wanted to mak…
    Consider the following scenario: You are working on a website and make something great - something that lets the server work with information submitted by your users. This could be anything, from a simple guestbook to a e-Money solution. But what…
    The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
    The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

    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