+ Reply to Thread
Page 1 of 2 12 LastLast
Results 1 to 10 of 14

Thread: Using Cookies in PHP

  1. #1
    Join Date
    Apr 2009
    Location
    Uppsala, Sweden
    Posts
    9,547
    Blog Entries
    5
    Rep Power
    98

    Using Cookies in PHP

    Cookies is small text files saved locally on the user's computer by the website, when the website later on wants the information it just reads the cookie on the user's computer. An example on what you can use cookies for is to see if the user has visited the site before, if it has then a cookie you saved the last time lies on the computer so therefor you can only check if it does.



    Creating a Cookie


    When creating a cookie you have to do it before sending anything to the page, so you can't have a html tag before it. To create a cookie you do like this:

    Code:
    setcookie(namevalueexpirepathdomain); 
    1. Name - The name of the cookie, you use this to receive the cookie later.
    2. Value - The value that you'll store in the cookie.
    3. Expire - When the cookie will expire.
    4. Path - The path on the server where the cookie is available on, leaving this empty will allow whole domain to access it.
    5. Domain - The domain which can access the cookie, leaving this empty will allow all your domains to access it.


    Here's an example on how to create a cookie:

    Code:
    $expire=time()+60*60*24*3;
    setcookie("Cookie_Name""Cookie_Value"$expire); 
    The above example will create a cookie called "Cookie_Name" with the value "Test_Value" which will expire after 3 days.







    Reading Cookies

    To read a cookie you use:

    Code:
    $_COOKIE["<Name of cookie>"]; 
    and replace <Name of cookie> with the name of the cookie you want to read.


    So if we want to read the cookie we created above we should do like this:

    Code:
    $_COOKIE["Cookie_Name"]; 



    Here comes a simple example on reading a cookie:


    Code:
    if (isset($_COOKIE["Cookie_Name"]))
    {
    $Value $_COOKIE["Cookie_Name"];
    $expire=time()+60*60*24*3;
    setcookie("Cookie_Name"$Value$expire);
    $Value "The Value of Cookie_Name is " $Value;
    }
    else
    {
    $Value "Cookie not set";
    }

    echo 
    $Value
    So first of all we check if the cookie exists, if it do we reads it. Then recreate the cookie with an expire time of 3 days, this is so each time the user visit the site we will update the expire date, then we set the value to be a message telling what the value is, and if the cookie didn't existed we stored "Cookie not set" in the variable called Value. At the the end we just echo the result.


    That was everything about how you're using cookies with PHP. I hope you found this tutorial useful.

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Location
    Advertising world
    Posts
    Many

     
  3. #2
    Jordan Guest

    Re: Using Cookies in PHP

    Nicely done, +rep!

  4. #3
    Join Date
    Jul 2006
    Posts
    16,491
    Blog Entries
    75
    Rep Power
    143

    Re: Using Cookies in PHP

    Nice +rep
    Programming is a branch of mathematics.
    My CodeCall Blog | My Personal Blog

  5. #4
    Hilary's Avatar
    Hilary is offline Newbie
    Join Date
    Apr 2008
    Location
    LA and Manchester
    Posts
    19
    Blog Entries
    1
    Rep Power
    0

    Re: Using Cookies in PHP

    Nicely done. Thank you; you make it look so simple

  6. #5
    technica's Avatar
    technica is offline Learning Programmer
    Join Date
    Oct 2009
    Posts
    64
    Rep Power
    0

    Re: Using Cookies in PHP

    The only use of cookies I know is for "Remember Me On this computer" check-box that we can see on some of websites.

    Good article with good explanation.

  7. #6
    Join Date
    Nov 2009
    Location
    London
    Posts
    866
    Blog Entries
    3
    Rep Power
    14

    Re: Using Cookies in PHP

    Good guide, +rep!

  8. #7
    Join Date
    Sep 2007
    Location
    Karlstad, Sweden
    Posts
    3,082
    Blog Entries
    7
    Rep Power
    42

    Re: Using Cookies in PHP

    Quote Originally Posted by technica View Post
    The only use of cookies I know is for "Remember Me On this computer" check-box that we can see on some of websites.

    Good article with good explanation.
    There is another, even bigger use for cookies, and that is to keep sessions live. one alternative is via url, but many uses a cookie for this information. Such cookies usually is just an 32 digit long hexstring as string name and another 32 digit long hexstring as value...
    __________________________________________
    I study Information Systems at Karlstad University when I'm not on CodeCall

  9. #8
    PGP_Protector's Avatar
    PGP_Protector is offline Programming Professional
    Join Date
    Jun 2009
    Posts
    253
    Rep Power
    11

    Re: Using Cookies in PHP

    Quote Originally Posted by technica View Post
    The only use of cookies I know is for "Remember Me On this computer" check-box that we can see on some of websites.

    Good article with good explanation.
    I've got a PHP Sig Image on another site I visit that uses cookies.
    It keeps track of how many times you've viewed the Image, and allows it to pull the relevant data from the database based on the number of views.

  10. #9
    nemoku is offline Newbie
    Join Date
    Jan 2010
    Posts
    12
    Rep Power
    0

    Re: Using Cookies in PHP

    is that possible, maybe you can even not only use it but you can edit it?

  11. #10
    PGP_Protector's Avatar
    PGP_Protector is offline Programming Professional
    Join Date
    Jun 2009
    Posts
    253
    Rep Power
    11

    Re: Using Cookies in PHP

    Quote Originally Posted by nemoku View Post
    is that possible, maybe you can even not only use it but you can edit it?
    ??
    If you asking if the site that saves the cookie can edit it, yes.
    Or you can edit the cookie file yourself.

    Example.


    Will create a cookie if you allow it to, then display verse 1.
    The next time it is called, it attempts to read the cookie stored on your computer
    Increments a counter, displays the next verse, and then saves the new verse location overwriting the old cookie (editing the file)

    If 3rd party cookies are blocked by your browser (My cookie would be a 3rd party cookie as CodeCall isn't giving it to you) it wouldn't be able to read the cookie, and then just give you a random verse.

+ Reply to Thread
Page 1 of 2 12 LastLast

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Creating Cookies
    By John in forum JavaScript and CSS
    Replies: 6
    Last Post: 04-18-2010, 01:33 AM
  2. Cookies
    By PGP_Protector in forum C# Programming
    Replies: 0
    Last Post: 11-23-2009, 02:17 PM
  3. other sites cookies
    By zeroradius in forum PHP Development
    Replies: 4
    Last Post: 07-14-2009, 06:50 AM
  4. Cookies gone?
    By Jaan in forum PHP Development
    Replies: 2
    Last Post: 02-18-2009, 02:32 PM
  5. Cookies
    By dirkfirst in forum HTML Programming
    Replies: 10
    Last Post: 01-06-2007, 02:11 AM

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