0

I want to set session on clicking of link so after login user will redirect to last visited page.

for example:

Step1 : user opened home page.(seesion url set to home page

$_SESSION['url'] = $_SERVER['REQUEST_URI'])

step2 : Next in new tab user will open contact page (again session variable reset)

step3: user will come to home page and click to login link.(session not set because no reload of page)

step4: after login user will redirect to contact page.

but here i want user to be redirect where user clicked the link for login.

i tried with following code but it not working for me.

PHP code:

session_start();

$_SESSION['url'] = $_SERVER['REQUEST_URI'];

$url=$_SESSION['url'];

HTML code

<div class="gallery_div"> 
    <p>
      Please Click <strong><a href="login.php?link=<?php echo $url; ?>" style="color:#993b05"> here </a></strong>to login

    </p></div>

In login page

session_start();
if(isset($_GET['link']))
{
$_SESSION['url'] = $_GET['link'];

}

when redirect this page it coming with parameter and so page not found is coming. because only login.php is there.

can any one help me on this.

6
  • As problem: you not open session. Try call session_start before write to session. Commented May 28, 2014 at 6:20
  • sorry in post i forgot. but in code used. Commented May 28, 2014 at 6:22
  • Where is $urldefined? Commented May 28, 2014 at 6:24
  • Do you have the correct url when you click the login link? try to echo the $_GET['link'] and the $_SESSION['url']. If you have it right you may need to redirect with header('Location:'.$_SESSION['url']);. Otherwise check if you reset or destroy your session on wrong positions. Commented May 28, 2014 at 6:29
  • Try to print_r($_SERVER) to see what is the right variable for last page because on localhost it may not work as on a specific domain. Commented May 28, 2014 at 6:40

3 Answers 3

1

You don't really have to use session to achieve this. You can redirect the user based on $_SERVER['HTTP_REFERER']. This will tell you the last visited page. You can even add some filtration here as needed.

if($_SERVER['HTTP_REFERER']!='' && stripos($_SERVER['HTTP_REFERER'],'mydomain.com')!==false){
   header('location:'.$_SERVER['HTTP_REFERER']);
   exit;
}
Sign up to request clarification or add additional context in comments.

1 Comment

Hello $_SERVER['HTTP_REFERER'] give last visited page "mydomain.com/display.php?ID=781" but here i have like "mydomain.com/display.php?ID=781#about" so how to get this #about is menu tabs in that page like about,share,setting and all. depending on user select that need to be go.. please help me on this
0

$_SESSION['url'] is not the same as $url

In your html, change <?php echo $url; ?> to <?php echo $_SESSION['url']; ?>

Comments

0

Two pages required.First one is for calling (on-click) a session and other sets the value of session through ajax.

< a href="#" onclick="setsession()" style="color:#993b05"> Login </a>
<br/>
<script>
<br/>
function setsession(){<br/>
if (window.XMLHttpRequest)  <br/>
  {// code for IE7+, Firefox, Chrome, Opera, Safari<br/>
  xmlhttp=new XMLHttpRequest(); <br/> 
  }<br/>
else<br/>
  {// code for IE6, IE5<br/>
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");<br/>
  }<br/>
xmlhttp.onreadystatechange=function()<br/>
  {<br/>
  if (xmlhttp.readyState==4 && xmlhttp.status==200)<br/>
    {<br/>
    document.getElementById("scat").innerHTML=xmlhttp.responseText;<br/>
    }<br/>
  }<br/>
<br/>
xmlhttp.open("post","setsession.php",true);// calls a page  where u can set session <br/>
xmlhttp.send();<br/>
}<br/>
</script><br/>
if(isset($_GET['link']))<br/>
{<br/>
$_SESSION['url'] = $_GET['link'];<br/>
<br/>
}<br/>
header("Location:redirect.php");<br/>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.