how to retrieve a value of a cookie php

2019-08-08 03:30发布

问题:

I have wordpress installed and when a user logs in, it saves a cookie in the temporary files with the name of wordpress.

I want to retrieve the value of the cookie using php in another page (out of wordpress). What would be a method to retrieve a persistent cookie in php.

回答1:

I notice three key value pairs on my local worpress installation. If you can place the php file under the same domain, then you can obtain the cookies as it appears worpress appropriately sets the cookie to "/". So for instance the key "wp-settings-1".

cookie_test.php

if(isset($_COOKIE["wp-settings-1"]))
{
    echo "Cookie: {$_COOKIE['wp-settings-1']}";
}
else
{
    echo "<h1>No Cookie Detected from wordpress!</h1>";

}

It has to be under the same domain. Locally I just drop this anywhere in the worpress folder and it will work when you browse to it.



回答2:

Look in $_COOKIE.

However, keep in mind, that the path may be set different to where you want to read it.



回答3:

You keep saying that its under localhost. But the point is, the cookie data wont be available between subdirectories of localhost. like the cookie set at http://localhost/wordpress wont be available at http://localhost/drupal .. you can only access the cookies of http://localhost/wordpress in that directory or subdirectories of it e.g. http://localhost/wordpress/test-page ..



标签: php wordpress