PHP 5.5.9版本中COOKIE的奇怪現象

來源:互聯網
上載者:User
我在localhost下建了四個檔案

/1.php/a/1.php/test/1.php/test/a/1.php

檔案中的代碼都是一樣的

我本以為,所有頁面開啟後,反覆重新整理,直到所有的cookie都設定完,結果應該是一樣的。
但實際上不是

/1.php array(1) { ["/var/www/html"]=> string(13) "/var/www/html" } /a/1.php array(2) { ["/var/www/html/a"]=> string(15) "/var/www/html/a" ["/var/www/html"]=> string(13) "/var/www/html" } /test/1.php array(2) { ["/var/www/html/test"]=> string(18) "/var/www/html/test" ["/var/www/html"]=> string(13) "/var/www/html" } /test/a/1.php array(3) { ["/var/www/html/test/a"]=> string(20) "/var/www/html/test/a" ["/var/www/html/test"]=> string(18) "/var/www/html/test" ["/var/www/html"]=> string(13) "/var/www/html" } 

明顯看出 php的$__COOKIE 擷取的cookie是檔案所在層級到根目錄,這個範圍中設定的cookie。而更深層的檔案設定的cookie和平級但不同目錄的檔案設定的cookie,是擷取不到的。
我想問
假設 /test/1.php 檔案要擷取/test/a/1.php 檔案設定的cookie,怎麼做到呢?
這種現象出現的原理是什麼呢?

回複內容:

我在localhost下建了四個檔案

/1.php/a/1.php/test/1.php/test/a/1.php

檔案中的代碼都是一樣的

我本以為,所有頁面開啟後,反覆重新整理,直到所有的cookie都設定完,結果應該是一樣的。
但實際上不是

/1.php array(1) { ["/var/www/html"]=> string(13) "/var/www/html" } /a/1.php array(2) { ["/var/www/html/a"]=> string(15) "/var/www/html/a" ["/var/www/html"]=> string(13) "/var/www/html" } /test/1.php array(2) { ["/var/www/html/test"]=> string(18) "/var/www/html/test" ["/var/www/html"]=> string(13) "/var/www/html" } /test/a/1.php array(3) { ["/var/www/html/test/a"]=> string(20) "/var/www/html/test/a" ["/var/www/html/test"]=> string(18) "/var/www/html/test" ["/var/www/html"]=> string(13) "/var/www/html" } 

明顯看出 php的$__COOKIE 擷取的cookie是檔案所在層級到根目錄,這個範圍中設定的cookie。而更深層的檔案設定的cookie和平級但不同目錄的檔案設定的cookie,是擷取不到的。
我想問
假設 /test/1.php 檔案要擷取/test/a/1.php 檔案設定的cookie,怎麼做到呢?
這種現象出現的原理是什麼呢?

你去看下setcookie的文檔。
setcookie的第三個參數path可以定義cookie生效的目錄。 把path設定成/, 就說明cookie可以在網域名稱下公用。

這種現象防止cookie變數衝突,也更安全。

  • 相關文章

    聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.