Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 11-30-2012, 02:10 PM   PM User | #1
Oatley
New Coder

 
Join Date: Sep 2012
Posts: 69
Thanks: 56
Thanked 0 Times in 0 Posts
Oatley is an unknown quantity at this point
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..
Oatley is offline   Reply With Quote
Old 11-30-2012, 02:30 PM   PM User | #2
firepages
Super Moderator


 
Join Date: May 2002
Location: Perth Australia
Posts: 3,890
Thanks: 5
Thanked 79 Times in 78 Posts
firepages will become famous soon enough
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)
firepages is offline   Reply With Quote
Users who have thanked firepages for this post:
Oatley (11-30-2012)
Old 11-30-2012, 02:31 PM   PM User | #3
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,635
Thanks: 4
Thanked 2,448 Times in 2,417 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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.
Fou-Lu is offline   Reply With Quote
Users who have thanked Fou-Lu for this post:
Oatley (11-30-2012)
Old 11-30-2012, 02:33 PM   PM User | #4
firepages
Super Moderator


 
Join Date: May 2002
Location: Perth Australia
Posts: 3,890
Thanks: 5
Thanked 79 Times in 78 Posts
firepages will become famous soon enough
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)
firepages is offline   Reply With Quote
Old 11-30-2012, 04:23 PM   PM User | #5
Oatley
New Coder

 
Join Date: Sep 2012
Posts: 69
Thanks: 56
Thanked 0 Times in 0 Posts
Oatley is an unknown quantity at this point
Thanks I'll have a look at that

Last edited by Oatley; 11-30-2012 at 04:33 PM..
Oatley is offline   Reply With Quote
Old 11-30-2012, 04:27 PM   PM User | #6
Oatley
New Coder

 
Join Date: Sep 2012
Posts: 69
Thanks: 56
Thanked 0 Times in 0 Posts
Oatley is an unknown quantity at this point
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..
Oatley is offline   Reply With Quote
Old 11-30-2012, 05:40 PM   PM User | #7
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,635
Thanks: 4
Thanked 2,448 Times in 2,417 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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.
Fou-Lu is offline   Reply With Quote
Users who have thanked Fou-Lu for this post:
Oatley (11-30-2012)
Old 11-30-2012, 05:42 PM   PM User | #8
Oatley
New Coder

 
Join Date: Sep 2012
Posts: 69
Thanks: 56
Thanked 0 Times in 0 Posts
Oatley is an unknown quantity at this point
Ignore me thanks. Worked it out myself totally forgot about output buffering, but thanks for your help.
Oatley is offline   Reply With Quote
Old 11-30-2012, 05:43 PM   PM User | #9
AndrewGSW
Senior Coder

 
Join Date: Apr 2011
Location: London, England
Posts: 2,120
Thanks: 15
Thanked 354 Times in 353 Posts
AndrewGSW will become famous soon enough
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
AndrewGSW is offline   Reply With Quote
Users who have thanked AndrewGSW for this post:
Oatley (11-30-2012)
Old 11-30-2012, 05:43 PM   PM User | #10
Oatley
New Coder

 
Join Date: Sep 2012
Posts: 69
Thanks: 56
Thanked 0 Times in 0 Posts
Oatley is an unknown quantity at this point
Thanks Fou-Lu and Andrew just worked it out at the same time as you kindly posted.
Oatley is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 02:08 PM.


Advertisement
Log in to turn off these ads.