Mike
Mike

Reputation: 11

How to store a cookie with php

How can a save a cookie using php for every visitor on my site? My website also has a unique ID generator. I want to know how to make the id stay the same using cookies

Upvotes: 1

Views: 1174

Answers (2)

Ed Holloway-George
Ed Holloway-George

Reputation: 5149


You could check W3Schools' PHP guide, it has a lot of useful code snippets to do with cookies.
Depending what you are doing, you may also want to check out Sessions.

Upvotes: -1

fin1te
fin1te

Reputation: 4351

You can use the setcookie() function to store a cookie.

Example Code

//Check if cookie exists
if (isset($_COOKIE['uid'])) {
    //Get UID from cookie
    $uid = $_COOKIE['uid'];
} else {
    //Generate UID
    $uid = uniqid();
    //Store in cookie
    setcookie('uid',$uid);
}

Upvotes: 8

Related Questions