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.
$_SESSION['url']
is not the same as $url
In your html, change <?php echo $url; ?>
to <?php echo $_SESSION['url']; ?>
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;
}
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/>