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

用JS設置cookie,用PHP讀取問題

Set cookie wih JS, read with PHP problem(用JS設置cookie,用PHP讀取問題)
本文介紹了用JS設置cookie,用PHP讀取問題的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我正在嘗試使用 javascript 設置 cookie,并使用 php 在另一個頁面中讀取它.我可以通過做來寫 cookie

document.cookie = cookieName+"="+cookieValue;

我部分工作.- cookie 已寫入,我可以使用 $_COOKIE[cookieName] 讀取它,但只能在同一網頁中讀取.

這真的不太有用.我需要在另一頁閱讀它.我通常在 asp.net 和 c# 中開發,所以我對 php 很陌生.我做錯了什么嗎?

感謝您的寶貴時間!

編輯 1:兩個頁面都在同一個域中......例如.site.com/index.php -> site.com/index2.php

cookie 通過以下方式設置在一頁中:

function SetCookie(cookieName,cookieValue,nDays) {今天無功 = 新日期();var expire = new Date();if (nDays==null || nDays==0) nDays=1;expire.setTime(today.getTime() + 3600000*24*nDays);document.cookie = cookieName+"="+escape(cookieValue)+ ";expires="+expire.toGMTString();}

在另一個頁面中它無法訪問,但在同一頁面中它可以......

我嘗試設置域并添加 path=<?php echo $_SERVER['HTTP_HOST'];?> 到 javascript 代碼...仍然什么都沒有..

到目前為止我有..

document.cookie = cookieName+"="+escape(cookieValue)+"; expires="+expire.toGMTString()+"; path=/"+"; domain=.<?php echo $_SERVER['HTTP_HOST']; ?>";

我仍然只能從同一頁面讀取 cookie..

哦..我的..天啊...一直是一個錯字...只需要刪除path=/"+;dom..."我為自己感到羞恥馬上...與此同時,我也重置了我的 cookie,所以 Jared 現在不幸地不能接受你的帖子作為 anwser...我給我的名字帶來了恥辱!!!....

解決方案

在此處閱讀有關設置 Javascript cookie,特別是路徑和域訪問的信息:

http://www.quirksmode.org/js/cookies.html

我認為正在發生的事情是兩件事之一:

  1. 您不是從同一個域/子域訪問 cookie,和/或
  2. 另一個頁面不是 cookie 指定的路徑的一部分.

因此,您的 cookie 沒有向瀏覽器提供相關信息,以便它可以跨子域和/或目錄路徑訪問.

document.cookie = 'ppkcookie1=testcookie;expires=2001 年 8 月 2 日星期四 20:47:11 UTC;路徑=/;;域=.example.com'

注意,.example.com 只是一個示例域(您需要在那里使用您的域),除了初始 . 之外,您不需要通配符遍歷所有子域.并且您需要生成一個 expires= 日期.來自 QuirksMode:

function createCookie(name,value,days) {如果(天){var date = new Date();date.setTime(date.getTime()+(days*24*60*60*1000));var expires = "; expires="+date.toGMTString();} 別的 {var expires = "";}document.cookie = name+"="+value+expires+"; path=/; domain=.example.com";}

我在 QuirksMode 的函數中添加了 domain= 位.

編輯(以下示例最初引用了我個人網站上的頁面.)

Andrej,這對我來說非常好:

http://example.com/test.php

function createCookie(name,value,days) {如果(天){var date = new Date();date.setTime(date.getTime()+(days*24*60*60*1000));var expires = "; expires="+date.toGMTString();}else var expires = "";document.cookie = name+"="+value+expires+"; path=/; domain=.example.com";}createCookie('cookieee','stuff','22');

http://example.com/test/test.php

<?php打印_r($_COOKIE);?>

$_COOKIE 的打印輸出將顯示 cookie.請注意,當我檢查 cookie 時,.example.com 被正確設置為域.

I'm trying to set a cookie with javascript and read it in an other page with php. I am able to write the cookie by doing

document.cookie = cookieName+"="+cookieValue;

and i partially works. - The cookie is written, and I am able to read it with $_COOKIE[cookieName] but ONLY in the same web page.

Which is not quite usefull really. I need to read it in another page. I usually develop in asp.net and c#, so I'm preety new to php. Am I doing something wrong?

Thank you for your time!

EDIT1: both pages are in the same domain.. eg. site.com/index.php -> site.com/index2.php

EDIT2: the cookie is set in one page through:

function SetCookie(cookieName,cookieValue,nDays) {
 var today = new Date();
 var expire = new Date();
 if (nDays==null || nDays==0) nDays=1;
 expire.setTime(today.getTime() + 3600000*24*nDays);
 document.cookie = cookieName+"="+escape(cookieValue)
                 + ";expires="+expire.toGMTString();
}

and in another page it can not be accessed, but in that same page it can...

EDIT3: i tried setting the domain and added path=<?php echo $_SERVER['HTTP_HOST']; ?> to the javascript code... still nothing..

EDIT4: so far I have..

document.cookie = cookieName+"="+escape(cookieValue)+"; expires="+expire.toGMTString()+"; path=/"+"; domain=.<?php echo $_SERVER['HTTP_HOST']; ?>";

and still i can read the cookie ONLY from the same page..

EDIT5: oh.. my .. god... it was a typo all along... just needed to remove the" path=/"+"; dom..." i am so ashamed of myself right about now... in the meantime i also reset my cookies, so Jared now i unfortuneatly can't accept your post as anwser... i brought shame upon my name!!!....

解決方案

Read on setting Javascript cookies and in particular path and domain access here:

http://www.quirksmode.org/js/cookies.html

I think what is happening is one of two things:

  1. You aren't accessing the cookie from the same domain/subdomain, and/or
  2. the other page is not part of the path the cookie has specified.

So your cookie is not giving the relevant information to the browser for it to be accessible across subdomains and/or the directory path.

document.cookie = 'ppkcookie1=testcookie; expires=Thu, 2 Aug 2001 20:47:11 UTC; path=/; ;domain=.example.com'

Note, .example.com is just an example domain (you need yours in there), and you do not need a wildcard other than the initial . for it go across all subdomains. And you need to generate an expires= date. From QuirksMode:

function createCookie(name,value,days) {
    if (days) {
        var date = new Date();
        date.setTime(date.getTime()+(days*24*60*60*1000));
        var expires = "; expires="+date.toGMTString();
    } else {
        var expires = "";
    }
    document.cookie = name+"="+value+expires+"; path=/; domain=.example.com";
}

I added the domain= bit to QuirksMode's function.

EDIT (The example below originally referenced pages on my personal website.)

Andrej, this works perfectly fine for me:

http://example.com/test.php

function createCookie(name,value,days) {
    if (days) {
        var date = new Date();
        date.setTime(date.getTime()+(days*24*60*60*1000));
        var expires = "; expires="+date.toGMTString();
    }
    else var expires = "";
    document.cookie = name+"="+value+expires+"; path=/; domain=.example.com";
}

createCookie('cookieee','stuff','22');

http://example.com/test/test.php

<pre>
<?php 

print_r($_COOKIE);

?>

And the printout of $_COOKIE will show the cookie. Note I when I inspect the cookie the .example.com is set correctly as the domain.

這篇關于用JS設置cookie,用PHP讀取問題的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持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)
主站蜘蛛池模板: 天堂综合网久久 | 日韩精品在线免费观看视频 | 一区二区三区福利视频 | 久久精品国产一区二区三区不卡 | 欧美日韩精品中文字幕 | 欧美1区2区 | 欧美日韩成人在线观看 | 国产精品久久久久久久久免费高清 | 国户精品久久久久久久久久久不卡 | 夜夜夜夜夜夜曰天天天 | 欧美日高清视频 | 午夜影院黄 | 欧美日韩国产一区二区三区 | 一级做a爰片性色毛片视频停止 | 天天综合国产 | 国产精品久久久久久久久久妞妞 | 国户精品久久久久久久久久久不卡 | 天天操天天玩 | 无人区国产成人久久三区 | 日韩黄 | 婷婷福利| 欧美日韩国产精品一区 | 欧美国产中文字幕 | 精品久久久久久亚洲综合网站 | 国产电影一区二区 | 久久一区二区视频 | 久草在线 | 视频二区 | 亚洲一区久久 | 亚洲精品乱码久久久久久按摩观 | 亚洲精品国产综合区久久久久久久 | 亚洲精品一区二区三区中文字幕 | 中文字幕一区二区三区四区五区 | 97国产超碰| 久热中文字幕 | www.青娱乐| 成人免费日韩 | 99视频免费在线 | 毛片片 | 国产免费色 | 亚洲一区在线播放 |