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

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

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

問題描述

我在一個多語言網站上工作,所以我嘗試了這種方法:

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"];

這個想法是,如果客戶端沒有 lg cookie(因此,這是他們第一次訪問該站點),則設置一個 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.

一切正常,只是如果我第一次進入這個頁面,第一個和第二個 echo 什么都不返回.只有當我刷新頁面時才會設置 cookie,然后 echo 都會打印我期望的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.

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

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.

推薦答案

答案

你不能根據PHP手冊:

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

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

這是因為 cookie 是在響應標頭中發送到瀏覽器的,然后瀏覽器必須將它們與下一個請求一起發送回去.這就是它們僅在第二次頁面加載時可用的原因.

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.

但是你也可以通過在調用 setcookie() 時設置 $_COOKIE 來解決這個問題:

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'];

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

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

相關文檔推薦

Joining 2 tables in SELECT(MYSQL/PHP)(在 SELECT(MYSQL/PHP) 中加入 2 個表)
How to make lt;option selected=quot;selectedquot;gt; set by MySQL and PHP?(如何使lt;option selected=“selectedgt;由 MySQL 和 PHP 設置?)
Auto populate a select box using an array in PHP(使用 PHP 中的數組自動填充選擇框)
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 產生 JSON_ERROR_UTF8)
MySQL ORDER BY rand(), name ASC(MySQL ORDER BY rand(),名稱 ASC)
主站蜘蛛池模板: 在线日韩 | 色婷婷激情综合 | www国产亚洲精品 | 91精品久久久 | 99热这里有精品 | 在线观看亚洲 | 亚洲视频在线观看一区二区三区 | 特黄特色大片免费视频观看 | 欧美日韩a| 超级黄色一级片 | 欧美激情久久久 | 欧美日韩久久久久 | 在线观看精品视频网站 | 久久久久久中文字幕 | 91久久精品一区二区三区 | 亚洲一区中文字幕在线观看 | 91网在线播放| 欧美乱人伦视频 | 日韩中文字幕 | 欧美日韩一区二区在线观看 | 亚洲综合小视频 | 国产精品久久久久久久久久久久午夜片 | 91综合网 | 亚洲一区二区在线免费观看 | 国产精品久久在线观看 | 在线观看视频91 | 国产丝袜一区二区三区免费视频 | 国产视频91在线 | 精品国产精品国产偷麻豆 | 91久久精品一区二区二区 | 天天艹| 国精日本亚洲欧州国产中文久久 | 九九爱这里只有精品 | 精品欧美乱码久久久久久 | 国产欧美日韩一区 | 中文字幕一区二区三区四区 | 97精品国产97久久久久久免费 | 国产免费拔擦拔擦8x高清 | 国产二区精品视频 | 国产精品成人一区二区三区夜夜夜 | 视频一区在线播放 |