Jump to content

If statement issues with checking my cookie info!?!


suess0r

Recommended Posts

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]
Link to comment
Share on other sites

You could split it based on the & sign.

[code]<?php
// printing out
if (!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]

Regards
Huggie
Link to comment
Share on other sites

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
Link to comment
Share on other sites

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]

Regards
Huggie
Link to comment
Share on other sites

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 cookie
setcookie('cookie_city', $_GET['city'], time()+3600);

// This won't work
echo $_COOKIE['cookie_city']; // doesn't get set until the next page load

// But this will
echo $_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.

Regards
Huggie
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.