Redirecting To A Required Page

Discussion in 'PHP' started by Alley Cat, Feb 14, 2008.

  1. #1
    I have a facility to redirect people to the login page when they try to access another, this is the code;
    
    	$url = 'http://' . $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']);
    	if ((substr($url, -1) == '/') OR (substr($url, -1) == '\\') ) {
    		$url = substr ($url, 0, -1);
    	}
    	$url .= '/login.php';
    	
    	ob_end_clean();
    	header("Location: $url");
    	exit();
    
    Code (markup):
    What I would like to do after they have logged in is to detect which page they were trying to access and redirect them to that page. Can anyone help?
     
    Alley Cat, Feb 14, 2008 IP
  2. LittleJonSupportSite

    LittleJonSupportSite Peon

    Messages:
    386
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Store it in a variable then bounce them back.

    
    header('Location: '.$_SERVER['HTTP_REFERER']);
    
    Code (markup):
     
    LittleJonSupportSite, Feb 14, 2008 IP
  3. Alley Cat

    Alley Cat Peon

    Messages:
    41
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Would I need to do that on the page that I redirect in the first place, or on the login page? Something like this.
    
    $redir = header('Location: '.$_SERVER['HTTP_REFERER']);
    
    	$url = 'http://' . $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']);
    	if ((substr($url, -1) == '/') OR (substr($url, -1) == '\\') ) {
    		$url = substr ($url, 0, -1);
    	}
    	$url .= '/login.php';
    	
    	ob_end_clean();
    	header("Location: $url");
                header("Location: $redir");
    	exit();
    
    Code (markup):
     
    Alley Cat, Feb 14, 2008 IP
  4. jnestor

    jnestor Peon

    Messages:
    133
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #4
    What he means is in login.php after they've successfully logged in you'd redirect them back to the page they came from. The code you have for the page is fine. You'd add the redirect to the login script.
     
    jnestor, Feb 14, 2008 IP
  5. Alley Cat

    Alley Cat Peon

    Messages:
    41
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Please forgive my ignorance, but I have tried a few ways to incorporate this code into my login script, but with no success at all. Could you please show me how it should work?
     
    Alley Cat, Feb 14, 2008 IP
  6. zerxer

    zerxer Peon

    Messages:
    368
    Likes Received:
    18
    Best Answers:
    0
    Trophy Points:
    0
    #6
    I do not think their method will work because you're first redirecting them to the login page, then when they submit the login info, they want you to make it redirect to the referrer page. If you think about it, wouldn't the referrer page always be the login.php page because that's the page they used to submit the information?

    The only thing I can think of is setting a SESSION variable, which I believe uses some type of temporary cookie only for the current session (expires if they're inactive). This would require them to have cookies enabled but obviously they do if they're able to login.

    Anyways.. try this! Put this ABOVE your header("Location: $url");
    $_SESSION['last_page'] = basename($_SERVER['PHP_SELF']);
    PHP:
    Then, in the script that login.php sends the login information to for processing (most likely login.php itself), when you're ready to redirect them back to where they were, put this:
    if(!empty($_SESSION['last_page'])) { //They were redirected from another page
      $last_page = $_SESSION['last_page'];   //$last_page is now set to the filename they were previously trying to access!
      $_SESSION['last_page'] = null; //Remove the value
      unset($_SESSION['last_page']); //Make sure it's completely DEAD.. probably not necessary :P
      header("Location: $last_page");
    }
    PHP:
    If your files are also in a bunch of different directories and you want to make sure they go back to the proper directory, just remove the basename( ) part as that will only extract the filename and will remove the dir names.
     
    zerxer, Feb 15, 2008 IP
  7. jnestor

    jnestor Peon

    Messages:
    133
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #7
    You're right but you don't need to use a session variable. Just set it in a hidden field. In login.php add this to the form:

    <input type='hidden' name='redirectto' value='<?php echo $_SERVER['HTTP_REFERER']; ?>'>

    and then when the user successfully logs in you'd redirect to that page. The one trick is that if they don't input the correct username/password you'll want to retain the redirect info from the form rather than using the referrer.
     
    jnestor, Feb 15, 2008 IP
  8. Alley Cat

    Alley Cat Peon

    Messages:
    41
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Thanks Zerxer, that code works perfectly, let me know if you like our recordings, and I'll send a link to any album of your choice from our site. The site is www.home-groan.com, that is the html site.
     
    Alley Cat, Feb 15, 2008 IP
  9. nelse

    nelse Peon

    Messages:
    87
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #9
    You should (in most cases) add die() after this code.

    
    header('Location: '.$_SERVER['HTTP_REFERER']);
    die();
    
    Code (markup):
     
    nelse, Feb 15, 2008 IP
  10. zerxer

    zerxer Peon

    Messages:
    368
    Likes Received:
    18
    Best Answers:
    0
    Trophy Points:
    0
    #10
    Glad to hear it worked. Not sure I have any interest in the music but reputation+ is just as good. :)
     
    zerxer, Feb 15, 2008 IP
  11. Alley Cat

    Alley Cat Peon

    Messages:
    41
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #11
    Zerxer, If you send me an image logo I'll place a link to your site on my main navigation column, with a little thanks for the script help you gave me.
     
    Alley Cat, Feb 15, 2008 IP
  12. asdalol

    asdalol Well-Known Member

    Messages:
    320
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    115
    #12
    does HTTP_REFERER even work on a header redirect?
    why not add another variable in the url called &page=/account
     
    asdalol, Feb 15, 2008 IP