SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Guru
    Join Date
    Sep 2008
    Location
    Dubai
    Posts
    971
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Redirect using $_SERVER[http_referer]

    Hi,

    Some guys tell me best to use $_SERVER['HTTP_REFERER'] to redirect user after login, back to original page.

    How can I actually put that in my code ?

  2. #2
    SitePoint Wizard silver trophybronze trophy Stormrider's Avatar
    Join Date
    Sep 2006
    Location
    Nottingham, UK
    Posts
    3,133
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    You can use a 'Location' header to force the browser to redirect to the URL you give it. Don't forget to put an exit() afterwards, to stop the script execution.

    http://uk2.php.net/manual/en/function.header.php

  3. #3
    SitePoint Evangelist
    Join Date
    May 2006
    Location
    Denmark
    Posts
    407
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If what you mean is that you from page A click a link to get to the login form, page B, then you could on page B set a hidden field in the form like <input type="hidden" name="from" value="<?php echo htmlentities($_SERVER['HTTP_REFERER']) ?>"> and then redirect to $_POST['from'] after processing the form data like Stormrider explained above.

  4. #4
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Also bear in mind that referrer is not always populated, setup an alternative if no referrer is set.
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  5. #5
    SitePoint Guru
    Join Date
    Sep 2008
    Location
    Dubai
    Posts
    971
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Daniel0 View Post
    If what you mean is that you from page A click a link to get to the login form, page B, then you could on page B set a hidden field in the form like <input type="hidden" name="from" value="<?php echo htmlentities($_SERVER['HTTP_REFERER']) ?>"> and then redirect to $_POST['from'] after processing the form data like Stormrider explained above.
    I do this, and it goes straight back to homepage, which was undesirable.

    The code was supposed to redirect me to user section. because from there i get to the login form not hompage.

  6. #6
    SitePoint Evangelist
    Join Date
    May 2006
    Location
    Denmark
    Posts
    407
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Check that the values are set correctly. I.e. check that the hidden form field has an expected value and check that you can read it when processing the form.

  7. #7
    SitePoint Guru
    Join Date
    Sep 2008
    Location
    Dubai
    Posts
    971
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Daniel0 View Post
    Check that the values are set correctly. I.e. check that the hidden form field has an expected value and check that you can read it when processing the form.
    Actually, what code did proves that it works, but I doubt http_preferer thinks hompage is the previous page instead of user page.

  8. #8
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You're making assumptions about the values of variables based on navigation behavior you're observing. You simply can't do this.

    use var_dump() on variables to see thier contents.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •