kmimage
kmimage

Reputation: 73

how do I convert a cookie into a variable in php?

Let's say the user logs in, a cookie is sent and the user is redirected to another page.

How do I make it where I can convert the cookie value into a variable in PHP (on the page they are redirected to)?

I want to write the cookie value to a .txt file.

Thanks.

Upvotes: 0

Views: 726

Answers (7)

djayii
djayii

Reputation: 79

Cookies stored in the $_COOKIE array. So if you want to use it, just assign an appropriate cookie key value to your variable, for example:

$name = $_COOKIE['name'];

Upvotes: 1

SpeedBirdNine
SpeedBirdNine

Reputation: 4676

Cookie is available in $_COOKIE as $_COOKIE['key']; get that value and store it in a variable, which can be output to a file.

Upvotes: 0

dan
dan

Reputation: 856

Use this:

$newvar = $_COOKIE['cookiename'];

Upvotes: 0

Rupesh Pawar
Rupesh Pawar

Reputation: 1917

You don't need to convert them just retrieve into a php variable.

<?php
if(isset($_COOKIE['lastVisit']))
    $visit = $_COOKIE['lastVisit']; 
else
    echo "You've got some stale cookies!";

echo "Your last visit was - ". $visit;
?>

see here tutorial

Upvotes: 3

mario
mario

Reputation: 145512

Well cookies live in the $_COOKIE global. (When received.)

And writing them to a file would be as boring as:

 file_put_contents("var/cookie.txt", $_COOKIE["cookiename"]);

Upvotes: 3

GoalieGuy6
GoalieGuy6

Reputation: 522

Cookies can be accessed in the global $_COOKIE array.

Upvotes: 0

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 799450

From the docs:

Any cookies sent to you from the client will automatically be included into a $_COOKIE auto-global array if variables_order contains "C".

Upvotes: 5

Related Questions