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 10 of 10
  1. #1
    New Coder
    Join Date
    Sep 2012
    Posts
    76
    Thanks
    61
    Thanked 0 Times in 0 Posts

    Checking for a cookie?

    Hello, I'm stuck on something with a cookie and must be missing something obvious, but I can't see what.

    In my script I want to load a page and

    1 - Check if a cookie exists
    2- If it does not launch a JavaScript function called myFunction and set a cookie

    Now, the first time I launch the page the function shows (as there is no cookie in my browser) which is great but on subsequent loads when I refresh the page it should not show as a cookie should be created

    PHP Code:
    <?php
    function check_cookie() {
    If(
    $_COOKIE['test'] != TRUE) {
    ?>
    <script type="text/javascript">
    myFunction();
    </script> 
    <?php
    setcookie
    ("test"TRUEtime()+3600);
    }
    }
    ?>
    <?php check_cookie
    (); ?>
    Can anyone see where I am going wrong here?

    Thank you
    Last edited by Oatley; 11-30-2012 at 05:46 PM.

  • #2
    Super Moderator
    Join Date
    May 2002
    Location
    Perth Australia
    Posts
    4,058
    Thanks
    10
    Thanked 95 Times in 93 Posts
    as it happens the code works for me on firefox, but any issues are possibly down to the fact that setcookie expects a string value as the value, not a boolean (true/false) so if you used !== then you check might fail

    so really you should
    PHP Code:
    function check_cookie() {
        if(
    $_COOKIE['test'] != "someval"){
            echo 
    'setting cookie';
            
    setcookie("test""someval"time()+3600);
        }

    though you could just as easily test if the cookie is set .. if(isset($_COOKIE['test'])){etc
    resistance is...

    MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)

  • Users who have thanked firepages for this post:

    Oatley (11-30-2012)

  • #3
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    That looks like you can never set a cookie. Since you don't check the existence of the cookie before reading from it, and you have output before the setcookie header, it will result in no cookie ever being set.
    This is probably what you want:
    PHP Code:
    <?php
    function check_cookie()
    {
        if (!isset(
    $_COOKIE['test']) || isset($_COOKIE['test']) && $_COOKIE['test'] != true)
        {
            
    setcookie('test'truetime() + 3600);
        
    ?>
    <script type="text/javascript">
    myFunction();
    </script> 

        <?php
        
    }
    }
    check_cookie(); ?>

    Edit:
    Sounds like firepages has output buffering set and error reporting shut down on notice.

  • Users who have thanked Fou-Lu for this post:

    Oatley (11-30-2012)

  • #4
    Super Moderator
    Join Date
    May 2002
    Location
    Perth Australia
    Posts
    4,058
    Thanks
    10
    Thanked 95 Times in 93 Posts
    Quote Originally Posted by Fou-Lu View Post
    Edit:
    Sounds like firepages has output buffering set and error reporting shut down on notice.
    DOH, indeedy I do have output buffering on sorry
    resistance is...

    MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)

  • #5
    New Coder
    Join Date
    Sep 2012
    Posts
    76
    Thanks
    61
    Thanked 0 Times in 0 Posts
    Thanks I'll have a look at that
    Last edited by Oatley; 11-30-2012 at 04:33 PM.

  • #6
    New Coder
    Join Date
    Sep 2012
    Posts
    76
    Thanks
    61
    Thanked 0 Times in 0 Posts
    Thanks. However the JavaScript function I call is still showing all the time and not one time only. It seems the cookie is never created.

    My JavaScript is simply a "hello" alert, which I have placed in a function called myFunction().. I've even tried it on a different server and I still have the same problem.

    Any further ideas please?

    Thank you
    Last edited by Oatley; 11-30-2012 at 05:35 PM.

  • #7
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    Make sure you haven't sent any output prior to this point or you will toss a headers already sent error. You can even verify prior by simply checking the headers_sent function. Verify the cookie path and domain are valid too. If you are on a subdomain, you'll need to modify the domain, but this particular example shouldn't cause any issues when it comes to the cookie path.

  • Users who have thanked Fou-Lu for this post:

    Oatley (11-30-2012)

  • #8
    New Coder
    Join Date
    Sep 2012
    Posts
    76
    Thanks
    61
    Thanked 0 Times in 0 Posts
    Ignore me thanks. Worked it out myself totally forgot about output buffering, but thanks for your help.

  • #9
    Senior Coder
    Join Date
    Apr 2011
    Location
    London, England
    Posts
    2,120
    Thanks
    15
    Thanked 354 Times in 353 Posts
    As mentioned by firepages the cookie value is a string so should be set and compared as "true", not true (a boolean).
    "I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
    Validate your HTML and CSS

  • Users who have thanked AndrewGSW for this post:

    Oatley (11-30-2012)

  • #10
    New Coder
    Join Date
    Sep 2012
    Posts
    76
    Thanks
    61
    Thanked 0 Times in 0 Posts
    Thanks Fou-Lu and Andrew just worked it out at the same time as you kindly posted.


  •  

    Posting Permissions

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