twilitegxa Posted July 17, 2009 Share Posted July 17, 2009 Not sure if this should be posted here or in the HTML section but this is part of another post in which I solved part of it. I would like to know how I can redirect the viewer to the last page they were at before they were directed to log in. Some pages I have it set to direct them to the log in page if they are not logged in in order to view the page. How can I redirect them back to the page they were viewing before they were directed to the log in page? This is my log in page currentlly: <?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> [/code I currently have it set to redirect them to the account.php page when they log in successfully using the following script: [code] <?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'); } } ?> How can I alter this script to do that? Is this an HTML topic? I foudn soem HTML code to redirect to the last visited page: window.location.href = window.history.back(1) Anyone know if I can use php to do this or do I need to move this to the HTML section for an answer and take out the php coding? Link to comment https://forums.phpfreaks.com/topic/166277-redirecting-based-on-last-page-mixed-with-php-statements/ Share on other sites More sharing options...
twilitegxa Posted July 17, 2009 Author Share Posted July 17, 2009 I read somewhere I could use the $_SERVER['PHP_SELF'], but I'm not sure how to use it int he current script I have. Any suggestions? Link to comment https://forums.phpfreaks.com/topic/166277-redirecting-based-on-last-page-mixed-with-php-statements/#findComment-876876 Share on other sites More sharing options...
twilitegxa Posted July 17, 2009 Author Share Posted July 17, 2009 I tried to add just that code $_SERVER['PHP_SELF'], but it just logged me in and redirected me to take login pag, whihc is not what I want. I want it to take me to the page right before the login page is accessed. How can I do that. I found that example with this script: <script> // Define the URL for when the user is logged in $url = 'http://' . $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']); // Make sure there is no trailing slash if ((substr($url, -1) == '/') OR (substr($url, -1) == '') ) { $url = substr ($url, 0, -1); // Chop off the slash. } // Redirect to the page at logon $url .= '/' . $_POST['username'] . '.php' </script> But I'm not sure how I can use this. Can anyone help? Link to comment https://forums.phpfreaks.com/topic/166277-redirecting-based-on-last-page-mixed-with-php-statements/#findComment-876879 Share on other sites More sharing options...
twilitegxa Posted July 17, 2009 Author Share Posted July 17, 2009 I currently have this for the redirect: <?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'); } } ?> Link to comment https://forums.phpfreaks.com/topic/166277-redirecting-based-on-last-page-mixed-with-php-statements/#findComment-876880 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.