PHP COOKIE及時(shí)生效的方法介紹
更新時(shí)間:2014年02月14日 09:26:28 作者:
本篇文章主要是對(duì)PHP中COOKIE及時(shí)生效的方法進(jìn)行了介紹,需要的朋友可以過來參考下,希望對(duì)大家有所幫助
通常,php里要瀏覽器刷一下才能出現(xiàn)cookie,怎么才能讓cookie及時(shí)生效呢,下面分享一個(gè)讓cookie及時(shí)生效的一個(gè)方法,很實(shí)用,代碼如下:
/**
* 設(shè)置cookie
* @param string $name 鍵名
* @param mixed $value 值
* @param int $expire 過期時(shí)間,默認(rèn)是一天
*/
public final function setCookie($name, $value, $expire = null){
//cookie值為空,退出
if(empty($value)) return;
//過期時(shí)間
if(empty($expire)) $expire = time() + 86400;
$_COOKIE[$name] = $value;
//判斷value是否是數(shù)組
if(is_array($value)){
foreach ($value as $k => $v){
if(empty($v)) continue;
setcookie($name . "[$k]", $v, $expire);
}
}else{
setcookie($name, $value, $expire);
}
}
復(fù)制代碼 代碼如下:
/**
* 設(shè)置cookie
* @param string $name 鍵名
* @param mixed $value 值
* @param int $expire 過期時(shí)間,默認(rèn)是一天
*/
public final function setCookie($name, $value, $expire = null){
//cookie值為空,退出
if(empty($value)) return;
//過期時(shí)間
if(empty($expire)) $expire = time() + 86400;
$_COOKIE[$name] = $value;
//判斷value是否是數(shù)組
if(is_array($value)){
foreach ($value as $k => $v){
if(empty($v)) continue;
setcookie($name . "[$k]", $v, $expire);
}
}else{
setcookie($name, $value, $expire);
}
}
相關(guān)文章
PHP將Excel導(dǎo)入數(shù)據(jù)庫及數(shù)據(jù)庫數(shù)據(jù)導(dǎo)出至Excel的方法
這篇文章主要介紹了PHP將Excel導(dǎo)入數(shù)據(jù)庫及數(shù)據(jù)庫數(shù)據(jù)導(dǎo)出至Excel的方法,涉及php操作數(shù)據(jù)庫及Excel的相關(guān)技巧,需要的朋友可以參考下2015-06-06
php正則匹配文章中的遠(yuǎn)程圖片地址并下載圖片至本地
這篇文章主要介紹了php正則匹配文章中的遠(yuǎn)程圖片地址并下載圖片至本地的實(shí)現(xiàn)技巧,可實(shí)現(xiàn)采集并保存遠(yuǎn)程圖片的功能,是非常實(shí)用的技巧,感興趣的小伙伴可以參考一下2015-09-09
php curl獲取https頁面內(nèi)容,不直接輸出返回結(jié)果的設(shè)置方法
今天小編就為大家分享一篇php curl獲取https頁面內(nèi)容,不直接輸出返回結(jié)果的設(shè)置方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-01-01
PHP遞歸遍歷多維數(shù)組實(shí)現(xiàn)無限分類的方法
這篇文章主要介紹了PHP遞歸遍歷多維數(shù)組實(shí)現(xiàn)無限分類的方法,涉及PHP遞歸操作遍歷數(shù)組的相關(guān)技巧,在聯(lián)動(dòng)菜單及父子欄目設(shè)計(jì)等方面非常具有實(shí)用價(jià)值,需要的朋友可以參考下2016-05-05

