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 2 of 2
  1. #1
    Senior Coder
    Join Date
    May 2006
    Posts
    1,683
    Thanks
    28
    Thanked 4 Times in 4 Posts

    Problem with cookie write/read

    Hi,

    I am wanting to count real users to my website
    and as most bots do not use cookies, setting a cookie and
    then checking it seemed to be a fairly good approximation.

    so ...

    This is my code:

    PHP Code:
    setcookie("user""Alex Porter"time()+3600);

    if (isset(
    $_COOKIE["user"])) {
        
    $sql "UPDATE platforms SET
            views = views+1,
            last_date = '$today_time'
            WHERE platform_id =  '$Db_platform_id' "
    ;
        }
    else {
        
    $last_bot $_SERVER['HTTP_USER_AGENT'];
        
    $last_bot safe_sqlsubstr $last_bot 98 ));
        
        
    $sql "UPDATE platforms SET
            bot_views = bot_views+1,
            last_bot = '$last_bot',
            last_date = '$today_time'
            WHERE platform_id =  '$Db_platform_id' "
    ;
        } 
        
    mysql_query($sql)or die("Could not update Platform Display count for Db_platform_id: $Db_platform_id"mysql_error()); 
    Is this a correct way to set and check cookies ?

    Will the cookie get set "in time" to be checked by the next php statement ?

    I am not getting any errors so far, but just wanted to
    know if this is likely to work ??


    Thanks for any help



    .
    Last edited by jeddi; 06-21-2013 at 09:25 AM.
    If you want to attract and keep more clients, then offer great customer support.

    Support-Focus.com. automates the process and gives you a trust seal to place on your website.
    I recommend that you at least take the 30 day free trial.

  • #2
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,352
    Thanks
    61
    Thanked 528 Times in 515 Posts
    Quote Originally Posted by jeddi View Post
    PHP Code:
    setcookie("user""Alex Porter"time()+3600);

    if (isset(
    $_COOKIE["user"])) { 
    Is this a correct way to set and check cookies ?
    Technically yes. The use of setcookie sets the cookie and the cookie is accessed via the $_COOKIE array. You however have not used them correctly.

    Quote Originally Posted by jeddi View Post
    Will the cookie get set "in time" to be checked by the next php statement ?
    No and it's to do with the way the http protocol works.

    When you make a request for a page, your browser sends REQUEST HEADERS. This includes any cookies that it has already stored for that site which are then put into the $_COOKIE array. The server then replies with reply headers that include any cookies being set.

    Now in your script, you're setting a cookie after the request has been made and you're then trying to read the cookie straight away. This won't work because the request headers have already been sent (hence your script is running) and you will not get them again until another http request is made and your script starts again from scratch.

    The link in my signature about headers already been sent will show you more about the http process.
    See my new CodingForums Blog: http://www.codingforums.com/blogs/tangoforce/

    Many useful explanations and tips including: Cannot modify headers - already sent, The IE if (isset($_POST['submit'])) bug explained, unexpected T_CONSTANT_ENCAPSED_STRING, debugging tips and much more!


  •  

    Posting Permissions

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