SitePoint Sponsor

User Tag List

Results 1 to 2 of 2

Thread: cookie crumbles

  1. #1
    SitePoint Zealot
    Join Date
    Jul 2005
    Location
    Houston, TX
    Posts
    167
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    cookie crumbles

    I have a map that displays on my website that I want users to have the ability to display or not. By default, it's set up not to show the map. They click on the link which sends a $_GET request, etc., etc., piece of cookie...I mean...cake.

    Ok, not quiet (or else I wouldn't be posting, right?). Everything works fine...except that the visitor must click the link once, wait for it to refresh (while the information remains the same), then click it again for it to work. The only thing I have above the following code in my script is setting the ini to my include path and starting a session. Nothing else.

    Here's the code.

    PHP Code:
    $setMap $_GET['map'];
    if (
    $setMap == 1) {
        
    setcookie("map""1"time()+60*60*24*365);
    } else {
        
    setcookie("map""0"time()+60*60*24*365);
    }

    if (
    $HTTP_COOKIE_VARS["map"] == 0) {
        
    $showMap false;
    } elseif (
    $HTTP_COOKIE_VARS["map"] == 1) {
        
    $showMap true;

    I've posted only this because I believe this is where the problem lies. Of course, later I have an if statement to check the $showMap variable and then display the link and css division accordingly.

    Does anyone see the problem?

    Thanks,

    Tim Trice

  2. #2
    SitePoint Wizard silver trophy
    Join Date
    Mar 2006
    Posts
    6,132
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    so your problem is that you dont want them to have to refresh again after clicking the link to show map? seems you probably also have the problem that 2 page refreshes later it turns showmap back off.


    you need to be careful how you compare variables
    there is a big difference between these 2
    PHP Code:
    // comparing to integer 0
    if ($_GET['map'] == 0) {}

    // comparing to string 0
    if ($_GET['map'] == '0') {} 
    if your comparing it to integer 0, keep in mind a variable that doesnt exist, or even if it exists but just doesnt have a value, is 'equal' to integer 0, or rather, it evaluates to integer 0. you should compare it to string 0, because a variable from _GET _POST _COOKIE etc will generally always be a string.

    PHP Code:

    // default
    $showmap false;

    // make sure the variable was actually passed
    // otherwise we shouldnt act on it
    if (isset($_GET['map'])) {
        
    // if equal to string 1, they want to see the map
        
    if ($_GET['map'] == '1') {
            
    $showmap true;
            
    setcookie("map""1"time()+60*60*24*365);

        
    // elseif equal to string 0, they dont want 
        // to see the map anymore, so set cookie to replace the other cookie
        
    } elseif ($_GET['map'] == '0') {
            
    setcookie("map""0"time()+60*60*24*365);
        }
    }

    // if the cookie exists
    if (isset($_COOKIE['map'])) {
        
    // if the value is string 1
        
    if ($_COOKIE['map'] == '1') {
            
    $showmap true;
        }

    btw all the $HTTP_*_VARS variables are deprecated. its better to use the new superglobals like $_GET $_POST $_COOKIE etc...


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •