suess0r Posted December 18, 2006 Share Posted December 18, 2006 Question:I'm setting my cookie and POSTing it to another page, i want to be able to check if it's set and if it is i want to say "Your city is Cookie_name" if not i want to have a link that says "Please select a city." i'm POSTing the variables fine but all i get after this is "Please select city." even after one has been set, if i echo the $_COOKIE i get the value of my POST, so i was wondering if my if statement was f'd up something?!? :-[[code]<?php$time = time(); $city = $_REQUEST['cookie_city'];$visits = 1;$cookie_string = $city.'&'.$visits; if($city != ""){ setcookie("cookie_city", $cookie_string, $time+3600);}?><?php require_once('Connections/backendform.php'); mysql_select_db($database_backendform, $backendform);$query_landmark = "SELECT landmark FROM landmark ORDER BY landmark ASC";$landmark = mysql_query($query_landmark, $backendform) or die(mysql_error());$row_landmark = mysql_fetch_assoc($landmark);$totalRows_landmark = mysql_num_rows($landmark);?> //printing out <?php if (!isset($_COOKIE['choose_city'])) { echo '<a href="/mcity.php">Please select a city[/url]'; } else { echo 'Your cookie is set to '.$_COOKIE['choose_city'].'!'; } ?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/31153-if-statement-issues-with-checking-my-cookie-info/ Share on other sites More sharing options...
HuggieBear Posted December 18, 2006 Share Posted December 18, 2006 You're cookie is called [color=green]cookie_city[/color], but when you check it to see if it's set, you're referring to is as [color=brown]choose_city[/color].The inconsistency is what's causing the problem.RegardsHuggie Quote Link to comment https://forums.phpfreaks.com/topic/31153-if-statement-issues-with-checking-my-cookie-info/#findComment-143953 Share on other sites More sharing options...
suess0r Posted December 18, 2006 Author Share Posted December 18, 2006 great, it works now. thanks huggie, except it prints Orlando&1. is there anyway to just parse out the $city in the Cookie to print?? Quote Link to comment https://forums.phpfreaks.com/topic/31153-if-statement-issues-with-checking-my-cookie-info/#findComment-143972 Share on other sites More sharing options...
suess0r Posted December 18, 2006 Author Share Posted December 18, 2006 actually it keeps saying Orlando&1 even after i change cities to anything else. It won't replace the cookie, or reset the cookie... i've been trying to figure this out for 5 days now and i really could use some help :-\ Quote Link to comment https://forums.phpfreaks.com/topic/31153-if-statement-issues-with-checking-my-cookie-info/#findComment-143976 Share on other sites More sharing options...
HuggieBear Posted December 18, 2006 Share Posted December 18, 2006 You could split it based on the & sign.[code]<?php// printing outif (!isset($_COOKIE['cookie_city'])){ echo '<a href="/mcity.php">Please select a city[/url]';}else { $cookie_parts = explode("&", $_COOKIE['cookie_city']); echo "Your cookie is set to {$cookie_parts[0]}!";}?>[/code]RegardsHuggie Quote Link to comment https://forums.phpfreaks.com/topic/31153-if-statement-issues-with-checking-my-cookie-info/#findComment-143978 Share on other sites More sharing options...
suess0r Posted December 18, 2006 Author Share Posted December 18, 2006 Thanks huggie, that helped a lot....1 Problem:Whenever I pick a city in mcity.php and POST it over, it doesn't take right away... it's not until i go back and select something else that it shows the Original one i picked first hand. There has to be some issue with my initial call that it doesn't set until after the page loads or something... any thoughts?? ;x Quote Link to comment https://forums.phpfreaks.com/topic/31153-if-statement-issues-with-checking-my-cookie-info/#findComment-144021 Share on other sites More sharing options...
HuggieBear Posted December 18, 2006 Share Posted December 18, 2006 Yes that's correct.This was taken from the [url=http://uk.php.net/manual/en/function.setcookie.php]setcookie()[/url] page in the manual:[quote]Once the cookies have been set, they can be accessed on the next page load with the $_COOKIE or $HTTP_COOKIE_VARS arrays.[/quote]RegardsHuggie Quote Link to comment https://forums.phpfreaks.com/topic/31153-if-statement-issues-with-checking-my-cookie-info/#findComment-144044 Share on other sites More sharing options...
suess0r Posted December 19, 2006 Author Share Posted December 19, 2006 Then should I set the cookie on the mcity.php page onchange() or something? how would you go about making it when they load the mpg1.php that it is already set without having to reload the page somehow? Quote Link to comment https://forums.phpfreaks.com/topic/31153-if-statement-issues-with-checking-my-cookie-info/#findComment-144136 Share on other sites More sharing options...
HuggieBear Posted December 19, 2006 Share Posted December 19, 2006 No, there's no need for that. It's only the cookie variable that can't be used on the same page, you could use the $_POST or $_GET variable instead.Here's an example of what I mean...[code]<?php// Get the city from the drop down list or whatever you have$city = $_GET['city'];// Assign the value to a cookiesetcookie('cookie_city', $_GET['city'], time()+3600);// This won't workecho $_COOKIE['cookie_city']; // doesn't get set until the next page load// But this willecho $_GET['city'];?>[/code]So if you need to use the value of a cookie on the same page that you set it, just use the source, where the cookie came from.RegardsHuggie Quote Link to comment https://forums.phpfreaks.com/topic/31153-if-statement-issues-with-checking-my-cookie-info/#findComment-144313 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.