2010-06-11 86 views
8

當我使用PHP頭重定向所有會話變量丟失...有人說,添加退出();在標題後面(「」);將解決問題,但它似乎並不是解決方案...會話丟失頁面重定向後在php

任何人都可以請幫忙嗎?

這是我如何存儲可變進會議:

include 'dbc.php'; 

$err = array(); 

foreach($_GET as $key => $value) { 
    $get[$key] = filter($value); //get variables are filtered. 
} 

if ($_POST['doLogin']=='Login') 
{ 

foreach($_POST as $key => $value) { 
    $data[$key] = filter($value); // post variables are filtered 
} 


$user_email = $data['usr_email']; 
$pass = $data['pwd']; 


if (strpos($user_email,'@') === false) { 
    $user_cond = "user_name='$user_email'"; 
} else { 
     $user_cond = "user_email='$user_email'"; 

} 


$result = mysql_query("SELECT `id`,`pwd`,`full_name`,`approved`,`user_level` FROM users WHERE 
      $user_cond 
      AND `banned` = '0' 
      ") or die (mysql_error()); 
$num = mysql_num_rows($result); 

    // Match row found with more than 1 results - the user is authenticated. 
    if ($num > 0) { 

    list($id,$pwd,$full_name,$approved,$user_level) = mysql_fetch_row($result); 

    if(!$approved) { 
    //$msg = urlencode("Account not activated. Please check your email for activation code"); 
    $err[] = "Account not activated. Please check your email for activation code"; 

    //header("Location: login.php?msg=$msg"); 
    //exit(); 
    } 

     //check against salt 
    if ($pwd === PwdHash($pass,substr($pwd,0,9))) { 
    // this sets session and logs user in 
     session_start(); 
     session_regenerate_id (true); //prevent against session fixation attacks. 

     // this sets variables in the session 
     $_SESSION['user_id']= $id; 
     $_SESSION['user_name'] = $full_name; 
     $_SESSION['user_level'] = $user_level; 
     $_SESSION['HTTP_USER_AGENT'] = md5($_SERVER['HTTP_USER_AGENT']); 

     //update the timestamp and key for cookie 
     $stamp = time(); 
     $ckey = GenKey(); 
     mysql_query("update users set `ctime`='$stamp', `ckey` = '$ckey' where id='$id'") or die(mysql_error()); 

     //set a cookie 

     if(isset($_POST['remember'])){ 
        setcookie("user_id", $_SESSION['user_id'], time()+60*60*24*COOKIE_TIME_OUT, "/"); 
        setcookie("user_key", sha1($ckey), time()+60*60*24*COOKIE_TIME_OUT, "/"); 
        setcookie("user_name",$_SESSION['user_name'], time()+60*60*24*COOKIE_TIME_OUT, "/"); 
        } 
     if(empty($err)){    
      header("Location: myaccount.php"); 
     } 
     } 
     else 
     { 
     //$msg = urlencode("Invalid Login. Please try again with correct user email and password. "); 
     $err[] = "Invalid Login. Please try again with correct user email and password."; 
     //header("Location: login.php?msg=$msg"); 
     } 
    } else { 
     $err[] = "Error - Invalid login. No such user exists"; 
     }  
} 

重定向代​​碼:

//connect database 
    require_once 'dbc.php'; 

    page_protect(); 

    $authorID = $_SESSION['user_id']; 
    if (!empty($_POST["answ_content"]) && $authorID != 0) { 
      //vaqciot html chveulebriv texad 
      $content = htmlentities($_POST["answ_content"],ENT_COMPAT,'UTF-8'); 
      $dro = date('Y-m-d H:i:s'); 
      $qID = $_POST["question_ID"]; 
      $author = $_SESSION["user_name"]; 


      $sql="INSERT INTO wp_comments (comment_ID, comment_post_ID, comment_author, comment_author_IP, comment_date, comment_content, user_id) 
       VALUES 
       (NULL, '$qID', '$author', '123.123.123.123', '$dro', '$content', '$authorID')"; 

      $result = mysql_query($sql); 

      //pasuxebis raodenobis ertit gazrda 
      $increase = "UPDATE wp_posts SET comment_count = comment_count+1 WHERE ID = $qID"; 
      mysql_query($increase); 

      //gadamisamarteba shekitxvis gverdze 
      $url = 'Location:http://example.com/site/answ/question.php?ID=' .$qID; 
      header($url); 
    } else { 
     echo 'error'; 
    } 
+0

請顯示您重定向到的一些代碼和一些示例地址。 – 2010-06-11 14:01:06

+2

你能告訴我們一些代碼嗎?在用戶電腦上啓用cookies?你是否在會話中使用cookie,或者你是否總是將session-id作爲get-parameter發送,並忘記了在你的重定向? – oezi 2010-06-11 14:02:41

+0

添加所有必要的代碼... – 2010-06-11 14:07:52

回答

11

你需要把exit();你的頭重定向後,否則你剛纔加載的內容的兩頁分成1頁。

此外,請確保您的session_start();位於所有腳本的頂部。

+0

我加了exit();但它不起作用! 不幸的是,我不得不使用重定向,因爲網站結構... – 2010-06-11 14:31:11

+4

你爲什麼把答案標記爲接受...?雖然沒有修復我的! – Eliethesaiyan 2013-04-18 08:28:12

+1

這裏的關鍵是session_start();在所有頁面的頂部 – Lizard 2013-04-20 00:11:29

4

您未開始會話。爲了使用會話變量並讓它們傳送頁面,您需要將

session_start(); 

放在每個頁面頂部的任何其他位置之前。

1

簡單!確保您來自的網頁(例如www.example.com)在開始時重定向到(例如www.example.com/redirect.php)通知www。如果你從一個頁面到另一個頁面改變它,那麼是的,事情會變得不可靠。

+0

同樣的問題,頁眉重定向正在不斷改變,我將其切換到整個項目中使用的通用方案,瞧,它工作..把我的屁股在這個至少一個星期。 ..謝謝你,你是一個救世主... – coder101 2013-11-25 15:27:13

0

這些會話並不總是按照我們預期的那樣工作。我的網站使用了會話丟失的類似問題。我基本上是通過在第一次加載頁面時將我希望保留在會話中的值注入隱藏文本字段來解決此問題。然後我第二次打電話給頁面(頁面提交),我只是從隱藏文本字段中讀取值,並繼續使用我的代碼。

這比在這種情況下使用會話更容易和更清潔!

+3

它也很不安全。某些會話數據僅與服務器相關,不應暴露給客戶端。 – konqi 2012-10-11 09:46:26

+1

與JoSo一致,會話比視圖狀態更清潔,會話數據不被視爲視圖狀態數據... – 2012-10-18 17:48:26

+0

隱藏的textfield是什麼意思...?通過查看您的html源代碼無法檢查的內容? – Eliethesaiyan 2013-04-18 08:25:53

2

我試圖設置用自己的會話ID:

session_id('own_generated_session_id_string'); 

但作爲documentation說,你必須在session_start前後

session_start(); 

使用該使用它()清除會話參數。

0

exit;應放置在標題重定向或session_regenerate_id(true)之後;可以使用