我寫了一個函數,用於檢查cookie是否已設置,如果未設置,則設置cookie。在函數中檢查並設置cookie
因爲cookie不是馬上可用的,所以我需要刷新頁面一次,因此我可以訪問該值。
但是,當我調用該函數時,它只是不斷重新加載頁面。當它在功能外正常工作。只有當它在一個函數內部時,我纔會這樣做。
function getCookie(){
if(isset($_COOKIE['ID'])){
$cookieID = $_COOKIE['ID'];
}
else{
//generate random value for cookie id
$charid = strtoupper(md5(uniqid(rand(), true)));
$uuid = substr($charid, 0, 8)
.substr($charid,20,12);
setcookie("ID", $uuid, strtotime('+7 days'));
$cookieID = $_COOKIE['ID'];
echo "<META HTTP-EQUIV='Refresh' CONTENT='0'> ";
}
echo $cookieID;
}
在那裏你可以打電話給你的'get_cookie()'函數? – Ranjith 2013-04-24 10:20:57
在index.php文件中,它包含了header.php文件>它包含了包含這個函數的functions.php – user2183216 2013-04-24 10:25:37
以正確的方式檢查函數,例如'echo'hello';'類似於那。 – Ranjith 2013-04-24 10:29:05