久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

檢查 PHP cookie 是否存在,如果不存在則設(shè)置其值

Check if a PHP cookie exists and if not set its value(檢查 PHP cookie 是否存在,如果不存在則設(shè)置其值)
本文介紹了檢查 PHP cookie 是否存在,如果不存在則設(shè)置其值的處理方法,對大家解決問題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

問題描述

我在一個(gè)多語言網(wǎng)站上工作,所以我嘗試了這種方法:

I am working on a multilingual site so I tried this approach:

echo $_COOKIE["lg"];
if (!isset($_COOKIE["lg"]))
    setcookie("lg", "ro");
echo $_COOKIE["lg"];

這個(gè)想法是,如果客戶端沒有 lg cookie(因此,這是他們第一次訪問該站點(diǎn)),則設(shè)置一個(gè) cookie lg = ro 用于該用戶.

The idea is that if the client doesn't have an lg cookie (it is, therefore, the first time they've visited this site) then set a cookie lg = ro for that user.

一切正常,只是如果我第一次進(jìn)入這個(gè)頁面,第一個(gè)和第二個(gè) echo 什么都不返回.只有當(dāng)我刷新頁面時(shí)才會(huì)設(shè)置 cookie,然后 echo 都會(huì)打印我期望的ro"字符串.

Everything works fine except that if I enter this page for the first time, the first and second echo return nothing. Only if I refresh the page is the cookie set and then both echo print the "ro" string I am expecting.

如何設(shè)置此 cookie 以便在用戶第一次訪問/頁面加載時(shí)從第二個(gè) echo 中查看其值?應(yīng)該不需要刷新頁面或創(chuàng)建重定向.

How can I set this cookie in order to see its value from the second echo on the first visit/page load of the user? Should be without needing to refresh the page or create a redirect.

推薦答案

答案

你不能根據(jù)PHP手冊:

設(shè)置 cookie 后,即可在下一頁訪問它們加載 $_COOKIE 或 $HTTP_COOKIE_VARS 數(shù)組.

Once the cookies have been set, they can be accessed on the next page load with the $_COOKIE or $HTTP_COOKIE_VARS arrays.

這是因?yàn)?cookie 是在響應(yīng)標(biāo)頭中發(fā)送到瀏覽器的,然后瀏覽器必須將它們與下一個(gè)請求一起發(fā)送回去.這就是它們僅在第二次頁面加載時(shí)可用的原因.

This is because cookies are sent in response headers to the browser and the browser must then send them back with the next request. This is why they are only available on the second page load.

但是你也可以通過在調(diào)用 setcookie() 時(shí)設(shè)置 $_COOKIE 來解決這個(gè)問題:

But you can work around it by also setting $_COOKIE when you call setcookie():

if(!isset($_COOKIE['lg'])) {
    setcookie('lg', 'ro');
    $_COOKIE['lg'] = 'ro';
}
echo $_COOKIE['lg'];

這篇關(guān)于檢查 PHP cookie 是否存在,如果不存在則設(shè)置其值的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

Joining 2 tables in SELECT(MYSQL/PHP)(在 SELECT(MYSQL/PHP) 中加入 2 個(gè)表)
How to make lt;option selected=quot;selectedquot;gt; set by MySQL and PHP?(如何使lt;option selected=“selectedgt;由 MySQL 和 PHP 設(shè)置?)
Auto populate a select box using an array in PHP(使用 PHP 中的數(shù)組自動(dòng)填充選擇框)
PHP SQL SELECT where like search item with multiple words(PHP SQL SELECT where like search item with multiple words)
json_encode produce JSON_ERROR_UTF8 from MSSQL-SELECT(json_encode 從 MSSQL-SELECT 產(chǎn)生 JSON_ERROR_UTF8)
MySQL ORDER BY rand(), name ASC(MySQL ORDER BY rand(),名稱 ASC)
主站蜘蛛池模板: 久久成人亚洲 | 国产精品视频网 | av片免费观看 | 欧美一区二区三区在线看 | 久久一二| av手机免费在线观看 | 日本成人片在线观看 | 精品中文在线 | 日韩欧美一级精品久久 | 日韩在线中文字幕 | 91精品国产91久久久久久吃药 | 中文字幕 在线观看 | 亚洲精品久| 国内精品久久久久 | 日韩免费高清视频 | 国产a级毛毛片 | 国产成人免费视频网站视频社区 | 四虎永久免费影院 | 在线a视频网站 | 亚洲精品久久久一区二区三区 | 亚洲一二三区精品 | 欧美日韩国产精品激情在线播放 | 99reav| 成人日批视频 | 国产精品国产成人国产三级 | 国产精品久久久久久婷婷天堂 | 在线播放中文字幕 | 欧美性区| 亚洲午夜视频在线观看 | 免费在线看a | www.99热.com | 国产一区二区精品在线观看 | 日韩www视频 | 黄色一级大片在线免费看产 | 天天操夜夜骑 | 日韩精品久久久久 | 国产精品久久国产精品 | 97在线超碰| 麻豆av电影网| 日韩一区二区三区视频 | www.色综合|