lukelee Posted January 27, 2009 Share Posted January 27, 2009 I want to make a function: after you login, it auto go back to the previous page, does anyone know how to write the code? Quote Link to comment https://forums.phpfreaks.com/topic/142608-how-to-go-to-make-auto-go-back-to-previous-page-after-login/ Share on other sites More sharing options...
MadTechie Posted January 27, 2009 Share Posted January 27, 2009 Its hard to say without knowing the code behind it but the basic concept is as follows On the page that redirects you to the login page save the URL in a session ie 'ePage'.. then on login if 'ePage' is set redirect to it Quote Link to comment https://forums.phpfreaks.com/topic/142608-how-to-go-to-make-auto-go-back-to-previous-page-after-login/#findComment-747464 Share on other sites More sharing options...
lukelee Posted January 27, 2009 Author Share Posted January 27, 2009 Its hard to say without knowing the code behind it but the basic concept is as follows On the page that redirects you to the login page save the URL in a session ie 'ePage'.. then on login if 'ePage' is set redirect to it here is my code: <?php session_start(); session_destroy(); $Login=$_POST['Login']; if($Login){ $username=$_POST['username']; $password=$_POST['password']; require_once('db.php'); $result=mysql_query("select * from member where username='$username' and password='$password'"); if(mysql_num_rows($result)!='0'){ session_register("username"); header("index.php"); exit; }else{ echo "Wrong username and password, back in 3 sec!"; } } ?> <meta http-equiv="Refresh" content="3; URL=index.php" > if the user is invalid, go to index.php, but if the user is valid, go to the previous page, I have login from in every page, so is there any codes to let you go back 1 page? Quote Link to comment https://forums.phpfreaks.com/topic/142608-how-to-go-to-make-auto-go-back-to-previous-page-after-login/#findComment-747473 Share on other sites More sharing options...
gevans Posted January 27, 2009 Share Posted January 27, 2009 What I do is in my main include file (included on everypage) save a cookie of the current page url. Pages such as register and login/logout don't update the cookie (as you don't want to redirect them to login after login ) After registration/login/logout my header points at the cookie if it's set or index if it's not. Hope that's useful. Quote Link to comment https://forums.phpfreaks.com/topic/142608-how-to-go-to-make-auto-go-back-to-previous-page-after-login/#findComment-747478 Share on other sites More sharing options...
MadTechie Posted January 27, 2009 Share Posted January 27, 2009 Okay update the code to the below $result=mysql_query("select * from member where username='$username' and password='$password'"); if(mysql_num_rows($result)!='0'){ session_register("username"); $page = (!empty($_SESSION['gPage']))?$_SESSION['gPage']:"index.php";//add header($page); //update exit; }else{ echo "Wrong username and password, back in 3 sec!"; } (you could use $_COOKIE instead of $_SESSION and add <?php session_start(); $_SESSION['gPage'] = $_SERVER['REQUEST_URI']; ?> at the top of your pages ( you could use <?php setcookie("gPage", $_SERVER['REQUEST_URI']); ?> instead ) Quote Link to comment https://forums.phpfreaks.com/topic/142608-how-to-go-to-make-auto-go-back-to-previous-page-after-login/#findComment-747483 Share on other sites More sharing options...
lukelee Posted January 27, 2009 Author Share Posted January 27, 2009 no, it doesnt work, and also i dont understand what is $page = (!empty($_SESSION['gPage']))?$_SESSION['gPage']:"index.php"; why do we put index.php here? Quote Link to comment https://forums.phpfreaks.com/topic/142608-how-to-go-to-make-auto-go-back-to-previous-page-after-login/#findComment-747517 Share on other sites More sharing options...
lukelee Posted January 27, 2009 Author Share Posted January 27, 2009 I have made it, I put $htmpage=$_SERVER['HTTP_REFERER']; at the top, then <meta HTTP-EQUIV="REFRESH" CONTENT="3;URL=<? echo $htmpage; ?>"> thanks guys Quote Link to comment https://forums.phpfreaks.com/topic/142608-how-to-go-to-make-auto-go-back-to-previous-page-after-login/#findComment-747536 Share on other sites More sharing options...
gevans Posted January 27, 2009 Share Posted January 27, 2009 You might find that that causes a problem if there's a failed login attempt. As it will re-load login.php and the referer will be the same page. Just so that you're aware Quote Link to comment https://forums.phpfreaks.com/topic/142608-how-to-go-to-make-auto-go-back-to-previous-page-after-login/#findComment-747552 Share on other sites More sharing options...
MadTechie Posted January 27, 2009 Share Posted January 27, 2009 no, it doesnt work, and also i dont understand what is $page = (!empty($_SESSION['gPage']))?$_SESSION['gPage']:"index.php"; why do we put index.php here? If $_SESSION['gPage'] is empty goto index.php instead.. type in the URL of the login page directly into the address bar and then login.. what happens! Oh what didn't work ? and i should of said don't add <?php session_start(); $_SESSION['gPage'] = $_SERVER['REQUEST_URI']; ?> to the login page! Quote Link to comment https://forums.phpfreaks.com/topic/142608-how-to-go-to-make-auto-go-back-to-previous-page-after-login/#findComment-747602 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.