homepage Welcome to WebmasterWorld Guest from 54.205.241.107
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Pubcon Platinum Sponsor 2014
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
Need to set city cookies like craigslist
How to make a craigslist like cookie
creativeramsey

5+ Year Member



 
Msg#: 3944507 posted 2:37 am on Jul 2, 2009 (gmt 0)

I need to know how to in PHP to set a conditional cookie, one that displays 3 cities and then wants you select one you set the cookie for that computer so that the next time they come back, they go to that homepage instead of having to choose cites everytime, I am a PHP noob so any advice would help and thanks ahead of time.

 

max4

5+ Year Member



 
Msg#: 3944507 posted 7:57 am on Jul 2, 2009 (gmt 0)

Use the following cookie as an example:
[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]

penders

WebmasterWorld Senior Member penders us a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month



 
Msg#: 3944507 posted 8:05 am on Jul 2, 2009 (gmt 0)

The PHP Manual is a good resource:
setcookie() [uk2.php.net]

creativeramsey

5+ Year Member



 
Msg#: 3944507 posted 10:48 pm on Jul 2, 2009 (gmt 0)

thank you for what you have contribute so far, but I have set it locally on my MAMP server and I basically have an index.php file that reads this;

<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?

tr8er8

5+ Year Member



 
Msg#: 3944507 posted 6:01 am on Jul 3, 2009 (gmt 0)

your not doing time()+3600 your subtracting:

<?php
setcookie("city", "Oakland", time()+3600);
echo "Hey Oakland"
?>

penders

WebmasterWorld Senior Member penders us a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month



 
Msg#: 3944507 posted 8:46 am on Jul 3, 2009 (gmt 0)

Note that time()+3600 is only 1 hour in the future (3600 seconds). If you are wanting to set a cookie for returning visitors you might want to set this to something like 7 days (60*60*24*7 = 604800 seconds)... time()+604800

creativeramsey

5+ Year Member



 
Msg#: 3944507 posted 8:52 am on Jul 3, 2009 (gmt 0)

Ok all of its done here is the completed code;
<?php
if (isset($_COOKIE["city"])) {
if ($_COOKIE["city"] == 'fresno') {
header("Location: fresno.php");
}
}
if (isset($_COOKIE["city"])) {
if ($_COOKIE["city"] == 'oakland') {
header("Location: oakland.php");
}
}
?>

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.

g1smd

WebmasterWorld Senior Member g1smd us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 3944507 posted 9:06 am on Jul 3, 2009 (gmt 0)

If you are going to have more than half a dozen cities, put the city and cookie data in an array. Your code will then be very much smaller, and faster, and will be easy to maintain.

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.

creativeramsey

5+ Year Member



 
Msg#: 3944507 posted 9:10 am on Jul 3, 2009 (gmt 0)

O Ok I see, now the array itself, I can not envision exactly how that would come about, how would I go about that, and also how can I have an onclick delete cookie so they can go back to the page without being redirected.

creativeramsey

5+ Year Member



 
Msg#: 3944507 posted 9:11 am on Jul 3, 2009 (gmt 0)


O Ok I see, now the array itself, I can not envision exactly how that would come about, how would I go about that, and also how can I have an onclick delete cookie so they can go back to the page without being redirected.

tr8er8

5+ Year Member



 
Msg#: 3944507 posted 5:02 pm on Jul 3, 2009 (gmt 0)

to delete the cookie on a button click:

<input type="submit" name="submit" value="Unset Cookie">

<?php

if(isset($_POST['submit'])) { // Checks if the button is clicked
setcookie("city", "Oakland", time()-3600); // Unsets the cookie
}

?>

creativeramsey

5+ Year Member



 
Msg#: 3944507 posted 6:11 pm on Jul 3, 2009 (gmt 0)

Thank you again and this technique expires the cookie right, now what if I want to just delete any cookie named city?

tr8er8

5+ Year Member



 
Msg#: 3944507 posted 6:33 pm on Jul 3, 2009 (gmt 0)

just use the same thing except change the value/name to whatever you want.

creativeramsey

5+ Year Member



 
Msg#: 3944507 posted 6:47 pm on Jul 3, 2009 (gmt 0)

Its not deleting the cookie, Ive tried several times on both.

tr8er8

5+ Year Member



 
Msg#: 3944507 posted 7:16 pm on Jul 3, 2009 (gmt 0)

you need to learn for yourself spoon-feeding wont get you anywhere.

take a look at this tutorial on cookies it looks pretty good.

[w3schools.com...]

creativeramsey

5+ Year Member



 
Msg#: 3944507 posted 7:26 pm on Jul 3, 2009 (gmt 0)

i dont know what spoon feeding is other than the literal meaning, but to everyone else I have set the cookie but cannot delete the cookie, I've used this code to try and delete and the syntax checks out,
<?php
setcookie('city', 'oakland', time()+(60*60*24*7)); ?>
<html>
<head>
</head>
<body>
<p>hey oakland one</p>
<p> <a href="/index.php">Back to Cities</a></p>
<input type="submit" name="submit" value="Unset Cookie">

<?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.

penders

WebmasterWorld Senior Member penders us a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month



 
Msg#: 3944507 posted 9:31 pm on Jul 3, 2009 (gmt 0)

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)?

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved