PHP Classes

cCookies

Recommend this page to a friend!

      cCookies  >  All threads  >  cCookies  >  (Un) Subscribe thread alerts  
Subject:cCookies
Summary:issue with detecting cookie
Messages:3
Author:Darren Conyard
Date:2013-01-08 20:33:46
Update:2013-01-09 14:09:44
 

  1. cCookies   Reply   Report abuse  
Picture of Darren Conyard Darren Conyard - 2013-01-08 20:33:46
Hi,

This looks like it should work, but after testing this class the script cant detect the cookie.

I have checked to see if the cookie has been set within the set function, by echoing the setcookie statement and it displays 1 so thats fine.

But when I echo whether the cookie has been set within the get function it comes back as false.

Also the script states that the cookie is not set when it is run.

I was wondering if you were getting the same issue.

Best Regards

Darren Conyard

  2. Re: cCookies   Reply   Report abuse  
Picture of Archzilon Eshun-Davies Archzilon Eshun-Davies - 2013-01-08 22:38:21 - In reply to message 1 from Darren Conyard
Hi Darren

Thanks for using cCookies, here is an example usage
<?php
require_once("cCookies.php");

Usage:
$cookie = new cCookies;
$cookie->set('name', 'value'); // this cookie would disappear
// after you close the brwoser session
$cookie_name = $cookie->get('name');
if ($cookie_name != null)
echo $cookie_name;
else
echo "Cookie not found :( you make cookie monster sad.";
$cookie->destroy('name');
unset($cookie);
?>

I have tried and am not getting anything similar, try this and if you
still have the same issue let me know.

Thanks and Best Regards.

  3. cCookies   Reply   Report abuse  
Picture of Darren Conyard Darren Conyard - 2013-01-09 14:09:44 - In reply to message 2 from Archzilon Eshun-Davies
Hi Arch,

I think I need to explain myself better here too. This is one is a little trickier but hopefully it makes sense. So in this class theres two files cCookies.php and example.php. This is the code from example.php:

<?php
ini_set("display_errors", 1);
require_once("cCookies.php");

$cookie = new cCookies;
$cookie->set('name', 'value'); // this cookie would disappear
// after you close the brwoser session
$cookie_name = $cookie->get('name');
if ($cookie_name != null)
echo $cookie_name;
else
echo "Cookie not found :( you make cookie monster sad.";
$cookie->destroy('name');
unset($cookie);
?>

The text printed on screen is:

Cookie not found :( you make cookie monster sad.

So I thought I would try and solve the problem by first of all seeing where the problem lies. Within cCookies.php I have added the following lines of code:

public static function set( $name, $value, $expire=0, $path='/', $domain='localhost', $secure=0, $httponly=0 )
{
echo "setcookie( $name, $value, $expire, $path, $domain, $secure, $httponly )";
$ret = setcookie( $name, $value, $expire, $path, $domain, $secure, $httponly );

if( $ret ) {
return true;
} else {
return false;
}
}

When running this code I can see that the cookie is being set correctly, from the text printed:

setcookie( name, value, 0, /, localhost, 0, 0 )Cookie not found :( you make cookie monster sad.setcookie( name, , 530492400, /, localhost, 0, 0 )

So I do not think the problem is with the set function. So now I want to test the get function and I do so with the following code:

public static function get( $name ) {
echo $name;
if(isset($_COOKIE[$name])) {
echo "richtig";
} else {
echo "falsch";
}
return( (isset($_COOKIE[$name])) ? $_COOKIE[$name] : null );
}

This code then prints the following text:

namefalschCookie not found :( you make cookie monster sad.

So from what I can tell the cookie $name is not set properly but I do not know why :(

Hopefully this helps.

Best Regards

Darren Conyard