Forum Moderators: coopster
[fixed]
setcookie("city", "New York", time()+3600);
[/fixed] In our example, "city" is the name of the cookie, "New York" is the value you choose, time() is a PHP function which records the current time and +3600 is a number you choose, in seconds, that tells the client when to delete the cookie.
To remove the cookie, you would use the following:
[fixed]
setcookie("city", "New York", time()-3600);
[/fixed] Peace,
Mohamed
[edited by: max4 at 8:14 am (utc) on July 2, 2009]
<html>
<head>
</head>
<body>
<p>hey</p><p><a href="./oakland.php">Oakland</a></p><p><a href="./centralvalley.php">Central Valley</a></p><p></p>
</body>
</html>
then I have Oakland.php;
<?php
setcookie("city", "Oakland", time()-3600);
echo "Hey Oakland"
?>
then I have CentralValley.php;
<?php
setcookie("city", "Central Valley", time()-3600);
echo "Hey Oakland"
?>
But its not setting the cookie, am I doing something wrong?
all I need now, is to setup the PHP code that deletes it when they want to go back to the cities page, like on oakland if they want to go back to the cities page it goes then redirects.
If the filename/URL is always the same as the city name, you can simplify it even more by getting the city name from the cookie and asking for 'that name dot php'. In that case you need to make sure the cookie holds the state as well as the city name. You also need a robust way to handle spaces in names so that you have hyphens in the URL.
take a look at this tutorial on cookies it looks pretty good.
[w3schools.com...]
<?php
if(isset($_POST['submit'])) { // Checks if the button is clicked
setcookie("city", "", time()-(60*60*24*9)); // Unsets the cookie
}
?>
</body>
</html>
and to <strong>tr8er8</strong> also anyone can send me to a reference but this is a forum, its what its for, if you just wanted to send out references then im sure they would have made it a "WhatSiteShouldIGoToNext" type of site, but its not, thanks for the reference but the comment isnt needed.
if(isset($_POST['submit'])) {
$_POST['submit'] is never set because your form is incomplete (always a good idea to check your HTML with the W3C HTML Validator [validator.w3.org]). An INPUT element on its own won't do anything. You need something like:
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<input type="submit" name="submit" value="Unset Cookie">
</form>
But your unset cookie code (ie. setcookie(...)) is also in the wrong place. You will get an error if it executes. setcookie() must be called before any output is sent to the browser. ie. before the <html> tag. setcookie() just sets an HTTP response header. ALL headers must be set before output is sent. So something like:
<?php
setcookie('city', 'oakland', time()+(60*60*24*7));
if (isset($_POST['submit'])) { // Checks if the button is clicked
setcookie("city", "", time()-(60*60*24*9)); // Unsets the cookie
}
?>
<html>
Also, you should not be setting AND unsetting your cookie in one go. You should EITHER be setting OR unsetting the cookie, not both. This could be problematic to test, since you will only be able to test for the true existence of the cookie on the next page request.
The link that tr8er8 gives is a good read and will answer many of the questions discussed here, as will the setcookie() page in the PHP manual (that I linked to above) - these should not be dismissed. Some background reading on the subject will help greatly, in fact I would have said was essential if you are starting out with cookies. For instance, what directory/path is your cookie available to? What subdomain(s)?