Jump to content

Recommended Posts

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
Link to comment
https://forums.phpfreaks.com/topic/32071-cookie-only-echos-after-refresh/
Share on other sites

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]
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]
You cannot use cookies on the same page you created them. In order to access the cookie you just created the page will
have to be refreshed. Then the new cookie data will be loaded. Its to do with the headers. Reads up on how http headers work.
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 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.
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.