Results 1 to 5 of 5

Thread: How to Create a Cookie in PHP?

  1. #1
    Join Date
    Aug 2006
    Posts
    148

    How to Create a Cookie in PHP?

    Hi Friends,
    Since I am new in PHP, can someone explain me what is Cookie in PHP. Also can anyone tell me how to create a cookie in PHP? Please give me detailed information as I don't know anything about the cookie. I know the cookies that we use during the Internet Explorer. Also if possible provide me the coding in PHP for creating and deleting cookie.!!
    The Gaming Machine:
    Processor: Core 2 Duo E6400
    Motherboard: Asus P5W DH Deluxe
    RAM: 2GB XMS2 Corsair PC2-6400
    Video: ATI Radeon X1900XT 512MB w/ Arctic Accellero
    Drive: 74GB "WD Raptor" 10000RPM
    Sound: Creative SoundBlaster X-Fi
    Watercooling: *Soon* Zalman Reserator

  2. #2
    Join Date
    Aug 2006
    Posts
    235

    Re: How to Create a Cookie in PHP?

    I think that you know about the Cookie which gets created while using an Internet Explorer or any other browser. There is not much difference in between that Cookie and this one (its one and the same). A cookie is often used to identify a user. You can say that a cookie is a small file that the server embeds on the user's computer. When you requests a page with a browser, the computer will send the cookie along with the information. With PHP, you can both create and retrieve cookie values.
    3.2 (northwood)
    2gig ram
    ATI AIW X800xt 256mb
    Gigabyte GA-8knxp 875p Chipset
    Optiwrite 8X DVD Burner
    Win XP PRO Sp2 (Works Perfectly)
    2 SATA Raptor 74gig Raid 0
    2 7200 IDE 320gig HD

  3. #3
    Join Date
    Nov 2008
    Posts
    1,192

    Re: How to Create a Cookie in PHP?

    For setting a cookie you will have to use the setcookie() function. The setcookie() function must appear before the <html> tag. The Syntax for this function will be
    Code:
    setcookie(name, value, expire, path, domain);
    For you to understand better, I have provided an example that will create a cookie having cookie named "admin" and assign the value "Counter-T". I have also specified that the cookie should expire after one hour. Here is code for that :
    Code:
    <?php
    setcookie("admin", "Counter-T", time()+3600);
    ?>
    
    <html>
    .....

  4. #4
    Join Date
    Mar 2008
    Posts
    672

    Re: How to Create a Cookie in PHP?

    After looking at the example provided by the 'void'. I thought to provide an alternate coding for set the expiration time of the cookie in another way. It may be easier than using seconds. Because it will be difficult if you have to mention the 10 hours for expiry of cookie. Then you will have to calculate that first in seconds. So you can use this coding :
    Code:
    <?php
    $expire=time()+60*60*24*30;
    setcookie("admin", "Counter-T", $expire);
    ?>
    
    <html>
    .....

  5. #5
    Join Date
    May 2008
    Posts
    2,389

    Re: How to Create a Cookie in PHP?

    If you want to delete a cookie then you will have to make sure that the expiration date is in the past. Otherwise it will not delete. You can use the following code for deleting cookie :
    Code:
    <?php
    // set the expiration date to one hour ago
    setcookie("admin", "", time()-3600);
    ?>
    I have considered the above example for deleting the cookie. So the name and value are the same.

Similar Threads

  1. How to use ASP to maintain the cookie
    By Anirvinya in forum Software Development
    Replies: 5
    Last Post: 16-02-2010, 03:38 AM
  2. what is cookie in java?
    By Jabeen in forum Software Development
    Replies: 3
    Last Post: 21-11-2009, 02:53 PM
  3. How to create a cookie
    By KALLIYAN in forum Software Development
    Replies: 3
    Last Post: 30-10-2009, 05:40 PM
  4. LG Cookie applications
    By B e B o in forum Portable Devices
    Replies: 2
    Last Post: 21-09-2009, 12:52 PM
  5. What is a Cookie?
    By Dharmavira in forum Technology & Internet
    Replies: 5
    Last Post: 18-02-2009, 12:33 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,711,629,969.80373 seconds with 17 queries