Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    Regular Coder
    Join Date
    Jan 2008
    Posts
    334
    Thanks
    9
    Thanked 0 Times in 0 Posts

    $_COOKIE not working correctly

    I'm having a problem with $_COOKIE's on my site not working correctly. It seems as if the cookie is being set but at the same time is not being set as a global cookie which should happen. All cookies should be global. The use this cookie is to hold the chat color that a user is using. This is set by a ajax post request, you basically select a color from a color picker and on click it send that color via post inside of the $_POST['c'] array. After that it is then a met by a associative array that hold all of the colors:

    PHP Code:
        $colors = array(=> '#f1f1f1'=> '#ff593f'=> '#A51792',
                        
    => '#4E9258'=> '#2B65EC'=> '#DE954B',
                        
    => '#36b3e1'=> '#FD13DE'=> '#6eb361');
                        
        
    $num $_POST['c']; 
    Then is later used inside of the setcookie(). Its odd, though because it seems to be setting it on the chatsystem.php page but when I go to echo or print it out onto the page where the chat system actually set, I get nothing for a value. I had this working before with $_SESSION but for this chat system I'm going to need to use cookies. So without any more delay I'll provide you guys with some code and see if you guys can figure out my mess up.

    PHP Code:
        //Change your chat color.
        
    $colors = array(=> '#f1f1f1'=> '#ff593f'=> '#A51792',
                        
    => '#4E9258'=> '#2B65EC'=> '#DE954B',
                        
    => '#36b3e1'=> '#FD13DE'=> '#6eb361');
                        
        
    $num $_POST['c'];
        
        if(
    $_POST['c'] && $_POST['c'] <= 9) {
            
    setcookie('color',$colors[$num],time()+172800);
        } 
    I should also say that I turned on ini_set('display_errors', E_ALL); and I found no errors being reported.

    Thanks for any help I can get on this.
    Last edited by Jon W; 03-11-2010 at 12:36 PM.

  • #2
    Regular Coder
    Join Date
    Jan 2008
    Posts
    334
    Thanks
    9
    Thanked 0 Times in 0 Posts
    Okay so a little bit of a update. I found if I set the path works. Though I had trouble with it switching colors so I went a head and did this and this worked fine...

    PHP Code:
        if($_POST['c'] && $_POST['c'] <= 9) {
            
    setcookie('color','',time()-172800,"");
            
    setcookie('color',$colors[$num],time()+172800,"/");
        } 
    My question is, is this the way you would of done it? lol
    Last edited by Jon W; 03-11-2010 at 12:54 PM.

  • #3
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    You shouldn't need to destroy a cookie in order to set a new one, it should just overwrite the original value. Now, if you were to set a cookie and immediately check if _COOKIE exists, then it should be null; setcookie doesn't stick until the next load of a page that can access it.

    Path itself should be set within the cookie. But this depends on what you're doing with it. Using / will let you use it at a domain level, which IMO is the most handy way of using them. By default, its the current directory and any sub directories, so if you're cookie is being set at you're root directory level, then it should be accessable to all of you're scripts.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)


  •  

    Posting Permissions

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