I created four files under localhost {code ...} the code in the file is the same {code ...} I thought that after all the pages are opened, refresh them repeatedly until all the cookies are set up, and the results should be the same. But actually not {code...} clearly shows that php's $ __cookie... I created four files under localhost.
/1.php/a/1.php/test/1.php/test/a/1.php
The code in the file is the same
I thought that after all the pages are opened, refresh them repeatedly until all the cookies are set up, and the results should be the same.
But not actually
/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" }
It is obvious that the COOKIE obtained by php $ __cookie is the cookie set in the range from the file level to the root directory. The cookies set for files in deeper layers and files in different directories cannot be obtained.
I want to ask
Assume that the/test/1.php file needs to obtain the cookie set in the/test/a/1.php file. How can this problem be achieved?
What is the principle of this phenomenon?
Reply content:I created four files under localhost.
/1.php/a/1.php/test/1.php/test/a/1.php
The code in the file is the same
I thought that after all the pages are opened, refresh them repeatedly until all the cookies are set up, and the results should be the same.
But not actually
/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" }
It is obvious that the COOKIE obtained by php $ __cookie is the cookie set in the range from the file level to the root directory. The cookies set for files in deeper layers and files in different directories cannot be obtained.
I want to ask
Assume that the/test/1.php file needs to obtain the cookie set in the/test/a/1.php file. How can this problem be achieved?
What is the principle of this phenomenon?
Go to the setcookie documentation.
The third parameter path of setcookie can define the directory in which the cookie takes effect. Setting path to/indicates that the cookie can be shared under the domain name.
This prevents cookie variable conflicts and is more secure.