473,411 Members | 1,991 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,411 software developers and data experts.

How to store referring URL in a Session variable

hi
every body.
i have problem in login.
the problem is that
i want to redirect page where i come from login
in other word i want to store url in session
please help me for it
Jun 16 '07 #1
6 20324
Atli
5,058 Expert 4TB
If you follow a link into a .php page, PHP stores the URL of the page you came from in the &_SERVER['HTTP_REFERER'] index.

So when you enter your login form you could do something like this to get a link back to the page you came from:
Expand|Select|Wrap|Line Numbers
  1. echo '<a href="'. $_SERVER['HTTP_REFERER'] . '">Go back</a>';
  2.  
You can also get the current page you are on by using the $_SERVER['PHP_SELF'] index.
This on is defferent, however, as it does not store the host URL.
That is, if the URL is: http://mypage.com/files/index.php
the PHP_SELF stores: /files/index.php
Jun 16 '07 #2
pbmods
5,821 Expert 4TB
Changed thread title to better describe the problem.
Jun 17 '07 #3
If you follow a link into a .php page, PHP stores the URL of the page you came from in the &_SERVER['HTTP_REFERER'] index.

So when you enter your login form you could do something like this to get a link back to the page you came from:
Expand|Select|Wrap|Line Numbers
  1. echo '<a href="'. $_SERVER['HTTP_REFERER'] . '">Go back</a>';
  2.  
You can also get the current page you are on by using the $_SERVER['PHP_SELF'] index.
This on is defferent, however, as it does not store the host URL.
That is, if the URL is: http://mypage.com/files/index.php
the PHP_SELF stores: /files/index.php





hi
thanks
but it redirect same page login page
but i want to redirect before login i visited page
please help me
Jun 18 '07 #4
Atli
5,058 Expert 4TB
Ok, lets see...

The REFERER index only stores the last page you were on. So you would need your code to remember the REFERER value as it was when you entered your login page.

Lets say I have a login.php where I have a form that, when submitted, sends me to a submit.php, which processes the form data. Now, if I call REFERED in the submit page, I get sent back to the login page, which is not what we want.

What I need to do, is store the REFERER value as it is when I enter the login.php page. To do this I can use the $_SESSION array.
But I must be carefull not to write the loging page in the SESSION if the REFERER value is the login.php, which could happen if there is a link to it somwhere.
So, my login.php page would start like this:
Expand|Select|Wrap|Line Numbers
  1. // Start a session
  2. session_start();
  3.  
  4. // Make sure the REFERER value is
  5. // not pointing to the login.php page.
  6. if(strstr($_SESSION['HTTP_REFERER'], "login.php") == false)
  7. {
  8.   // Save the value in the $_SESSION array
  9.   $_SESSION['beforeLogin'] = $_SERVER['HTTP_REFERER'];
  10. }
  11.  
Now, in the submit page, after I proccess the form data, I will have to redirect to the value saved in the $_SESSION array.
That might look something like this:
Expand|Select|Wrap|Line Numbers
  1. // Start session
  2. session_start();
  3.  
  4. // Make sure the session variable is set.
  5. // If it is not, the login page must not
  6. // have had any value to save so we
  7. // must redirect somewhere else.
  8. if(!isset($_SESSION['beforeLogin']))
  9. {
  10.   // Redirect to some general place
  11.   header("Location: http://mypage.com/home.php");
  12. }
  13. else
  14. {
  15.   // Lets get our variable
  16.   $redirect = $_SESSION['beforeLogin'];
  17.  
  18.   // It is always good to unset session
  19.   // variables when we stop using them.
  20.   unset($_SESSION['beforeLogin']);
  21.  
  22.   // And now we redirect
  23.   header("Location: ". $redirect);
  24. }
  25.  
I hope this clears it up a bit.
Jun 18 '07 #5
Ok, lets see...

The REFERER index only stores the last page you were on. So you would need your code to remember the REFERER value as it was when you entered your login page.

Lets say I have a login.php where I have a form that, when submitted, sends me to a submit.php, which processes the form data. Now, if I call REFERED in the submit page, I get sent back to the login page, which is not what we want.

What I need to do, is store the REFERER value as it is when I enter the login.php page. To do this I can use the $_SESSION array.
But I must be carefull not to write the loging page in the SESSION if the REFERER value is the login.php, which could happen if there is a link to it somwhere.
So, my login.php page would start like this:
Expand|Select|Wrap|Line Numbers
  1. // Start a session
  2. session_start();
  3.  
  4. // Make sure the REFERER value is
  5. // not pointing to the login.php page.
  6. if(strstr($_SESSION['HTTP_REFERER'], "login.php") == false)
  7. {
  8.   // Save the value in the $_SESSION array
  9.   $_SESSION['beforeLogin'] = $_SERVER['HTTP_REFERER'];
  10. }
  11.  
Now, in the submit page, after I proccess the form data, I will have to redirect to the value saved in the $_SESSION array.
That might look something like this:
Expand|Select|Wrap|Line Numbers
  1. // Start session
  2. session_start();
  3.  
  4. // Make sure the session variable is set.
  5. // If it is not, the login page must not
  6. // have had any value to save so we
  7. // must redirect somewhere else.
  8. if(!isset($_SESSION['beforeLogin']))
  9. {
  10.   // Redirect to some general place
  11.   header("Location: http://mypage.com/home.php");
  12. }
  13. else
  14. {
  15.   // Lets get our variable
  16.   $redirect = $_SESSION['beforeLogin'];
  17.  
  18.   // It is always good to unset session
  19.   // variables when we stop using them.
  20.   unset($_SESSION['beforeLogin']);
  21.  
  22.   // And now we redirect
  23.   header("Location: ". $redirect);
  24. }
  25.  
I hope this clears it up a bit.


hi
thanks for reply
but it redirect me again in login page after login.
one thing i tell u i use function for it
and i have process page for it
please help me
Jun 18 '07 #6
xiaawan
17
Dear I viewed your problem yesterday. And I had a similar problem . I solved my problem using following function.

function getCurrentPageUrl()
{
$pageURL = 'http';

if ($_SERVER["HTTPS"] == "on")
{
$pageURL .= "s";
}

$pageURL .= "://";

if ($_SERVER["SERVER_PORT"] != "80")
{
$pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
}
else
{
$pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
}
$_SESSION['CURRENT_PAGE_URL'] = $pageURL;

}

Call this function to every page that can be redirected after login
And check at login code. If there is any value in the session your page will be redirected to the value stored in the session. I have implemented this function and its working fine. I hope it will work for you as well

if ($_SESSION['CURRENT_PAGE_URL'])
{
header('location: '.$_SESSION['CURRENT_PAGE_URL']);
}
else
{
//redirect to default url
}
Jun 19 '07 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

15
by: | last post by:
Hi, I want to do things this way: I have a bunch of stuff that I want to keep track of while a user is connected to the site. Maybe 50 little peices of information. So I know I can make 50...
2
by: Nate Spillson | last post by:
I have an asp.net web application that uses session variables to store user information (username, security areas, configuration data). When the user logs into the system I store all of this...
3
by: WilsonSmith | last post by:
When I use a session variable in a module, it says "Name session is not declared". Is it not possible to use session variable in module? Wilson
2
by: sb | last post by:
As the session variable are not recommended, i search for an alternative to store my classe that i use and that i store in that session variable. is that the context that is the alternative? ...
5
by: Andy G | last post by:
I have a registration page that captures 75% of the users data. After they enter that info they are redirected to one of two pages depending on how they answered a question on the registation...
2
by: dave | last post by:
Hi all I'm newbie to asp.net and building simple pages using vb.net. I have got three pages default.aspx (which is login page), index.aspx and logout.aspx. In logout.aspx i have put following...
5
by: TRB_NV | last post by:
I'm losing information from my Session when I change pages or start the same page over again. I simplified the code so the example is really clear. The sample code that follows is supposed to...
17
by: Control Freq | last post by:
Hi, Not sure if this is the right NG for this, but, is there a convention for the variable names of a Session variable? I am using .NET 2.0 in C#. I am new to all this .NET stuff, So, any...
8
by: YYZ | last post by:
I'm using asp, not asp.net. I've got some open ended questions that I was really hoping someone in here could answer, or direct me to some resources that will help me answer them on my own. ...
4
by: wish | last post by:
Hi all, Where is the location for store session variable? Because i feel that the session variable is keep store the previous variable and no release the variable. When i click back button,...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.