Link to home
Start Free TrialLog in
Avatar of crescue
crescue

asked on

How can I read the value from a cookie previously set using php

How can I read the value from a cookie previously set using php. I have set the cookie using :

<?php
$fromUser = htmlspecialchars($_GET["uname"]);
$cookie_name = "user";
$cookie_value = $fromUser;
setcookie($cookie_name, $cookie_value, time() + (86400 * 30), "/");   //    86400 = 1 day
?>

Now I want to read the value of that cookie when I goto a different page, within the same domain.  

Tnx for your help
ASKER CERTIFIED SOLUTION
Avatar of Dave Baldwin
Dave Baldwin
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
To read a cookie back, you just use the cookie name as the key of the $_COOKIE array:

// get the user:
$name = $_COOKIE['user'];

You might want to check it exists before using it by using the isset() function:

$name = isset($_COOKIE['name']) ? $_COOKIE['name'] : null;
Avatar of crescue
crescue

ASKER

Tnx it worked and I learned something new