Jump to content

Redirect to previous page?


twilitegxa

Recommended Posts

How can I change my page to take out the extra step of directing the user to the login page? I want to use a redirect script that redirects the user to the previous page they were at before being asked to log in, but I'm not having very much luck. Right now, my login page redirects them to a specific page (account.php), but I don't want to do that anymore. Now I want it to just ask them to log in and then redirect them back to the page they weer at when they were asked to log in. Can anyone tell me how I can alter my script to do this?

 

Here is my login page:

 

<?php

//Access Tracking Snippet

//set up static variables
$page_title = "login.php";
$user_agent = getenv("HTTP_USER_AGENT");
$date_accessed = date("Y-m-d");

//connect to server and select database
$conn = mysql_connect("localhost", "root", "")
or die(mysql_error());
$db = mysql_select_db("smrpg", $conn) or die(mysql_error());

//create and issue query
$sql = "insert into access_tracker values
('', '$page_title', '$user_agent', '$date_accessed')";
mysql_query($sql,$conn);
?>

<?php
session_start();

$user_area_location = 'account.php'; // Location of the user area
// Connect to MySQL database:
$access = mysql_connect('localhost','root','') or die ('Could not connect to database');
mysql_select_db('smrpg',$access) or die ('Could not select table');
# #
$error = array();
if(isset($_GET['action'])) {
switch($_GET['action']) {
case 'logoff':
unset($_SESSION['loggedIn']);
array_push($error, 'You were logged off.');
break;
}
}
if(!$error) {
if(empty($_POST['username'])) { array_push($error, 'You didn\'t supply a username'); }
if(empty($_POST['password'])) { array_push($error, 'You didn\'t supply a password'); }
}
if(!$error){
$result = @mysql_query('SELECT username, email, name FROM `users` WHERE username = \''.mysql_real_escape_string($_POST['username']).'\' AND password = \''.mysql_real_escape_string(md5($_POST['password'])).'\'');
if($row = @mysql_fetch_array($result)) {
$_SESSION['loggedIn'] = true;
$_SESSION['userName'] = $row['username']; 
$_SESSION['userMail'] = $row['email'];
$_SESSION['name'] = $row['name'];
header('Location: '.$user_area_location);
die('<a href="'.$user_area_location.'">Go to your user account</a>');
}else{
array_push($error, 'The username or password you provided were not correct');
}
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Sailor Moon RPG - Login</title>
<!-- Source File -->
<style type="text/css" media="screen">
/*<![CDATA[*/
@import url(global.css); 
/*]]>*/
</style>
</head>
<body>
<!-- HEADER -->
<h1 class="logo">Sailor Moon RPG</h1>
<!-- /HEADER -->
<?php include("topnav.php"); ?>
<div id="main">
<?php include("includes/log.php"); ?>
<?php include("mainnav.php"); ?>
<table cellspacing="2" cellpadding="0" border="0">
<form method="post" action="login.php">
<?php if(isset($error) && $error) { ?>
<tr>
<td colspan="2">
<ul><?php foreach($error as $key => $value) echo '<li>'.$value.'</li>'; ?></ul>
</td>
</tr><?php } ?>
<tr>
<td>Username:</td>
<td><input type="text" name="username" /></td>
</tr>
<tr>
<td>Password:</td>
<td><input type="password" name="password" /></td>
</tr>
<tr>
<td> </td>
<td><input type="submit" name="submit" value="Login!" /> <a href="forgot.php">I forgot my username or password</a></td>

</tr>
</form>
</table>
</div>
<?php include("bottomnav.php"); ?>
<!-- FOOTER -->
<div id="footer_wrapper">
<div id="footer">
<p>Sailor Moon and all characters are<br>
trademarks of Naoko Takeuchi.</p>
<p>Copyright © 2009 Liz Kula. All rights reserved.<br>
A product of <a href="#" target="_blank">Web Designs By Liz</a> systems.</p>
<div id="foot-nav"><!-- <ul>
<li><a href="http://validator.w3.org/check?uri=http://webdesignsbyliz.com/digital/index.php" target="_blank"><img src="http://www.w3.org/Icons/valid-xhtml10-blue" alt="Valid XHTML 1.0 Transitional" height="31" width="88" /></a></li>
<li><a href="http://jigsaw.w3.org/css-validator/validator?uri=http://webdesignsbyliz.com/digital/global.css" target="_blank"><img class="c2" src="http://jigsaw.w3.org/css-validator/images/vcss-blue" alt="Valid CSS!" /></a></li>
</ul> --></div>
</div>
</div>
<!-- /FOOTER -->
</body>
</html>

 

And here is a page that requires the user to be logged in:

 

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html lang="en">
<?php

session_start();

if(!isset($_SESSION['loggedIn'])) {
header("Location: login.php");
}

//Access Tracking Snippet

//set up static variables
$page_title = "creationform.php";
$user_agent = getenv("HTTP_USER_AGENT");
$date_accessed = date("Y-m-d");

//connect to server and select database
$conn = mysql_connect("localhost", "root", "")
        or die(mysql_error());
$db = mysql_select_db("smrpg", $conn) or die(mysql_error());

//create and issue query
$sql = "insert into access_tracker values
        ('', '$page_title', '$user_agent', '$date_accessed')";
mysql_query($sql,$conn);

$gender = (!empty($_POST['gender']))?$_POST['gender']:""; //Male or female?
$status = (!empty($_POST['status']))?$_POST['status']:""; //Hero or villain?

if($status == "villain"){
if($gender == "male") {
	//You are a male villain
	header("Location: mdark_warrior.php");
	exit;
	}elseif($gender == "female"){
	//You are a female villain
	header("Location: fdark_warrior.php");
	exit;
	}
}elseif($status == "hero"){
        if($gender == "male"){
                //You are a male hero!
                header("Location: knight.php");
        exit;
        }elseif($gender == "female"){
                //You are a female hero!
                header("Location: scout.php");
        exit;
        }
}
?>
<head>
<title>Sailor Moon RPG - Character Creation - Step 2: Character Outline</title>

<style type="text/css" media="screen">
/*<![CDATA[*/
@import url(global.css); 
/*]]>*/
</style>
</head>
<body>
<!-- HEADER -->
<h1 class="logo">Sailor Moon RPG</h1>
<!-- /HEADER -->
<?php include("topnav.php"); ?>
<div id="main">
<?php include("includes/log.php"); ?>
<?php include("mainnav.php"); ?>
<h1>Step 2: Character Outline - Creation</h1>
<form action="<?php echo $_SERVER['PHP_SELF']?>" method="post">
<table>
<tr>
<td>Would you like to create a:</td>
<td><input type="radio" name="status" value="hero"> Hero or a <input type="radio" name="status" value="villain" class="input1"> Villain</td>
</tr>
<tr>
<td></td>
<td><input type="radio" name="gender" value="female"> Female or <input type="radio" name="gender" value="male"> Male</td>
</tr>
</table>
<br>
<input type="submit" name="submit" value="Create Character" class="button1"> <input type="reset" name="reset" value="Reset"></form><br>

Or, would you like to use an <a href="existing.php">existing character</a>?
</div>
<?php include("bottomnav.php"); ?>
<!-- FOOTER -->
<div id="footer_wrapper">
<div id="footer">
<p>Sailor Moon and all characters are<br>
trademarks of Naoko Takeuchi.</p>
<p>Copyright © 2009 Liz Kula. All rights reserved.<br>
A product of <a href="#" target="_blank">Web Designs By Liz</a> systems.</p>
<div id="foot-nav"><!-- <ul>
<li><a href="http://validator.w3.org/check?uri=http://webdesignsbyliz.com/digital/index.php" target="_blank"><img src="http://www.w3.org/Icons/valid-xhtml10-blue" alt="Valid XHTML 1.0 Transitional" height="31" width="88" /></a></li>
<li><a href="http://jigsaw.w3.org/css-validator/validator?uri=http://webdesignsbyliz.com/digital/global.css" target="_blank"><img class="c2" src="http://jigsaw.w3.org/css-validator/images/vcss-blue" alt="Valid CSS!" /></a></li>
</ul> --></div>
</div>
</div>
<!-- /FOOTER -->
</body>
</html>

Link to comment
Share on other sites

Use the following variable to obtain the url of the page that referred the user:

 

$referrer = $_SERVER['HTTP_REFERER'];

 

Just use a Header('Location: '.$referrer.'); on the page that the users go to after submitting everything and being logged in. (IE: the page they post to)

 

Link to comment
Share on other sites

It is giving this error and showing the login page, and showing the user logged in but still asking them to log in:

 

Notice: Undefined index: HTTP_REFERER in C:\wamp\www\login.php on line 24

 

This i what I changed:

 

<?php
session_start();

$ref = $_SERVER['HTTP_REFERER']; // Location of the user area
// Connect to MySQL database:
$access = mysql_connect('localhost','root','') or die ('Could not connect to database');
mysql_select_db('smrpg',$access) or die ('Could not select table');
# #
$error = array();
if(isset($_GET['action'])) {
switch($_GET['action']) {
case 'logoff':
unset($_SESSION['loggedIn']);
array_push($error, 'You were logged off.');
break;
}
}
if(!$error) {
if(empty($_POST['username'])) { array_push($error, 'You didn\'t supply a username'); }
if(empty($_POST['password'])) { array_push($error, 'You didn\'t supply a password'); }
}
if(!$error){
$result = @mysql_query('SELECT username, email, name FROM `users` WHERE username = \''.mysql_real_escape_string($_POST['username']).'\' AND password = \''.mysql_real_escape_string(md5($_POST['password'])).'\'');
if($row = @mysql_fetch_array($result)) {
$_SESSION['loggedIn'] = true;
$_SESSION['userName'] = $row['username']; 
$_SESSION['userMail'] = $row['email'];
$_SESSION['name'] = $row['name'];
header( 'refresh: 3; url='.$ref);

}else{
array_push($error, 'The username or password you provided were not correct');
}
}
?>

Link to comment
Share on other sites

You can make the login POST the current page when logging in, or use a get variable such as .. login.php?ret=this. A cookie would also be a wise thing to look into, they are much simple.

 

HTTP referrers are never reliable, and most of the time do not set, I've not really seen many sites use the referrer to send them back.. since it could be spoofed into something or not reliable at all..

Link to comment
Share on other sites

How could I alter my page to include the login script on it? Like, if they want to view the page listed above, I want it to display, "You must log in to view this page.", and display the log in information (enter username and password input boxes like on my login.php). Then, after they log in, to show that page, but without the login input boxes and to show that they are logged in on the right. Right now how I have it is the page redirects them to to the separate login page, then when they log in, it redirects them to the account.php page. I want to eliminate then need to redirect. If I can just do what I explained all on the same page in a single script, then I won't need a redirect. Can anyone help me do it? I'm not very good with php yet. :-(

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.