Php Cookies
Moderator: General Moderators
-
Magicman0022
- Forum Newbie
- Posts: 11
- Joined: Thu Jul 28, 2005 8:38 am
Php Cookies
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
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
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
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
-
Magicman0022
- Forum Newbie
- Posts: 11
- Joined: Thu Jul 28, 2005 8:38 am
Still not working must be doing something wrong.
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-
Magicman0022
- Forum Newbie
- Posts: 11
- Joined: Thu Jul 28, 2005 8:38 am
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>
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>
-
Magicman0022
- Forum Newbie
- Posts: 11
- Joined: Thu Jul 28, 2005 8:38 am
-
Magicman0022
- Forum Newbie
- Posts: 11
- Joined: Thu Jul 28, 2005 8:38 am
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).
You could just try >> print $module; << I prefer print over echo in certain areas.
And if this helps at all, this is how I do my cookie based theme system:
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
}
?>I've always found the following more readable 
Code: Select all
setcookie('foo','bar', strtotime('now +30 days'));