Page 1 of 1

Php Cookies

Posted: Mon Aug 01, 2005 12:00 pm
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

Posted: Mon Aug 01, 2005 12:02 pm
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

Posted: Mon Aug 01, 2005 12:07 pm
by feyd
remember to do cookie setting before before any output to the browser is done...

Still not working must be doing something wrong.

Posted: Mon Aug 01, 2005 12:24 pm
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

Posted: Mon Aug 01, 2005 12:28 pm
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.

Posted: Mon Aug 01, 2005 12:32 pm
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.

Posted: Mon Aug 01, 2005 12:36 pm
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>

Posted: Mon Aug 01, 2005 12:45 pm
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.

Posted: Mon Aug 01, 2005 12:50 pm
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.

Posted: Mon Aug 01, 2005 12:53 pm
by Magicman0022
so do I just add a plus to my time variable ??

Posted: Mon Aug 01, 2005 12:55 pm
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).

Posted: Mon Aug 01, 2005 1:09 pm
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?>

Posted: Mon Aug 01, 2005 2:53 pm
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
	}
?>

Posted: Mon Aug 01, 2005 3:01 pm
by timvw
I've always found the following more readable :)

Code: Select all

setcookie('foo','bar', strtotime('now +30 days'));

Posted: Mon Aug 01, 2005 3:06 pm
by theda
Oh, by the way, PHP BBcode is uppity today.