Page 1 of 1

Navigate Back to The Page That Sent You

Posted: Fri Aug 01, 2008 8:43 pm
by zzack38
Hello, I am new to coding php and was trying to setup a login page to redirect back to the page that sent. :?

Here is the original code that I had before trying to setup the redirect

<?php

require_once 'config.php';
require_once 'inc/db_connect.php';

if (!session_id()) session_start();
if (!empty($_SESSION['user_id'])) {
header("Location: $site_base/logout.php");
exit;
}

$error = false;
$err_msg = array();

if (isset($_POST['submit'])) {
if (isset($_POST['login'])) $_POST['login'] = trim($_POST['login']);
if (
!isset($_POST['login']) || !preg_match('/^[a-z0-9_]{3,32}$/i', $_POST['login']) ||
!isset($_POST['password']) || !preg_match('/^[a-z0-9_]{3,32}$/i', $_POST['password'])
) {
$error = true;
$err_msg[] = 'Access Denied!';
} else {
$q = mysql_query(
"SELECT id, username, first_name, last_name, email FROM users ".
"WHERE username = '".mysql_real_escape_string($_POST['login'], $db)."' AND ".
"password = '".md5($_POST['password'])."' AND ".
"active = 'yes' AND blocked = 'no'",
$db
) or die(mysql_error($db));
if (mysql_num_rows($q) != 1) {
$error = true;
$err_msg[] = 'Access Denied!';
} else {
$user_row = mysql_fetch_assoc($q);
$_SESSION['user_id'] = $user_row['id'];
$_SESSION['username'] = $user_row['username'];
$_SESSION['first_name'] = $user_row['first_name'];
$_SESSION['last_name'] = $user_row['last_name'];
$_SESSION['email'] = $user_row['email'];
header("Location: $site_base/my_account.php");
exit;
}
mysql_free_result($q);
}
}

?>

==================================================================

Was trying to use...

if (isset($_SESSION['last_page_viewed'])) {
$_SESSION['last_page_viewed_2'] = $_SESSION['last_page_viewed'];
}

$_SESSION['last_page_viewed'] = $_SERVER['REQUEST_URI'];

==================================================================

and the session call

header("Location: " $_SERVER['last_page_viewed']");

==================================================================

I added it like this.....

<?php

require_once 'config.php';
require_once 'inc/db_connect.php';

if (!session_id()) session_start();
if (!empty($_SESSION['user_id'])) {
header("Location: $site_base/logout.php");

if (isset($_SESSION['last_page_viewed'])) {
$_SESSION['last_page_viewed_2'] = $_SESSION['last_page_viewed'];
}

$_SESSION['last_page_viewed'] = $_SERVER['REQUEST_URI'];

exit;
}

$error = false;
$err_msg = array();

if (isset($_POST['submit'])) {
if (isset($_POST['login'])) $_POST['login'] = trim($_POST['login']);
if (
!isset($_POST['login']) || !preg_match('/^[a-z0-9_]{3,32}$/i', $_POST['login']) ||
!isset($_POST['password']) || !preg_match('/^[a-z0-9_]{3,32}$/i', $_POST['password'])
) {
$error = true;
$err_msg[] = 'Access Denied!';
} else {
$q = mysql_query(
"SELECT id, username, first_name, last_name, email FROM users ".
"WHERE username = '".mysql_real_escape_string($_POST['login'], $db)."' AND ".
"password = '".md5($_POST['password'])."' AND ".
"active = 'yes' AND blocked = 'no'",
$db
) or die(mysql_error($db));
if (mysql_num_rows($q) != 1) {
$error = true;
$err_msg[] = 'Access Denied!';
} else {
$user_row = mysql_fetch_assoc($q);
$_SESSION['user_id'] = $user_row['id'];
$_SESSION['username'] = $user_row['username'];
$_SESSION['first_name'] = $user_row['first_name'];
$_SESSION['last_name'] = $user_row['last_name'];
$_SESSION['email'] = $user_row['email'];
header("Location: " $_SERVER['last_page_viewed']");

exit;
}
mysql_free_result($q);
}
}

?>

=================================================================

I am getting an error code of

Parse error: syntax error, unexpected T_VARIABLE in /home/content/mysite/html/login.php on line 48

Please advise what I have wrong in my coding. :banghead:

Thanks
:)

Re: Navigate Back to The Page That Sent You

Posted: Fri Aug 01, 2008 8:59 pm
by EverLearning

Code: Select all

header("Location: " $_SERVER['last_page_viewed']");
 
should be

Code: Select all

header("Location: " . $_SERVER['last_page_viewed']);
.

P.S. use code tags when posting your code, as it makes your code way more readable, and more people will be willing to take a look at it.

Re: Navigate Back to The Page That Sent You

Posted: Fri Aug 01, 2008 9:40 pm
by zzack38
Sorry about that, didn't know... here is the code with the code tags..
Thanks

Code: Select all

<?php
 
require_once 'config.php';
require_once 'inc/db_connect.php';
 
if (!session_id()) session_start();
if (!empty($_SESSION['user_id'])) {
header("Location: $site_base/logout.php");
 
if (isset($_SESSION['last_page_viewed'])) {
$_SESSION['last_page_viewed_2'] = $_SESSION['last_page_viewed'];
}
 
$_SESSION['last_page_viewed'] = $_SERVER['REQUEST_URI'];
 
exit;
}
 
$error = false;
$err_msg = array();
 
if (isset($_POST['submit'])) {
if (isset($_POST['login'])) $_POST['login'] = trim($_POST['login']);
if (
!isset($_POST['login']) || !preg_match('/^[a-z0-9_]{3,32}$/i', $_POST['login']) ||
!isset($_POST['password']) || !preg_match('/^[a-z0-9_]{3,32}$/i', $_POST['password'])
) {
$error = true;
$err_msg[] = 'Access Denied!';
} else {
$q = mysql_query(
"SELECT id, username, first_name, last_name, email FROM users ".
"WHERE username = '".mysql_real_escape_string($_POST['login'], $db)."' AND ".
"password = '".md5($_POST['password'])."' AND ".
"active = 'yes' AND blocked = 'no'",
$db
) or die(mysql_error($db));
if (mysql_num_rows($q) != 1) {
$error = true;
$err_msg[] = 'Access Denied!';
} else {
$user_row = mysql_fetch_assoc($q);
$_SESSION['user_id'] = $user_row['id'];
$_SESSION['username'] = $user_row['username'];
$_SESSION['first_name'] = $user_row['first_name'];
$_SESSION['last_name'] = $user_row['last_name'];
$_SESSION['email'] = $user_row['email'];
header("Location: " $_SERVER['last_page_viewed']");
 
exit;
}
mysql_free_result($q);
}
}
 
?>
 

Re: Navigate Back to The Page That Sent You

Posted: Fri Aug 01, 2008 9:42 pm
by EverLearning
Did you make the change I suggested and tested your code again?

Re: Navigate Back to The Page That Sent You

Posted: Sat Aug 02, 2008 7:31 pm
by zzack38
I added in the correction and tested. The script goes through without any error but the page goes blank and the link login.php is shown on the url. :?

Re: Navigate Back to The Page That Sent You

Posted: Sat Aug 02, 2008 11:56 pm
by Bill H
$_SESSION['last_page_viewed'] = $_SERVER['REQUEST_URI'];
$_SERVER['REQUEST_URI'] is the command which sent the user to the current page, usually the same as $_SERVER['PHP_SELF'] or some variation thereof.

I think what you are looking for is $_SERVER['HTTP_REFERER'], although it is not a completely relaible var.

Re: Navigate Back to The Page That Sent You

Posted: Sun Aug 03, 2008 7:54 pm
by zzack38
Ok I tried $_SERVER['HTTP_REFERER'] in place of $_SERVER['REQUEST_URI']

I still get a blank page after the login. If I hit the browser back button from the blank page, the page shows up and it's logged in.

Any ideas about the blank page :?:

Thanks

Re: Navigate Back to The Page That Sent You

Posted: Sun Aug 03, 2008 8:26 pm
by Bill H
On line 11 you are setting $_SESSION['last_page_viewed_2'] = $_SESSION['last_page_viewed']; but I don't see where you ever do anything with the latter variable.

On line 14 you set the value of $_SESSION['last_page_viewed'] and immediately killing the script with exit(); which doesn't seem useful.

Then on line 48 you are redirecting to $_SERVER['last_page_viewed'] instead of $_SESSION['last_page_viewed'].

I'm not surprised by the blank page. I don't think it's your script that's getting back to the last page, it's your browser's back button that's doing so.

Re: Navigate Back to The Page That Sent You

Posted: Sun Aug 03, 2008 8:33 pm
by omniuni
I'm not quite sure how you're doing this, but why don't you just put a tag like this at the top of each page that you would need to track:

Code: Select all

<?php $_SESSION['pageID'] = "myID" ?>
and make your logout page link to there?

-OmniUni

Re: Navigate Back to The Page That Sent You

Posted: Sun Aug 03, 2008 11:20 pm
by zzack38
Thanks Bill H, I have done what you suggested and it's working great now. :)

P.S. Also I would like to thank everyone who has replied... Great Forum!!!