2013-09-16 69 views
0

我使用PHP將包含特殊字符(如@#$)的變量存儲到會話和cookie中,但是當我檢索它們時,我無法看到特殊存儲的會話中的字符和cookie變量。提前感謝。會話和cookie變量存儲沒有特殊字符

     //storing 
         $_SESSION['userid'] = $db_id; 
      $_SESSION['email'] = $db_email; 
      $_SESSION['password'] = $db_pass_str; 
      setcookie("id", $db_id, strtotime('+30 days'), "/", "", "", TRUE); 
      setcookie("email", $db_email, strtotime('+30 days'), "/", "", "", TRUE); 
      setcookie("pass", $db_pass_str, strtotime('+30 days'), "/", "", "", TRUE); 

    //Retreivel 
    if(isset($_SESSION["userid"]) && isset($_SESSION["email"]) && isset($_SESSION["password"])) 
{ 
    $log_id = preg_replace('#[^0-9]#', '', $_SESSION['userid']); 
    $log_email = preg_replace('#[^a-z0-9]#i', '', $_SESSION['email']); 
    $log_password = preg_replace('#[^a-z0-9]#i', '', $_SESSION['password']); 
    // Verify the user`enter code here` 

} 
else if(isset($_COOKIE["id"]) && isset($_COOKIE["email"]) && isset($_COOKIE["pass"])) 
{ 
    $_SESSION['userid'] = $_COOKIE['id']; 
    $_SESSION['email'] = $_COOKIE['email']; 
    $_SESSION['password'] = $_COOKIE['pass']; 
} 

當我回顯會話變量時,看不到我存儲的特殊字符。 例如:我保存了[email protected],但當我查看時,我只能看到testexample.com。

+0

你需要更多地解釋 –

+0

哪裏出現此問題顯示你的代碼。告訴我們這個代碼運行時的樣本輸入/輸出值。 –

+0

嗨外星人和邁克,我已經添加了code.Thanks您的支持提前。 – ravikanth

回答

2

使用htmlspecialchars來編碼您的原始字符串。 例如,

<?php 
$session_variable = "[email protected]#$%@"; 
$new = htmlspecialchars("<?php echo $session_variable; ?>", ENT_QUOTES); 
echo $new; // output: [email protected]#$%@ 
?> 
+0

因此,在將我的變量存儲在session/cookies之前,我應該使用方法「htmlspecialchars」,然後存儲我的變量? – ravikanth

+0

否。當您想要檢索會話變量時,請使用此方法。我正在改變答案。覈實。 –

+0

感謝ronak.I當我存儲會話/ cookies時使用了這種方法,並且它工作得很好。所以,根據你的說法,這在存儲會話/ cookie之前不應該起作用? – ravikanth