Navigate Back to The Page That Sent You
Posted: Fri Aug 01, 2008 8:43 pm
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.
Thanks

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.
Thanks