suess0r Posted December 28, 2006 Share Posted December 28, 2006 hey there,i've asked this question before here but i'm still having similiar issues....I'm POSTing my city_pick variable to mpg1.php from mcity.php and here's the code of how i set the cookie, problem it doesn't echo until after i refresh the page... i put the setcookie() before i echo'd it so i'm wondering if there's a way around this somehow..[quote]<?php$time = time(); $city = $_REQUEST['city_pick'];if($city != ""){ setcookie("cookie_city", $city, time()+36000);}?>--body-- <?php $city = $_COOKIE["cookie_city"]; if (!isset($city)) { echo '<a href="/mcity.php">Please select a city</a>';} else { echo "Partying in: {$city}!<br>"; echo '<a href="/mcity.php">Change cities</a>';} ?>[/quote]now, from a previous post someone suggested this... which works but when i go to 'select a new city' and go through the process again of picking a city etc. it doesn't set the cookie again, after it's already been set..[quote]<?php$time = time(); if (isset($_REQUEST['city_pick']) && $_REQUEST['city_pick']!="") { $city = $_REQUEST['city_pick']; setcookie('city_pick', $city, time()+3600);} elseif (isset($_COOKIE['city_pick'])) { $city = $_COOKIE['city_pick'];}?><?php if (!isset($city)) { echo '<a href="/mcity.php">Please select a city[/url]'; } else { echo "Your city is: {$city}!"; echo '<a href="/mcity.php">Change cities[/url]'; }?>[/quote]thanks for everything guys, i live by this forum! ;D Quote Link to comment https://forums.phpfreaks.com/topic/32071-cookie-only-echos-after-refresh/ Share on other sites More sharing options...
Psycho Posted December 28, 2006 Share Posted December 28, 2006 Change REQUEST to POST in all instances. Quote Link to comment https://forums.phpfreaks.com/topic/32071-cookie-only-echos-after-refresh/#findComment-148853 Share on other sites More sharing options...
suess0r Posted December 28, 2006 Author Share Posted December 28, 2006 i changed the _REQUEST to _POST and i'm still unable to reset the cookie...http://www.mclb411.com/mpg1.php <- try it out and see what i mean.. Quote Link to comment https://forums.phpfreaks.com/topic/32071-cookie-only-echos-after-refresh/#findComment-148932 Share on other sites More sharing options...
Psycho Posted December 28, 2006 Share Posted December 28, 2006 I don't know what to tell you. I created a test page (see below) using the code you provided for your form as a template. When using REQUEST I could set the city one time, but it would not change after that. When I changed it to POST (which makes sense since the action in your form is POST) the test page wiorked correctly.Copy the code below into a page called test.php and see if it works for you as it did me.[code]<?php if (isset($_POST['city_pick']) && $_POST['city_pick']!="") { $city = $_POST['city_pick']; setcookie('city_pick', $city, time()+3600); } elseif (isset($_COOKIE['city_pick'])) { $city = $_COOKIE['city_pick']; } if (!isset($city)) { echo 'You have not selected a city'; } else { echo "Your city is: {$city}!"; }?><form action="test.php" method="post" enctype="multipart/form-data" name="form1" id="form1"> Choose your city: <select name="city_pick" class="field select medium" style="width: 160px;"> <option value="Los Angeles">Los Angeles</option> <option value="New York">New York</option> <option value="Wichita">Wichita</option> </select> <input name="submit" type="submit"></form>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/32071-cookie-only-echos-after-refresh/#findComment-148970 Share on other sites More sharing options...
suess0r Posted December 28, 2006 Author Share Posted December 28, 2006 hmmm... well here you are POSTing on the same page as the form, but i'm POSTing to another page, do you think that will cause issues with the refresh or something? Quote Link to comment https://forums.phpfreaks.com/topic/32071-cookie-only-echos-after-refresh/#findComment-148994 Share on other sites More sharing options...
Psycho Posted December 28, 2006 Share Posted December 28, 2006 It shouldn't matter. The page doesn't know it is posting to itself. All it knows is that it is receiving POSTED data. This is just to demonstrate how this would work. But, just for the hell of it, here are two pages with the same logic. Still works fine for me:test_1.php[code]<?php if (isset($_POST['city_pick']) && $_POST['city_pick']!="") { $city = $_POST['city_pick']; setcookie('city_pick', $city, time()+3600); } elseif (isset($_COOKIE['city_pick'])) { $city = $_COOKIE['city_pick']; } if (!isset($city)) { $msg = "You have not selected a city"; } else { $msg = "Your city is: {$city}!"; }?><html><head></head><body><a href="test_2.php">Choose a city</a><br><br><?=$msg?></body></html>[/code]test_2.php[code]<html><head></head><body><form action="test_1.php" method="post" enctype="multipart/form-data" name="form1" id="form1"> Choose your city: <select name="city_pick" class="field select medium" style="width: 160px;"> <option value="Los Angeles">Los Angeles</option> <option value="New York">New York</option> <option value="Wichita">Wichita</option> </select> <input name="submit" type="submit"></form></body></html>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/32071-cookie-only-echos-after-refresh/#findComment-149004 Share on other sites More sharing options...
suess0r Posted December 28, 2006 Author Share Posted December 28, 2006 thanks mj, i got it working, you really didn't need to go out of your way like that to create those pages but thanks for all your help, i really appreciate it! cya round Quote Link to comment https://forums.phpfreaks.com/topic/32071-cookie-only-echos-after-refresh/#findComment-149021 Share on other sites More sharing options...
wildteen88 Posted December 29, 2006 Share Posted December 29, 2006 You cannot use cookies on the same page you created them. In order to access the cookie you just created the page willhave to be refreshed. Then the new cookie data will be loaded. Its to do with the headers. Reads up on how http headers work. Quote Link to comment https://forums.phpfreaks.com/topic/32071-cookie-only-echos-after-refresh/#findComment-149299 Share on other sites More sharing options...
dbo Posted December 29, 2006 Share Posted December 29, 2006 Use sessions?Cookies limit you imo. If a user doesn't have them enabled you have to work around it or shut them out. If you use sessions (depending on how you have php configured) and your users have cookies enabled it will actually store session data in a cookie. If a user doesn't have cookies enabled the session data is stored on the server and you're set.To use sessions put session_start(); at the very top of your page before any headers/output have been sent. This must be at the top of every page that will use your session data. Then to use your session variables you simply do this: $_SESSION['my_variable_name'] = 'my_value';If you need to store it locally do something like this: $blah = $_SESSION['my_variable_name'];You can then work with it as normal: echo $blah; would be the same thing as: echo $_SESSION['my_variable_name'];Need to store an array in a session? $blah = Array(); $blah[] = 1; $blah[] = 2; $blah[] = 3; $_SESSION['my_array'] = $blah; $bleh = $_SESSION['my_array']; for( $i = 0; $i < count($bleh); ++$i ) { echo $bleh[$i] . "\n"; }Hope this helps some people. Quote Link to comment https://forums.phpfreaks.com/topic/32071-cookie-only-echos-after-refresh/#findComment-149346 Share on other sites More sharing options...
wildteen88 Posted December 29, 2006 Share Posted December 29, 2006 [quote author=dbo link=topic=120155.msg493171#msg493171 date=1167408347]and your users have cookies enabled it will actually store session data in a cookie. If a user doesn't have cookies enabled the session data is stored on the server and you're set.[/quote]Thats not true. All sessions are stored on the server. No session data gets saved to a cookie. The only thing that gets saved to the cookie is the unique session id, which referees to the session stored on the server. If the user doesn't have cookies enabled then the session id will sent through the URL. Quote Link to comment https://forums.phpfreaks.com/topic/32071-cookie-only-echos-after-refresh/#findComment-149381 Share on other sites More sharing options...
suess0r Posted December 29, 2006 Author Share Posted December 29, 2006 is there a way i can set the cookie on the mcity.php page like after they click it then when i submit to the next page it will be refreshed, or a way of setting it on the way to the mpg1.php somehow or what... uggg so frustrated with this Quote Link to comment https://forums.phpfreaks.com/topic/32071-cookie-only-echos-after-refresh/#findComment-149445 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.