admin.php
<?php
// connection to MySQL server
mysql_connect('localhost','root','');
mysql_select_db('administration');
if (isset($_POST['username'])) {
$loginUsername=$_POST['username'];
$loginPassword=$_POST['password'];
$MM_redirectLoginSuccess = "validated.php";
$MM_redirectLoginFailed = "admin.php";
$MM_redirecttoReferrer = true;
$errors = array();
if(empty($_POST['username'])) {
$errors[] = 'You think whom going to fill up the USERNAME for you?';
}
if(empty($_POST['password'])) {
$errors[] = 'You think whom going to fill up the PASSWORD for you?';
}
if (empty($errors)) {
$loginUsername = get_magic_quotes_gpc() ? $loginUsername : addslashes($loginUsername);
$password = get_magic_quotes_gpc() ? $password : addslashes($password);
$LoginRS_query = "SELECT username, password FROM adminprofile WHERE username='$loginUsername' AND password='$loginPassword'";
$LoginRS = mysql_query($LoginRS_query) or die(mysql_error());
$loginFoundUser = mysql_num_rows($LoginRS);
if ($loginFoundUser) {
session_name ('YourVisitID');
session_start();
$_SESSION['user_id'] = $loginFoundUser[0];
$_SESSION['first_name'] = $loginFoundUser[1];
$_SESSION['agent'] = md5($_SERVER['HTTP_USER_AGENT']);
echo "<script type='text/javascript'>location.href='$MM_redirectLoginSuccess';</script>";
exit();
}
else {
echo "<script type='text/javascript'>location.href='$MM_redirectLoginFailed';</script>";
}
}
mysql_close();
}
?>
validated.php
<?php
session_name ('YourVisitID');
session_start(); // Start the session.
$MM_redirectLoginFailed = "admin.php";
$MM_redirecttoReferrer = true;
// If no session value is present, redirect the user.
if (!isset($_SESSION['agent']) OR ($_SESSION['agent'] != md5($_SERVER['HTTP_USER_AGENT'])) ) {
echo "<script type='text/javascript'>location.href='$MM_redirectLoginSuccess';</script>";
exit(); // Quit the script.
}
?>
I've two pages as displayed above. I wanted to enable sessions so that the user can not go to validated.php straight without any validation. But the the code above does not create any sessions at all. The codes in validated.php is working fine and the codes in admin.php is working fine without the line 32 - 36. This line did not create/store any sessions when executed. Each time, after I log in using the correct username and password, it will redirect me bak to the index.php. Please help.