2013-04-03 64 views
0

下面的代碼應該檢查是否有效的會話存在或有效的cookie,如果這樣include_once file A.php如果不是include_once login.phpPHP是用戶會話/ cookie的有效的重定向錯誤

到目前爲止,login.phplogout.php正在正確執行(會話/ cookie)被創建和銷燬,但以下代碼仍未顯示正確的內容。

因爲這些代碼代表我看到了login.php不管有效的會話或餅乾。

任何幫助將是偉大的。謝謝。

<?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'; 
} 
?> 
+1

什麼是'COOKIE_TIME_OUT'和'$ ctime'?你能打印這兩個嗎? – Tushar 2013-04-03 22:17:06

+0

@Tushar當你說打印你的意思是....(sr我有點在這裏我的深度) – 2013-04-03 22:20:14

+0

@Tushar如果你想我有一個stacksoverflow聊天打開。無需填寫此區域。 http://chat.stackoverflow.com/rooms/info/27461/discussion-between-aventus-and-webmaster-alex-l – 2013-04-03 22:22:01

回答

0

它花了兩個13hr天一些幫助,但似乎只有一個,我只是將不得不忍受與小型服務器因果關係的錯誤是工作的第3重寫後的事情。這個問題可以關閉。謝謝。