Set Cookies in PHP

Hello Friends !!

Today i will show you hiw to use Cookie in PHP.

First of all Cookies are small pieces of text, stored by a user’s web browser, that contain the user’s settings, shopping cart contents, or other data used by websites.

In PHP we can set cookies by inbuilt function setcookie().

setcookie(‘username’, $name);

It is very easy to use and understand. First parameter is the name of the cookie and second one is the value you want to store in cookie.

By above code you can set simple cookie and this cookie will be deleted when user close their browser.

We can also set the time how long we want the cookie.

$cookie_life = time() + 31536000;
setcookie(‘username’, $name, $cookie_life);

Above example create cookie for 1 year. Yes 31536000 seconds means 1 year.Remember, cookies must be deleted with the same parameters they were set with.If users clear their history from browser then cookies will be deleted.

We can also use a forth parameter, the fourth parameter is the cookie path.

$cookie_life = time() + 31536000;
$path = ‘/folder/’;
setcookie(‘username’, $name, $cookie_life, $path);

The fourth parameter defines a path from which the cookie can be accessed. In the above example only scripts inside /folder/ will be able to access the cookie ($username), in scripts outside this folder the cookie will be unavailable.

There’s also a fifth parameter, the fifth parameter defines a hostname that can retrieve the cookie.

$cookie_life = time() + 31536000;
$path = ‘/folder/’;
$hostname = ‘your host address’;
setcookie(‘username’, $name, $cookie_life, $path, $hostname);

The fifth parameter is the domain which can retrieve this cookie. Normally a cookie can only be accessed by the domain that set it, otherwise sites would be able to get private information from cookies set by other sites. With this parameter we can define another domain which can retrieve the cookie we are setting.

Click on below links for more:

Now How to Retrive Cookie ?

Once you have set a cookie on a user’s system, it is only avialable when they load the next page. The cookie can be accessed very easily:

// Our cookie would be contained in $HTTP_COOKIE_VARS[‘username’]
// or $_COOKIE[‘username’]

When the cookie is set the value is automatically URL encoded, when the script retrieves a cookie, it automatically decodes the value, so you don’t have to worry about that.

Always remember, a limitation of cookies is that you have to set it before you can send the HTTP headers. In other words, always do all your cookie setting BEFORE you output any HTML, otherwise the cookie will not be set and your page will have an ugly error stuck on it.

  • Pingback: Set Cookies in PHP | Dailytuts.net - Daily tutorial for peoples()

  • http://debianusers.or.kr/ westporch

    Hello, admin.
    I’m korea student. major in computer
    Explanation of Setcookies function is very good. 🙂

  • http://debianusers.or.kr westporch

    Hello, admin.
    I’m korea student. major in computer
    Explanation of Setcookies function is very good. 🙂

  • admin

    Thanks westporch for comment..
    I you have knowledge about any php topic then you can share via phpgenious.com.

    Thanks..

  • admin

    Thanks westporch for comment..
    I you have knowledge about any php topic then you can share via phpgenious.com.

    Thanks..

  • http://www.marplo.net/ OnLine

    Hello
    How can I set a cookies that expires at the end of the day?
    Thanks

  • http://www.marplo.net OnLine

    Hello
    How can I set a cookies that expires at the end of the day?
    Thanks

  • admin

    Hello,

    If you want to set a cookies which expires at the end of the day, you have just write below code :

    $cookie_life = time() + 86400;
    setcookie(‘username’, $name, $cookie_life);

    Here 86400 is the seconds of 1 day(24 hours).

    If you have any query about this, please comment on this.

    Thanks.

  • admin

    Hello,

    If you want to set a cookies which expires at the end of the day, you have just write below code :

    $cookie_life = time() + 86400;
    setcookie(‘username’, $name, $cookie_life);

    Here 86400 is the seconds of 1 day(24 hours).

    If you have any query about this, please comment on this.

    Thanks.

  • Zwubber

    That’s a cookie which lives for one whole day, but what OnLine needs is a cookie that will expires at the end of the day(23:59). Is that an easy thing to do?

  • http://deepdiscuz.com Obasamu

    nice tutorial

  • Dsfsdf

    sadsd

  • Passerby

    Thanks for the help – but please don’t use an online translator for your site – have someone who knows English well take a look – you’ve got stuff like ‘genious’ instead of genius 😉 as well as ‘retrive’ instead of retrieve. The info is good but the language issues make it look unprofessional and I’m sure you didn’t intend that. Please see this as constructive critique … Again thanks!