SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Evangelist
    Join Date
    Apr 2007
    Posts
    398
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Printing a $_COOKIE value set?

    Hello

    Iím trying to figure out an online poll. Now if submit is hit here:

    PHP Code:
    if ($_REQUEST['submit']) {

    //Define Variables to make cookie
    $ending_time time()+(365*24*60*60);
    $new_end mktime($ending_time);
    $my_cookie 'vote_'.$_REQUEST['vote']; 

    if (!
    $_COOKIE[$my_cookie]) {
      
    setcookie($my_cookie1$new_end); // set the cookie
      
    print "This prints so I know i'm in the if statement"//This prints upon submit"
      
    print $_COOKIE["".$my_cookie.""]; //THIS COOKIE NAME DOES NOT PRINT
    }

    The above statement is run. Now the bit I get stuck on is I want to output(print) the contents of the cookie ie:

    PHP Code:
      print $_COOKIE["".$cookieName.""]; 
    But canít see why it wonít print. I know the statement actually accesses this part as I can print out This prints so I know i'm in the if statement

    Anyone advise?

    Thanks

  2. #2
    Non-Member
    Join Date
    Oct 2009
    Posts
    1,852
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sure, this cookie name does not print
    There is no such cookie sent to your server. You just checked it, with
    if (!$_COOKIE[$my_cookie])

    Why don't you print 1 instead?

  3. #3
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    $_COOKIE contains values that the browser sent to the server as part of the current http request. setcookie() sends a header to the browser telling it that on future http requests, it should send that cookie. So, setcookie() does not populate $_COOKIE for you.

    ps- become familiar with print_r() and var_dump() to help you debug. $_COOKIE is an array that can be printed with those. print_r($_COOKIE);

    mktime() should be removed. You aren't using it correctly, and it isn't needed.

    setcookie() can also be failing. Make sure to turn up error_reporting when developing/debugging code
    PHP Code:
    //top of scripts
    error_reporting(E_ALL);
    ini_set('display_errors'1); 

  4. #4
    SitePoint Evangelist
    Join Date
    Apr 2007
    Posts
    398
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks, i'm far from familiar with cookies and trying to learn although I don't quite get this. So how could I actually SET a cookie that I can check for. I've used print_r($_COOKIE) and the below has been returned. Take it this is nothing to do with bthe cookie i'm trying to create?

    PHP Code:
    [__utma] => 

  5. #5
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    looks like something from google analytics.

    Have you take the advice of error reporting and mktime and modified the code yet?

  6. #6
    SitePoint Wizard PHPycho's Avatar
    Join Date
    Dec 2005
    Posts
    1,201
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    print $_COOKIE["".$cookieName.""]; 
    Why are you using extra double quotes.
    Simply write as:
    PHP Code:
    print $_COOKIE[$cookieName]; 


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
  •