webdevRefinery Forum: Baking Cookies 101 - webdevRefinery Forum

Jump to content

  • 2 Pages +
  • 1
  • 2
  • You cannot start a new topic
  • You cannot reply to this topic

User is offline Comkid 

  • Group: Members
  • Posts: 87
  • Joined: 09-March 10
  • LocationWarez-BB
  • Expertise:PHP

Posted 19 March 2010 - 04:05 AM (#1)

Baking Cookies 101


In English: [Tutorial] Cookies
Hello,

I'm Comkid as you may know, anyway today I'm going to write a tutorial on Cookies. Anyway, let us begin.

Cookies have many uses, like storing information, storing items added to carts, staying logged in on a forum (like this one) and so on.

All cookies have a name, a value and an expiration date. These are the first three parameters of the setcookie function.

Example, Name - user, Value - rand & Expiration - 2 minutes from now:

<?php
setcookie('user', 'rand', time() + 120);
?>


Accessing cookies are simple, all you need to know is the name of the cookie.

Example (Corresponds with first example):

<?php
$cookie = $_COOKIE['user'];
echo $cookie;
?>


This will now output:

rand


Finally, we must learn how to delete cookies before their expiration date. This is fairly simple just requiring the name of the cookie.

Example (Corresponds with first example):

<?php
setcookie('user', '', time() - 3600);
?>


This makes the cookie 'user' expire an hour ago, which the browser will automatically delete.

Watch out for more of my tutorials ;)

Thanks,

Comkid

If you liked this tutorial: Check out my new tutorial which is still Work-in-Progress: [ Linkie ]
Ist php freelance coder :P

Comkid is trying to learn Javascript, AJAX and jQuery :D
- After two years, Comkid is not trying to learn anymore :(
1


User is offline Pavarok 

  • Group: Members
  • Posts: 11
  • Joined: 18-March 10
  • LocationAntwerp, Belgium

Posted 19 March 2010 - 06:50 AM (#2)

Whoah, thanks. Helps a lot.
0


User is offline Comkid 

  • Group: Members
  • Posts: 87
  • Joined: 09-March 10
  • LocationWarez-BB
  • Expertise:PHP

Posted 19 March 2010 - 07:08 AM (#3)

No problems, happy to help.
Ist php freelance coder :P

Comkid is trying to learn Javascript, AJAX and jQuery :D
- After two years, Comkid is not trying to learn anymore :(
0


User is offline joelixny 

  • Group: Members
  • Posts: 45
  • Joined: 08-March 10
  • Locationhave you checked your closet lately?
  • Expertise:Python

Posted 19 March 2010 - 10:31 AM (#4)

This is very useful, thanks!
This is not a signature. It was not written by HusH!. This is not an awesome webpage. This forum does not rock as much as a fox in a box with a devil's lock. [/sarcasm]
Posted Image That is not HusH! typing this.
0


User is offline Mack 

  • http://mackgoodstein.com/
  • Group: Members
  • Posts: 2072
  • Joined: 08-March 10
  • Expertise:HTML,CSS,PHP,Javascript

Posted 19 March 2010 - 03:35 PM (#5)

Rather than using setCookie(), I've been using Kyek's BakeCookie() function instead. He posted it in another thread, and I find it easier then the using php's built in setCookie
0


User is offline magik 

  • magikly delicious
  • Group: Members
  • Posts: 1095
  • Joined: 08-March 10
  • Expertise:HTML,PHP,Javascript,SQL

Posted 19 March 2010 - 05:41 PM (#6)

and eatCookie! NOM NOM NOM =þ
.... magik's most awesomely rainbowed signature =þ


the wdR forums, on crack rocks, said:

Your signature may contain:
  • Any number images
  • Images of any size
  • Any number of URLs
  • Any number of lines

dangerous...
0


User is offline Comkid 

  • Group: Members
  • Posts: 87
  • Joined: 09-March 10
  • LocationWarez-BB
  • Expertise:PHP

Posted 19 March 2010 - 11:05 PM (#7)

I've seen it, but I want to teach the base, so it should work on default php without having to look for functions ;)
Ist php freelance coder :P

Comkid is trying to learn Javascript, AJAX and jQuery :D
- After two years, Comkid is not trying to learn anymore :(
0


User is offline Mack 

  • http://mackgoodstein.com/
  • Group: Members
  • Posts: 2072
  • Joined: 08-March 10
  • Expertise:HTML,CSS,PHP,Javascript

Posted 20 March 2010 - 02:16 PM (#8)

View Postmagik, on 19 March 2010 - 05:41 PM, said:

and eatCookie! NOM NOM NOM =þ


well now, any baked cookie thats just sitting out gets eaten… but yeah, I use eatCookie too, makes your code looks so much nicer then if you use setCookie
0


User is offline arronhunt 

  • I'm a httpster
  • Group: Moderators
  • Posts: 3400
  • Joined: 09-March 10
  • LocationLos Angeles, CA
  • Expertise:HTML,CSS,Javascript,Graphics,Flash

Posted 21 March 2010 - 01:55 AM (#9)

 If ($_COOKIE['chocolate chip']=="baked"){
echo "NOM NOM NOM!";
$hunger=0;
}
else {
$hunger=100;
echo "I want my cookie..."; 
}

DO NOT OPEN THIS

Spoiler
0


User is offline Comkid 

  • Group: Members
  • Posts: 87
  • Joined: 09-March 10
  • LocationWarez-BB
  • Expertise:PHP

Posted 23 March 2010 - 05:55 AM (#10)

tsk, tsk, tsk

Here let me make that look better :P:

<?php
$hunger = 100
if ($_COOKIE['chocolate chip'] == "baked")
{
	echo "NOM NOM NOM!";
	$hunger = 0;
}
else
{
	$hunger = 110; //! Unbelievably Starving :P
	echo "I want my cookie...";
}
?>

Ist php freelance coder :P

Comkid is trying to learn Javascript, AJAX and jQuery :D
- After two years, Comkid is not trying to learn anymore :(
0


User is offline Kyek 

  • Founder of wdR
  • Group: Administrators
  • Posts: 5081
  • Joined: 20-February 10
  • LocationPhiladelphia, PA, USA
  • Expertise:HTML,CSS,PHP,Java,Javascript,Node.js,SQL

Posted 27 March 2010 - 09:26 AM (#11)

Great little tutorial :) Since Mack and magik referred to it, here are the functions I use in my apps. This is updated from the version I posted in another thread, as it uses a duration instead of a specific expiration date.
/**
 * Sets a cookie quickly and easily.
 *
 * @param cookieName string The name of the cookie to set
 * @param value string The contents of the cookie
 * @param duration number OPTIONAL: The amount of time, in minutes, until the cookie expires.
 * 	If not set, the cookie will last two years.
 * @param path string OPTIONAL: The path to restrict the cookie to.  If not set, the cookie
 * 	will be readable by the entire domain.
 * @param domain string OPTIONAL: The domain to associate the cookie with.  If not set, the
 * 	cookie will be associated with the domain in the user's URL string.
 * @return true if the cookie was sent successfully, false otherwise.
 */
function bakeCookie($cookieName, $value, $duration=false, $path=false, $domain=false) {
	if ($duration)
		$expiretime = time() + 60 * $duration;
	else
		$expiretime = time() + 60 * 60 * 24 * 365*2; // Expire in two years by default
	if (!$domain) {
		$domain = $_SERVER['HTTP_HOST'];
		while (preg_match('/\\..+\\./', $domain))
			$domain = substr($domain, strpos($domain, '.') + 1);
	}
	if (!$path)
		$path = '/';
	return setcookie($cookieName, $value, $expiretime, $path, $domain);
}

/**
 * Deletes a cookie by setting its expiration date to two years in the past.
 *
 * @param cookieName string The name of the cookie to delete
 * @param path string OPTIONAL: The path the cookie was associated with when
 * 	it was set.
 * @param domain string OPTIONAL: The domain that the cookie was associated
 * 	with when it was set.
 * @return true if the updated cookie was sent successfully; false otherwise.
 */
function eatCookie($cookieName, $path=false, $domain=false) {
	$expiretime = time() - (60 * 60 * 24 * 365*2);
	if (!$domain) {
		$domain = $_SERVER['HTTP_HOST'];
		while (preg_match('/\\..+\\./', $domain))
			$domain = substr($domain, strpos($domain, '.') + 1);
	}
	if (!$path)
		$path = '/';
	return setcookie($cookieName, '', $expiretime, $path, $domain);
}


An example of use:
// Set a cookie with a two-year expiration:
bakeCookie("username", "Kyek");

// Delete the cookie
eatCookie("username");

// Set another cookie with a five-minute expiration
bakeCookie("worst-band-ever", "Nickelback", 5);


But all I'm using in these is the basic setcookie method that Comkid is teaching -- it just figures out the details on its own so you don't need to deal with them :)

Promoted to official tutorial!
0


User is offline GreySyntax 

  • Group: Members
  • Posts: 102
  • Joined: 08-March 10
  • Expertise:HTML,CSS,PHP,Java,Javascript,Ruby on Rails,Node.js,SQL

Posted 03 April 2010 - 05:34 PM (#12)

View PostKyek, on 27 March 2010 - 09:26 AM, said:

Great little tutorial :) Since Mack and magik referred to it, here are the functions I use in my apps. This is updated from the version I posted in another thread, as it uses a duration instead of a specific expiration date.
/**
 * Sets a cookie quickly and easily.
 *
 * @param cookieName string The name of the cookie to set
 * @param value string The contents of the cookie
 * @param duration number OPTIONAL: The amount of time, in minutes, until the cookie expires.
 * 	If not set, the cookie will last two years.
 * @param path string OPTIONAL: The path to restrict the cookie to.  If not set, the cookie
 * 	will be readable by the entire domain.
 * @param domain string OPTIONAL: The domain to associate the cookie with.  If not set, the
 * 	cookie will be associated with the domain in the user's URL string.
 * @return true if the cookie was sent successfully, false otherwise.
 */
function bakeCookie($cookieName, $value, $duration=false, $path=false, $domain=false) {
	if ($duration)
		$expiretime = time() + 60 * $duration;
	else
		$expiretime = time() + 60 * 60 * 24 * 365*2; // Expire in two years by default
	if (!$domain) {
		$domain = $_SERVER['HTTP_HOST'];
		while (preg_match('/\\..+\\./', $domain))
			$domain = substr($domain, strpos($domain, '.') + 1);
	}
	if (!$path)
		$path = '/';
	return setcookie($cookieName, $value, $expiretime, $path, $domain);
}

/**
 * Deletes a cookie by setting its expiration date to two years in the past.
 *
 * @param cookieName string The name of the cookie to delete
 * @param path string OPTIONAL: The path the cookie was associated with when
 * 	it was set.
 * @param domain string OPTIONAL: The domain that the cookie was associated
 * 	with when it was set.
 * @return true if the updated cookie was sent successfully; false otherwise.
 */
function eatCookie($cookieName, $path=false, $domain=false) {
	$expiretime = time() - (60 * 60 * 24 * 365*2);
	return setcookie($cookieName, '', $expiretime, $path, $domain);
}


An example of use:
// Set a cookie with a two-year expiration:
bakeCookie("username", "Kyek");

// Delete the cookie
eatCookie("username");

// Set another cookie with a five-minute expiration
bakeCookie("worst-band-ever", "Nickelback", 5);


But all I'm using in these is the basic setcookie method that Comkid is teaching -- it just figures out the details on its own so you don't need to deal with them :)

Promoted to official tutorial!

Nice i must admit i like how your wrapping it all up i might end up doing something like that in one of my projects :P
0


User is offline Mack 

  • http://mackgoodstein.com/
  • Group: Members
  • Posts: 2072
  • Joined: 08-March 10
  • Expertise:HTML,CSS,PHP,Javascript

Posted 03 April 2010 - 06:40 PM (#13)

View PostKyek, on 27 March 2010 - 09:26 AM, said:

Great little tutorial :) Since Mack and magik referred to it, here are the functions I use in my apps. This is updated from the version I posted in another thread, as it uses a duration instead of a specific expiration date.
/**
 * Sets a cookie quickly and easily.
 *
 * @param cookieName string The name of the cookie to set
 * @param value string The contents of the cookie
 * @param duration number OPTIONAL: The amount of time, in minutes, until the cookie expires.
 * 	If not set, the cookie will last two years.
 * @param path string OPTIONAL: The path to restrict the cookie to.  If not set, the cookie
 * 	will be readable by the entire domain.
 * @param domain string OPTIONAL: The domain to associate the cookie with.  If not set, the
 * 	cookie will be associated with the domain in the user's URL string.
 * @return true if the cookie was sent successfully, false otherwise.
 */
function bakeCookie($cookieName, $value, $duration=false, $path=false, $domain=false) {
	if ($duration)
		$expiretime = time() + 60 * $duration;
	else
		$expiretime = time() + 60 * 60 * 24 * 365*2; // Expire in two years by default
	if (!$domain) {
		$domain = $_SERVER['HTTP_HOST'];
		while (preg_match('/\\..+\\./', $domain))
			$domain = substr($domain, strpos($domain, '.') + 1);
	}
	if (!$path)
		$path = '/';
	return setcookie($cookieName, $value, $expiretime, $path, $domain);
}

/**
 * Deletes a cookie by setting its expiration date to two years in the past.
 *
 * @param cookieName string The name of the cookie to delete
 * @param path string OPTIONAL: The path the cookie was associated with when
 * 	it was set.
 * @param domain string OPTIONAL: The domain that the cookie was associated
 * 	with when it was set.
 * @return true if the updated cookie was sent successfully; false otherwise.
 */
function eatCookie($cookieName, $path=false, $domain=false) {
	$expiretime = time() - (60 * 60 * 24 * 365*2);
	return setcookie($cookieName, '', $expiretime, $path, $domain);
}


An example of use:
// Set a cookie with a two-year expiration:
bakeCookie("username", "Kyek");

// Delete the cookie
eatCookie("username");

// Set another cookie with a five-minute expiration
bakeCookie("worst-band-ever", "Nickelback", 5);


But all I'm using in these is the basic setcookie method that Comkid is teaching -- it just figures out the details on its own so you don't need to deal with them :)

Promoted to official tutorial!


Your eatCookie doesn't work right on my server. You have to add path and domain to it:

function eatCookie($cookieName, $path=false, $domain=false) {
	$expiretime = time() - (60 * 60 * 24 * 365*2);
    if (!$domain) {
		$domain = $_SERVER['HTTP_HOST'];
		while (preg_match('/\\..+\\./', $domain))
			$domain = substr($domain, strpos($domain, '.') + 1);
	}
	if (!$path)
		$path = '/';
	return setcookie($cookieName, '', $expiretime, $path, $domain);
}


Not sure if its just my server, or if its all servers, but thats the only way to get it to work on mine.
0


User is offline Kyek 

  • Founder of wdR
  • Group: Administrators
  • Posts: 5081
  • Joined: 20-February 10
  • LocationPhiladelphia, PA, USA
  • Expertise:HTML,CSS,PHP,Java,Javascript,Node.js,SQL

Posted 04 April 2010 - 07:14 AM (#14)

You're absolutely right -- I actually realized last night that eatCookie wouldn't work for anyone using a different configuration than mine. Updating my code with that now :) Thanks!
0


User is offline Mack 

  • http://mackgoodstein.com/
  • Group: Members
  • Posts: 2072
  • Joined: 08-March 10
  • Expertise:HTML,CSS,PHP,Javascript

Posted 04 April 2010 - 10:17 AM (#15)

View PostKyek, on 04 April 2010 - 07:14 AM, said:

You're absolutely right -- I actually realized last night that eatCookie wouldn't work for anyone using a different configuration than mine. Updating my code with that now :) Thanks!


Whats your server config that that would work? Do you have a default path and domain?
0


User is offline Kyek 

  • Founder of wdR
  • Group: Administrators
  • Posts: 5081
  • Joined: 20-February 10
  • LocationPhiladelphia, PA, USA
  • Expertise:HTML,CSS,PHP,Java,Javascript,Node.js,SQL

Posted 04 April 2010 - 04:15 PM (#16)

That was on Appulous, where I had nginx configged to forcibly remove any subdomain. When there's no sub and you're forcing a / path, the original eatCookie works.
0


User is offline Mack 

  • http://mackgoodstein.com/
  • Group: Members
  • Posts: 2072
  • Joined: 08-March 10
  • Expertise:HTML,CSS,PHP,Javascript

Posted 04 April 2010 - 07:15 PM (#17)

View PostKyek, on 04 April 2010 - 04:15 PM, said:

That was on Appulous, where I had nginx configged to forcibly remove any subdomain. When there's no sub and you're forcing a / path, the original eatCookie works.


If you go to www.mackgoodstein.com it makes it mackgoodstein.com if thats similar? The code still didn't work for me
0


User is offline Kyek 

  • Founder of wdR
  • Group: Administrators
  • Posts: 5081
  • Joined: 20-February 10
  • LocationPhiladelphia, PA, USA
  • Expertise:HTML,CSS,PHP,Java,Javascript,Node.js,SQL

Posted 04 April 2010 - 07:28 PM (#18)

Very strange! That's what's in the appdb code and it worked a treat :)
0


User is offline Mack 

  • http://mackgoodstein.com/
  • Group: Members
  • Posts: 2072
  • Joined: 08-March 10
  • Expertise:HTML,CSS,PHP,Javascript

Posted 04 April 2010 - 08:28 PM (#19)

View PostKyek, on 04 April 2010 - 07:28 PM, said:

Very strange! That's what's in the appdb code and it worked a treat :)


Hmm, this is my code:


if ($host ~* ^www\.(.*)) {
    set $host_without_www $1; 
    rewrite ^(.*)$ http://$host_without_www$1 permanent;
}

0


User is offline dida 

  • Group: Members
  • Posts: 1982
  • Joined: 10-March 10
  • Expertise:HTML,CSS,PHP,Java,Javascript,Python,Ruby on Rails,Node.js,SQL,Graphics,Flash,MongoDB,CouchDB,Cassandra

Posted 05 April 2010 - 03:03 PM (#20)

this thread is really dangerous.after i have read throught it i have rushed in the kitchen and started to bake some cookies...but they got burnt...i cooked one again, but i couldnt find anything to eat it with...then i have started to kill the cookies..slowly...painfully...
then i woke up...
0


Share this topic:


  • 2 Pages +
  • 1
  • 2
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users


Enter your sign in name and password


Sign in options
  Or sign in with these services