How would you go about creating a PHP script for the following scenario? The reason being I wish to do this so it eliminates a navigation step for the users of my website.
Let’s say you go to a page of the website where authentication is required (you need to login). You came in externally, from a link on another web site, or a person gave you the link. My site will redirect you automatically to an error page explaining that you don’t have access to the page. So you enter the login details, and now your back at the home page. Well, if your not familiar with the layout of my website, this could be a problem, since, well, you don’t know where to go to find the information your looking for!
Logically, I guess the program layout would be to save the requested url into a PHP variable, and store it across pages using either a Session, Cookie, or transfer it through the url. Since these pages are login protected, it would make more sense to use a Cookie or a Session. Then, when the user logs in and submits the form, the variable would be used in the header() function so as to redirect the user back to their intended destination.
My real question would be: Does this code make sense to you?
On the Authorization Script.
[php]
/* LOL I am showing my steps as a student… ROFL! */
//If An Existing Session is NOT already present
if(!isset($_SESSION)) {
//Start a NEW Session
session_start();
}
else
{
//Do Nothing
}
//If the user is not logged in, save the requested URI for later redirection
//Obtain the host address or name requested
$host = $_SERVER[‘HTTP_HOST’];
//Obtain the URI requested
$uri = $_SERVER[‘REQUEST_URI’];
//Assemble the link together
$redirect_back = “http://$host$uri”;
//Store in the SESSION array
$_SESSION[‘orig_req’] = $redirect_back;
[/php]
On the login page:
[php]
/* Blah Blah Blah Login Code
{
//Login Code DUN DUN DUN
}
*/
//Redirect the user to their original page
header(“Location:” . $_SESSION[‘orig_req’] . “”);
[/php]