Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get a list of browser cookies using PHP?

Tags:

php

From my understanding $_COOKIE gives me all the cookies in a user's browser.

But when I do

var_dump($_COOKIE);

I get the following:

Array
    (
        [wp-settings-1] => m8=o&m2=o&m5=o&m4=o&m3=o&m1=o&m7=o&m0=o
        [wp-settings-time-1] => 1329859655
        [PHPSESSID] => ST88bLB7PE5S9BbY5oXxLgDIcas
    )

This list does not match the list I see in the browser. For example, PHPSESSID is not in the list of cookies in my browser. Am I accessing the wrong variable?

Thanks.

like image 293
rexposadas Avatar asked Feb 22 '12 21:02

rexposadas


People also ask

How can I get browser cookies in PHP?

Accessing Cookies with PHP Simplest way is to use either $_COOKIE or $HTTP_COOKIE_VARS variables. Following example will access all the cookies set in above example. You can use isset() function to check if a cookie is set or not.

What PHP function is used to read cookies?

You can use the print_r or var_dump function to check all available cookies for debugging purposes. print_r( $_COOKIE ); It's that easy to read cookies in PHP!

Can PHP set cookies?

With PHP, you can both create and retrieve cookie values. The name of the cookie is automatically assigned to a variable of the same name. For example, if a cookie was sent with the name "user", a variable is automatically created called $user, containing the cookie value.

What is $_ cookies?

A cookie is a piece of data from a website that is stored within a web browser that the website can retrieve at a later time. Cookies are used to tell the server that users have returned to a particular website.


1 Answers

As mentioned $_COOKIE only gives you cookies for the current domain/path. You cannot see all cookies in a browser. You can see how this works if you ever sniff/view HTTP packets. When you make an HTTP request, the browser sends cookies to the server. This page has some excellent examples of what that HTTP traffic looks like.

like image 147
Rob Avatar answered Oct 12 '22 08:10

Rob