The following code should check if a valid session exists or a valid cookie and if so include_once file A.php
if not include_once login.php
.
So far the login.php
and logout.php
are executing correctly (session/cookie) are being created and destroyed but the following code is still not displaying the proper content.
As this code stands I am seeing the login.php
regardless of valid session or cookie.
Any help would be great. Thank you.
<?php
include_once '../accounts/dbc.php';
if (isset($_SESSION['user_id']) && isset($_SESSION['user_name']) )
{
include_once 'A.php';
}
else if(isset($_COOKIE['user_id']) && isset($_COOKIE['user_key'])){
/* we double check cookie expiry time against stored in database */
$cookie_user_id = filter($_COOKIE['user_id']);
$rs_ctime = mysql_query("select `ckey`,`ctime` from `users` where `id` ='$cookie_user_id'") or die(mysql_error());
list($ckey,$ctime) = mysql_fetch_row($rs_ctime);
// coookie expiry
if( (time() - $ctime) > 60*60*24*COOKIE_TIME_OUT) {
include_once '../login.php';
}
/* Security check with untrusted cookies - dont trust value stored in cookie.
/* We also do authentication check of the `ckey` stored in cookie matches that stored in database during login*/
if( !empty($ckey) && is_numeric($_COOKIE['user_id']) && isUserID($_COOKIE['user_name']) && $_COOKIE['user_key'] == sha1($ckey) ) {
session_regenerate_id(); //against session fixation attacks.
$_SESSION['user_id'] = $_COOKIE['user_id'];
$_SESSION['user_name'] = $_COOKIE['user_name'];
/* query user level from database instead of storing in cookies */
list($user_level) = mysql_fetch_row(mysql_query("select user_level from users where id='$_SESSION[user_id]'"));
$_SESSION['user_level'] = $user_level;
$_SESSION['HTTP_USER_AGENT'] = md5($_SERVER['HTTP_USER_AGENT']);
include_once 'A.php';
}
else {
include_once '../login.php';
}
} else {
include_once '../login.php';
}
?>