Jump to content

how to go to make auto go back to previous page after login?


lukelee

Recommended Posts

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

)

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.