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

Recommended Answers

All 6 Replies

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

Member Avatar for diafol

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.

commented: Good eye. +3

@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

:) thank you very much
@diafol and @matrixdevuk
so kind of u.......

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.