Jump to content

Using Cookies in PHP

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
13 replies to this topic

#1
Vswe

Vswe

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 9,552 posts
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:

setcookie(name, value, expire, path, domain);

  • Name - The name of the cookie, you use this to receive the cookie later.
  • Value - The value that you'll store in the cookie.
  • Expire - When the cookie will expire.
  • Path - The path on the server where the cookie is available on, leaving this empty will allow whole domain to access it.
  • 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:

$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:

$_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:

$_COOKIE["Cookie_Name"];




Here comes a simple example on reading a cookie:


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
Guest_Jordan_*

Guest_Jordan_*
  • Guests
Nicely done, +rep!

#3
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
Nice +rep
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#4
Hilary

Hilary

    Newbie

  • Members
  • PipPip
  • 19 posts
Nicely done. Thank you; you make it look so simple :)

#5
technica

technica

    Learning Programmer

  • Members
  • PipPipPip
  • 63 posts
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.

#6
James.H

James.H

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 866 posts
Good guide, +rep!

#7
Orjan

Orjan

    Writes binary right handed and hex left handed

  • Moderators
  • 3,299 posts

technica said:

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

#8
PGP_Protector

PGP_Protector

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 253 posts

technica said:

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.

#9
nemoku

nemoku

    Newbie

  • Members
  • PipPip
  • 12 posts
is that possible, maybe you can even not only use it but you can edit it?

#10
PGP_Protector

PGP_Protector

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 253 posts

nemoku said:

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.

Posted Image
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.

#11
Vswe

Vswe

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 9,552 posts

PGP_Protector said:

Posted Image
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.

One will get a random verse the first time, isn't it supposed to assume it should start on the first?

#12
PGP_Protector

PGP_Protector

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 253 posts

Vswe said:

One will get a random verse the first time, isn't it supposed to assume it should start on the first?

Problem is I've not figured out how to see if they're seeing it for the first time & not accepting cookies or just seeing it for the first time.

I could track IP addresses, but figured that might be too creepy :D
(That or just track how many times an IP address has seen it)