Jump to content

Recommended Posts

hi, so I'm a little lost.

in my security script when someone is NOT logged in, I use: header('Location: index.php');

 

thing is, I would liek to keep track of what page they were ON so once they log in it takes them BACK.

 

ie, user goes to example.com/photos.php gets redirected to login, then after that gets taken BACK to photos.

 

I tried detecting the previous page with both getenv ("HTTP_REFERER"); and  $_SERVER['SCRIPT_URI'];

 

which as I discovered don't work with a header.

so is there any other way to detect the previous page, or send it via a post or variable when using header()?

 

thanks

Link to comment
https://forums.phpfreaks.com/topic/50386-header-can-you-send-a-value-with-it/
Share on other sites

using ("HTTP_REFERER") will work but not in the index.php as the header is like the user manually entered the address, so i would say store ("HTTP_REFERER") in a session (from the files they tried to access and not the index.php)

 

hope that helps

The problem with HTTP_REFERER is that is specified by the client (read browser).  The client can really send in whatever it wants as the referer, or omit it all together.  I would suggest taking an approach more like this:

 

<?php
  session_start();
  // push the currently requested page into the session array so we can get at it later.
  $_SESSION['post_login_redirect'] = $_SERVER['PHP_SELF'];
  if(!$_SESSION['logged_in']){
    // redirect to login page
    header('location: login.php');
  }
?>

 

Then in your login script...

 

<?php
// ...
if($successfulLogin){
  if(isset($_SESSION['post_login_redirect']))
    header('location: ' . $_SESSION['post_login_Redirect');
}
?>

 

Best,

 

Patrick

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.