Capturing Referring Page when redirected


I need to be able to capture a variable on a redirect

The code works like this:

if user is logged in send user to page review?id=1234 (this works as expected)

other wise they are sent to the login page main.php with the code below:

       header('Location: main.php');

Then on main.php when the user logs in they are returned back to the page that they were previously on based on the

$_SERVER['HTTP_REFERER']  rather than review?id=1234

For some reason because I am using the header function to redirect it doesn't count the page that it was trying to access as the referrer page it counts the page that the user was previously on.

What I need to be able to do is capture the page the user would have accessed if they were logged in so I can redirect them there after they login.  The ID in the URL references a unique product ID that provides information on the product based on that ID.
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Kyle HamiltonData ScientistCommented:
You could use javascript to capture the query (review?id=1234) as a variable (link) and POST it to your script in main.php. Then, instead of using REFERER, use the variable ($link = _POST[link])

(To give you exact code, I would need to see your scripts and page source.)
Ray PaseurCommented:
You might want to follow the design pattern shown in this article.  It remembers the client entry point in the case that the client is not logged in.  Once the login is completed, it goes back to the client's preferred URL.
You might use sessions to store the pagename of the current page then you can get the last value of the session on any page, after that you can change the session value after getting the pagename with the current one
The 7 Worst Nightmares of a Sysadmin

Fear not! To defend your business’ IT systems we’re going to shine a light on the seven most sinister terrors that haunt sysadmins. That way you can be sure there’s nothing in your stack waiting to go bump in the night.

neilsavAuthor Commented:
rakjosh -

Can you supply the code to store the page name in a session?

Will this have any effect on the session I create when a user logs in?  Can you run multiple sessions at once?
Ray PaseurCommented:
Will this have any effect on the session I create when a user logs in?  Can you run multiple sessions at once?  Based on these questions I am going to recommend a good book for you to get started in PHP.

You can also find some foundational material on the PHP web site.

In PHP the "session" is an array.  You can store lots of things in an array.  It can contain scalar variables, arrays (yes, an array of arrays) and objects.  About the only things you cannot put into the session are the resources that are unique to the instant request, such as the data base connections.

In the article I mentioned above, look for the use of the array key "entry_uri" and you will have your answer.  It is set in the first line of the access_control() function:
    $_SESSION["entry_uri"] = $_SERVER["REQUEST_URI"];

Open in new window

If you search the rest of the article for "entry_uri" you will understand what it contains and how it is used to store the page name in the $_SESSION array.

Best of luck with your PHP adventures, ~Ray
neilsavAuthor Commented:
The problem is I use phpbb as my login system so I don't have access to the session nor do I want to mess with the session that they have configured.  So I am concerned about the impact on creating a new session.
Ray PaseurCommented:
creating a new session - There is only one session.

I don't have access to the session - Of course you have access to the session!  It is one of the superglobal variables.

Here is what I would do.  Write a script that requires the login through PHPBB.  Inside that script put this statement:


Read the output from var_dump().  From that you can probably infer what keys in the $_SESSION array are used by PHPBB. Choose a key that will not collide with the PHPBB keys.  Put the session data you want in that key.
You can write the code like:

if(isset($_SESSION["pageName"]) ) {
    $varPageName = $_SESSION["pageName"];
    $_SESSION["pageName"] = $_SERVER["PHP_SELF"];
} else {
    $_SESSION["pageName"] = $_SERVER["PHP_SELF"];
    $varPageName = $_SESSION["pageName"];

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Ray PaseurCommented:
You probably want $_SERVER["REQUEST_URI"] instead of $_SERVER["PHP_SELF"].
neilsavAuthor Commented:
Thanks you have been more than helpful - one last question:

If the user has not logged in yet is there a session to add to?

This is the code for the phpbb session

define('IN_PHPBB', true);
$phpbb_root_path = './golf-forum/';
$phpEx = substr(strrchr(__FILE__, '.'), 1);
include($phpbb_root_path . 'common.' . $phpEx);
// Start session management

Then I added

$_SESSION["entry_course"] = "coursereview_specify.php?course=$courseid";

But the session does not store the variable on the next page, when I try to echo $_SESSION["entry_course"]  it returns null.
have you used the $user->session_begin(); before storing the $_SESSION["entry_course"] = "coursereview_specify.php?course=$courseid";
please check.
neilsavAuthor Commented:
Yes...I would think it would store the variable, but if the user isn't logged in I guess the session doesn't start, right?
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.