Php Cookies

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Magicman0022
Forum Newbie
Posts: 11
Joined: Thu Jul 28, 2005 8:38 am

Php Cookies

Post by Magicman0022 »

I am having real trouble implementing a cookie in one of my webpages and calling it up in my next.

Can someone please tell me the code needed to create a cookie called Module that holds a variable $Module and then show me how to call up that cookie in another page and allow me to use that variable in my other php script.

I am a total novice to php and am realyl struggling with this please help.

thanx
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

how to set a cookie

setcookie("name","value","howlongtopersist");

example:

setcookie("module",$module,86400);

how to call it up in your php page

echo $_COOKIE['module'];

setcookie() reference -> http://us3.php.net/manual/en/function.setcookie.php
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

remember to do cookie setting before before any output to the browser is done...
Magicman0022
Forum Newbie
Posts: 11
Joined: Thu Jul 28, 2005 8:38 am

Still not working must be doing something wrong.

Post by Magicman0022 »

It still isn't working I set up this little bit of code as a test.... Please have a look and tell me whats wrong wit it.

Code: Select all

<?php
	$Module = 'CMT911';
	setcookie ("module",$Module,86400);

php?>

<html>

<head>
</head>
<body>
	<a href = "Test1.php"> Test1.php </a>
	<?php
	echo $_COOKIE['module'];
	php?>
</body>
</html>

Code: Select all

and my other program test1 has this code below and cant call up the cookie.

Code: Select all

<html>
	<head>
	</head>
	<body>
	<?php
		$Module = $_COOKIE['module'];
		echo "$Module"
	php?>

	</body>
</html>

Code: Select all

I'm sure I am just being stupid but could someone please help me resolve this.

Thanx
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

I don't know if I'm correct in saying this, but I don't believe you can set a cookie and then call it on the same page.

Go to that page, then refresh it, and see if it works as desired.
Magicman0022
Forum Newbie
Posts: 11
Joined: Thu Jul 28, 2005 8:38 am

Post by Magicman0022 »

Tried that and it still doesn't work.

I'm sure the cookie is being set but I just cant seem to call it in my new page. Anyone have any ideas.
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

well on your second code echo $Module is missing a ; at the end. Other than that throwing you a syntax error, I do not know why it isn't showing.

give this a shot

<html>
<head>
</head>
<body>

<?
if(isset($_COOKIE['module'])
{
$Module = $_COOKIE['module'];
echo $Module;
} ELSE
{
echo "Your cookie is not set.";
}
?>

</body>
</html>
User avatar
wwwapu
Forum Contributor
Posts: 197
Joined: Wed Apr 07, 2004 11:57 am
Location: Turku, Finland

Post by wwwapu »

Your cookie expires at the beginning of the year 1970. Unlike session cookies you have to set time()+86400 to be the expiration time.
Magicman0022
Forum Newbie
Posts: 11
Joined: Thu Jul 28, 2005 8:38 am

Post by Magicman0022 »

just comes up as a blank screen so something isn't working write.

could it be that i dont have an up to date version of php or something.
Magicman0022
Forum Newbie
Posts: 11
Joined: Thu Jul 28, 2005 8:38 am

Post by Magicman0022 »

so do I just add a plus to my time variable ??
User avatar
wwwapu
Forum Contributor
Posts: 197
Joined: Wed Apr 07, 2004 11:57 am
Location: Turku, Finland

Post by wwwapu »

Like The Manual tells:
Manual wrote:The time the cookie expires. This is a Unix timestamp so is in number of seconds since the epoch. In other words, you'll most likely set this with the time() function plus the number of seconds before you want it to expire. Or you might use mktime().

time()+60*60*24*30 will set the cookie to expire in 30 days. If not set, the cookie will expire at the end of the session (when the browser closes).
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

if you're getting a blank screen, there's something wrong with your PHP syntax.

Make sure you always use ;'s and close php with ?> not php?>
theda
Forum Contributor
Posts: 332
Joined: Sat Feb 19, 2005 8:35 am
Location: USA

Post by theda »

You could just try &gt;&gt; print $module; &lt;&lt; I prefer print over echo in certain areas.

And if this helps at all, this is how I do my cookie based theme system:

Code: Select all

<?
// Theme Identity
	if (isset($_COOKIE['the']) AND empty($_GET['the'])) { // if cookie 'the' is set and empty (get 'the'), then...
		$the = $_COOKIE['the']; // variable theme equals the cookie value
	} elseif (isset($_GET['the'])) { // else get theme variable
		$the = $_GET['the']; // the get equals http://www.url.com/?the=theme
		setcookie('the',$the, time()+60*60*24*30); // set the cookie to that value
	} else {
		setcookie('the','see', time()+60*60*24*30); // otherwise automatically set the cookie to a default theme
		$the = "see"; // and when you load the page, the theme equals the default variable
	}
?>
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

I've always found the following more readable :)

Code: Select all

setcookie('foo','bar', strtotime('now +30 days'));
theda
Forum Contributor
Posts: 332
Joined: Sat Feb 19, 2005 8:35 am
Location: USA

Post by theda »

Oh, by the way, PHP BBcode is uppity today.
Post Reply