hello, everyone im working on my masters thesis, i need little help. if anyone can help me with cookies methods. i want to implement cookies in my website through PHP, im nt that much expert in php.how can i do that ??? please

2 Years
Discussion Span
Last Post by soomro_moon

You could use cookies but you will need Javascript to achieve it because "cookies" are client side, not the server side. You will have to learn Javascript first. To incorporate with PHP, simply echo the script out to the HTML page.


Here's an example:

$hours = 1;
$cookie = [
    "name" => "myCookie",
    "value" => 123,
    "expire" => time()+(3600*$hours) // Set the cookie expiry to 1 hour (3600 seconds) * $hours
setcookie($cookie['name'], $cookie['value'], $cookie['expire']);

// Check if it was created
if (isset($_COOKIE[$cookie['name']))
    echo $cookie['name'] . " is set.";

// Get it from another script
$user_id = $_COOKIE['myCookie']; // $user_id is now 123 (or whatever your cookie's value is)

code untested but should be valid; wrote it within the DaniWeb editor. if you have any questions just ask me


You're missing a ] in the conditional statement in line 10
This script will return an error due to cookie not set on line 16. It should work on page reload though.

Votes + Comments
Good eye.

@diafol — good spotting!
Also, I know that line 16 won't work instantly if they put it there. Hence the "// Get it from another script". ;D

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.