Part of the EllisLab Network
   
2 of 3
2
Cookie Helper
Posted: 27 April 2007 01:40 PM   [ Ignore ]   [ # 16 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  472
Joined  09-26-2006

Whooowee, but this station is busy on Fridays!!!

Jim, looks good to me, but I’ll want to test drive a bit.

I’d change the $secure type though, it should not use a bool, but rather 1 for true and 0 for false.

The php manual says:

Because setting a cookie with a value of FALSE will try to delete the cookie, you should not use boolean values. Instead, use 0 for FALSE and 1 for TRUE.

EDIT: Jim, would’nt be the first time a manual was wrong.

 Signature 

Old programmers never die, they just parse away.

Profile
 
 
Posted: 27 April 2007 02:12 PM   [ Ignore ]   [ # 17 ]  
Lab Assistant
RankRank
Total Posts:  128
Joined  04-06-2007
Oscar Bajner - 27 April 2007 01:40 PM

The php manual says:

Because setting a cookie with a value of FALSE will try to delete the cookie, you should not use boolean values. Instead, use 0 for FALSE and 1 for TRUE.

Oscar, I found that page and read through it.  The quote is indeed in the manual as you say, but it is referring to the value of a cookie, not any of its flags.  If I had a cookie that I needed to set with a BOOL value, the PHP folks are recommending I use 1 or 0 as the value instead.

Also please take note, I have edited my earlier post to remove the cookie helper code and post a link to my site where it is now hosted with a modified user guide page.

Jim

Profile
 
 
Posted: 27 April 2007 02:47 PM   [ Ignore ]   [ # 18 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  472
Joined  09-26-2006

Jim you are absolutely correct grin I did not read that carefully enough

I am testing the changes, but for some reason I’m getting strange results. Sigh, maybe I should leave
this until tommorow.

 Signature 

Old programmers never die, they just parse away.

Profile
 
 
Posted: 27 April 2007 02:53 PM   [ Ignore ]   [ # 19 ]  
Lab Assistant
RankRank
Total Posts:  128
Joined  04-06-2007

Well crap…it is working well for me.  I don’t know 100% that this is a straight-forward drop in replacement for the original, though…and that could be a bad thing as far as getting an update out there.  Maybe if you give the modified user guide page a good look through you can see what is different…?

Profile
 
 
Posted: 27 April 2007 03:13 PM   [ Ignore ]   [ # 20 ]  
Administrator
Avatar
RankRankRankRankRankRank
Total Posts:  7337
Joined  03-23-2006

Yikes!  Again, I just can’t keep up with you guys.

Nothing at this point is committed.  Yes, I will be happy to commit the changes after everyone’s had a chance to vet them.  In fact, I’d be willing to look at the complete library replacement as well.

 Signature 

DerekAllard.com - CodeIgniter, ExpressionEngine, and the World of Web Design
BambooInvoice - Open Source, CodeIgniter powered invoicing.

Profile
MSG
 
 
Posted: 27 April 2007 03:30 PM   [ Ignore ]   [ # 21 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  472
Joined  09-26-2006

Jim, it’s late (Where I am wink I’m probably doing something strange here.
But the error is weird:

$cookie = array(
            
'name'      => 'cookie name',
            
'value'     => 'eat me',
            
'expire'    => 0,
            
'domain'    => '',
            
'path'      => '/',
            
'prefix'    => '',
            
'secure'    => false,
            );
        
set_cookie($cookie);

Gives this :
Message: Cookie names can not contain any of the folllowing ‘=,; \t\r\n1314’ (ccookie name)

I kid you not. Notice the extra ‘c’ in ccookie name.
I get this when I look at the cookie via tamper, it has things like cpath=c and so on.
In FF, It appears the cookie is not set (options, view cookies)

I am going to leave this until tommorow, maybe I’ll be sane again wink

 Signature 

Old programmers never die, they just parse away.

Profile
 
 
Posted: 28 April 2007 05:20 AM   [ Ignore ]   [ # 22 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  472
Joined  09-26-2006

OK, I will leave this as an exercise in “I-am-a-php-guru-cause-i-can-answer-this-question” wink

I noticed that the problem occurs if I pass an array, but not if I use discrete parameters.
So then, if the order of parameters in the foreach is changed to have ‘name’ last, it works!!!

function set_cookie($name, $value = '', $expire = 0, $domain = null, $path = null, $prefix = null, $secure = false)
{
    
if (is_array($name))
    
{
        
foreach (array('value', 'expire', 'domain', 'path', 'prefix', 'secure', 'name') as $item)
        
{
            
if (isset($name[$item]))
            
{
                
$$item = $name[$item];
            
}
        }
    }

This was the original order in the helper, now with ‘secure’ added in before ‘name’.

I’m still testing a few things to make sure we won’t break existing code with the changes to the config defaults.

 Signature 

Old programmers never die, they just parse away.

Profile
 
 
Posted: 28 April 2007 11:03 AM   [ Ignore ]   [ # 23 ]  
Lab Assistant
RankRank
Total Posts:  128
Joined  04-06-2007

Oscar, that makes perfect sense that ‘name’ would have to be the last parameter. I had reordered the array indexes to match the order of the function parameters.  But I forgot that if an array is passed, its name IS $name, and each of its indexes (one of which is also named ‘name’) is turned into a variable of the same name.  So if ‘name’ is anywhere but last, $name is over written by ‘name’ being converted into a variable leaving portions of the array having not be processed.

New file uploaded: modCookieHelper.zip

Profile
 
 
Posted: 29 April 2007 07:09 AM   [ Ignore ]   [ # 24 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  472
Joined  09-26-2006

@ Jim : Yup, everything is fine now with latest update to modCookieHelper.zip

@Derek : I have tested this pretty exhaustively as regards the functionality: That is, setting, getting and
deleting cookies using the modified helper, with and without defaults, via array or discrete params.

I have however only tested on windows with FireFox so far. I think Jim is using FF on Linux.

Jim has done a great job, both on the helper and the User guide page. I don’t think the change to the config
will cause any problems, but perhaps best to revert to the original defaults viz empty prefix, domain and
path = “/”. The new cookie_lifetime config should default to zero.

I’m happy to sign off on this commit. You should be able to grab the helper and user guide page from Jim’s site
as is and commit. If there’s anything else you can think of, let us know, be happy to help.
EDIT: Ozone is at his parade ground best this Sunday wink The user guide refers to
“discreet parameters”, that means modest or unobtrusive, it should be “discrete” meaning
distinct or seperate.

 Signature 

Old programmers never die, they just parse away.

Profile
 
 
Posted: 29 April 2007 09:26 AM   [ Ignore ]   [ # 25 ]  
Lab Assistant
RankRank
Total Posts:  128
Joined  04-06-2007

Wow, Oscar, thanks for that input and your testing (again!).  Tomorrow I will do some heavy stand alone testing on this myself with Fx, IE, and perhaps Opera.  I am confident with this as it is right now, but we wouldn’t want to miss anything.

I changed the user guide page to use ‘discrete’ vice ‘discreet’.  Good catch!

I’d like to see some of the others who were involved in this thread do some testing too since it seems they are likely using cookie helper…

Anyway, I will report back,
Jim

Profile
 
 
Posted: 30 April 2007 07:46 AM   [ Ignore ]   [ # 26 ]  
Lab Assistant
RankRank
Total Posts:  128
Joined  04-06-2007

Ok, I poked around in IE, Fx, and Opera.  I can’t see anything causing any problems at this time.

Any thoughts from others involved in this thread?

Thanks,
Jim

Profile
 
 
Posted: 02 May 2007 11:05 AM   [ Ignore ]   [ # 27 ]  
Lab Assistant
RankRank
Total Posts:  128
Joined  04-06-2007

I have added one more piece to this based on something I ran into with a custom session handler.

When deleting a cookie, it still exists in the $_COOKIE super global.  I added an argument to delete_cookie() which defaults to TRUE.  If it is set to TRUE, delete_cookie() calls for the cookie to be set to the browser with a negative expiration date AND it calls unset() on it in the $_COOKIE array.

If you were to want to hold that cookie value in the $_COOKIE array for some reason making it usable there until the script ends (and only until then because you are sending the negatively expiring cookie to the browser) you could set this flag to FALSE and it would not be explicitly unset() from $_COOKIE.

Here is the modified delete_cookie() function:

/**
* Delete a COOKIE
*
* @access    public
* @param    mixed
* @param    string    the cookie domain.  Usually:  .yourdomain.com
* @param    string    the cookie path
* @param    string    the cookie prefix
* @param     bool
* @return    void
*/
function delete_cookie($name = '', $domain = null, $path = null, $prefix = null, $remove_live = TRUE)
{
    
if($remove_live===TRUE)
    
{
        $CI
=& get_instance();
        
$default_prefix = $CI->config->item('cookie_prefix');
        if (
is_null($prefix) AND ! empty($default_prefix))
        
{
            $prefix
= $default_prefix;
        
}
        $cIndex
= is_array($name) ? $name['name'] : $name;
        if(isset(
$_COOKIE[$prefix.$cIndex]))
        
{
            
unset($_COOKIE[$prefix.$cIndex]);
        
}
    }
    set_cookie
($name, '', -86500, $domain, $path, $prefix);
}

Get the new version: modCookieHelper.zip

Profile
 
 
Posted: 05 May 2007 10:00 AM   [ Ignore ]   [ # 28 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  472
Joined  09-26-2006

@JAAulde : That change looks fine to me, tests ok.

Can you confirm something for me please? Using the set_cookie / get_cookie, can you set a cookie where
the name contains spaces, eg ‘The Cookie Name’.

The cookie spec states that name=value must not contain spaces (among other things) and that it should
be encoded if needed. On my setup I get a PHP error as expected when setting a cookie like “Cookie Jar”.

What’s bugging me is some other strange things that are happening (nothing to do with your changes). But
I don’t want to go into all that until I’m sure of this behaviour.
Thanks, Oscar.

 Signature 

Old programmers never die, they just parse away.

Profile
 
 
Posted: 05 May 2007 01:38 PM   [ Ignore ]   [ # 29 ]  
Lab Assistant
RankRank
Total Posts:  128
Joined  04-06-2007

Oscar I will take a look at that.  Cookies should definitely be encoded—I do that myself in my JavaScript Cookie Library.  It will probably be Monday that I look more closely at it.

Jim

Profile
 
 
Posted: 07 May 2007 08:17 AM   [ Ignore ]   [ # 30 ]  
Lab Assistant
RankRank
Total Posts:  128
Joined  04-06-2007

Ok, here is a question based on preference—please, everyone weigh in.

PHP has two methods of handling URL style encoding—urlencode() and rawurlencode().  These two are exactly the same in that they “return a string in which all non-alphanumeric characters except -_. have been replaced with a percent (%) sign followed by two hex digits” EXCEPT that the first translates spaces as ‘+’ whereas the second translates spaces as ‘ ’.

Points of discussion:

1) I would prefer we used rawurlencode().  While urlencode() more closely matches what a browser does with POSTed data, it is not what JavaScript does when escaping or encoding URI strings.  Since many cookies are set via JavaScript, we would want to use an encoding/decoding method which will match its methods.

2) I would suggest we add the encoding to set_cookie() for all cookies being set via the cookie helper, as well as adding decoding to get_cookie() for all cookies being retrieved via the cookie helper.  The logic here is that we are assuming someone who is getting cookies via the cookie helper is probably setting them via the cookie helper as well, so we would want to decode all cookies on retrieval.

Any discussion before I add this functionality?

Jim

Profile
 
 
   
2 of 3
2
 
Post Marker Legend
New Topic New posts Hot Topic Hot Topic with new posts New Poll New Poll Moved Topic Moved Topic Sticky Topic Sticky topic
Old Topic No new posts Hot Old Topic Hot Topic with no new posts Old Poll Old Poll Closed Topic Closed Topic Announcement Announcements
Theme
Change Theme
Visitor Statistics
The most visitors ever was 819, on March 11, 2010 11:15 AM
Total Registered Members: 120309 Total Logged-in Users: 43
Total Topics: 126427 Total Anonymous Users: 2
Total Replies: 664850 Total Guests: 459
Total Posts: 791277    
Members ( View Memberlist )