当前位置:网站首页>Redirect to previous page after login? PHP - Redirecting to previous page after login? PHP

Redirect to previous page after login? PHP - Redirecting to previous page after login? PHP

2022-06-25 07:12:00 Technology feast

problem :

I've been searching for a solution but it's seem I can't get it right, no matter what I try. I've been looking for a solution , But whatever I try , I can't seem to solve the problem .

After successful login, the user should be redirected to the page he came from, let's say he's been browsing a post and wants to log in so he can leave a comment, so he should be redirected to the post he was browsing. After successful login , The user should be redirected to the page he came from , Suppose he has been browsing a post and wants to log in so that he can comment , So he should be redirected to the post he is browsing .So here is what I have: So this is what I have :

login.php shows the login form:login.php Show login form :

<form method="post" action="login-check.php">... //input for username and password</form>

The login-check.php checks if the username and pass are entered, does the user exist, or if he's already logged in, and a p parameter is sent to login.php:login-check.php Check whether the user name and password are entered , Does the user exist , Or whether he has logged in , And to login.php Send a p Parameters :

<?phpsession_start();if((empty($username) OR empty($password) AND !isset($_SESSION['id_login']))) {   header("Location:login.php?p=1");   exit();}elseif (!user_exists($username,$password) AND !isset($_SESSION['id_login'])) {   header("Location:login.php?p=2");   exit();}elseif(isset($_SESSION['id_login'])) {   header("Location:login.php?p=3");   exit();}?>

then parameter p is sent back to login.php and displays the according message: And then the parameters p Be sent back to login.php And display the corresponding message :

<?phpif(isset($_GET['p'])) {  $p = $_GET["p"];  if($p=="1")    echo "<p class=\"red\">You didn't fill the form.</p><br></br>";  if($p=="2")    echo "<p class=\"red\">User exists.</p><br></br>";  if($p=="3")    header("Location: index.php");}?>

BUT, instead of going to index.php after successful login, it should go to the page the user has previously been. however , After successful login , Not to index.php, Instead, go to the page the user has visited before .I've tried in different ways but ether it doesn't work at all or it returns to login.php. I tried different ways , But the ether doesn't work at all , Or it returns to login.php.It doesn't need to be super safe, because I'm doing this for a school project. It doesn't need super security , Because I am doing this for the school project .
ALSO, I consider myself pretty novice, so please have patience :D in addition , I think I am a novice , So please wait patiently :D


Solution :

Reference resources : https://stackoom.com/en/question/ywDU
原网站

版权声明
本文为[Technology feast]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202201234217379.html