Solved

Capturing Referring Page when redirected

Posted on 2012-03-11
12
279 Views
Last Modified: 2012-03-29
Hello,

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:

else{
       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.
0
Comment
Question by:neilsav
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
  • 3
  • +1
12 Comments
 
LVL 25

Expert Comment

by:Kyle Hamilton
ID: 37707819
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.)
0
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 37707897
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.
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_2391-PHP-login-logout-and-easy-access-control.html
0
 
LVL 1

Expert Comment

by:rakjosh
ID: 37708892
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
0
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 
LVL 1

Author Comment

by:neilsav
ID: 37709172
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?
0
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 37709475
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.
http://www.sitepoint.com/books/phpmysql4/

You can also find some foundational material on the PHP web site.
http://us3.php.net/tut.php

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:
    // REMEMBER HOW WE GOT HERE
    $_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
0
 
LVL 1

Author Comment

by:neilsav
ID: 37709880
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.
0
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 37709992
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.
http://php.net/manual/en/language.variables.superglobals.php

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

var_dump($_SESSION);

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.
0
 
LVL 1

Accepted Solution

by:
rakjosh earned 500 total points
ID: 37711137
You can write the code like:

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

Expert Comment

by:Ray Paseur
ID: 37711239
You probably want $_SERVER["REQUEST_URI"] instead of $_SERVER["PHP_SELF"].
0
 
LVL 1

Author Comment

by:neilsav
ID: 37712712
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
$user->session_begin();
$auth->acl($user->data);
$user->setup();

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.
0
 
LVL 1

Expert Comment

by:rakjosh
ID: 37713035
have you used the $user->session_begin(); before storing the $_SESSION["entry_course"] = "coursereview_specify.php?course=$courseid";
please check.
0
 
LVL 1

Author Comment

by:neilsav
ID: 37714429
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?
0

Featured Post

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Creating and Managing Databases with phpMyAdmin in cPanel.
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

691 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question