Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Added the delete_cookie() function in order to help with the removal of ... #1120

Closed
wants to merge 7 commits into from

3 participants

John Clarke M. Vugteveen Andrey Andreev
John Clarke

...cookies that might have been set. Although one can make use of the set_cookie() function and pass an 0 via the expire field, this is a simpler method

John Clarke jc1arke Added the delete_cookie() function in order to help with the removal …
…of cookies that might have been set. Although one can make use of the set_cookie() function and pass an 0 via the expire field, this is a simpler method
386c3ec
John Clarke

That is in the helper. So without having to load the helper since you are already working with the cookies in the controller side of things. So for instance, if you have CI_Cookie loaded you don't have to load the helper as well in order to perform this action.

system/core/Input.php
@@ -226,6 +226,27 @@ public function cookie($index = '', $xss_clean = FALSE)
// ------------------------------------------------------------------------
/**
+ * Remove cookie
+ *
+ * Accepts the name of the cookie and expires it accordingly
+ *
+ * @param string the name of the cookie
+ * @return bool
+ */
+ public function delete_cookie($name = '')
+ {
+ if( isset($name) && !empty($name) && $this -> cookie( $name ) ) // Check if the cookie exists, just a failsafe, one never knows.... :)
M. Vugteveen
it-can added a note

should be: if (isset($name) && ! empty($name) && $this->cookie($name))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
system/core/Input.php
@@ -226,6 +226,27 @@ public function cookie($index = '', $xss_clean = FALSE)
// ------------------------------------------------------------------------
/**
+ * Remove cookie
+ *
+ * Accepts the name of the cookie and expires it accordingly
+ *
+ * @param string the name of the cookie
+ * @return bool
+ */
+ public function delete_cookie($name = '')
+ {
+ if( isset($name) && !empty($name) && $this -> cookie( $name ) ) // Check if the cookie exists, just a failsafe, one never knows.... :)
+ {
+ $this -> set_cookie( $name, '', 0 );
M. Vugteveen
it-can added a note

should be: $this->set_cookie($name, '', 0);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
system/core/Input.php
@@ -226,6 +226,27 @@ public function cookie($index = '', $xss_clean = FALSE)
// ------------------------------------------------------------------------
/**
+ * Remove cookie
+ *
+ * Accepts the name of the cookie and expires it accordingly
+ *
+ * @param string the name of the cookie
+ * @return bool
+ */
+ public function delete_cookie($name = '')
+ {
+ if( isset($name) && !empty($name) && $this -> cookie( $name ) ) // Check if the cookie exists, just a failsafe, one never knows.... :)
+ {
+ $this -> set_cookie( $name, '', 0 );
+ return true; // Let the calling function know that it succeeded
M. Vugteveen
it-can added a note

TRUE

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
system/core/Input.php
((4 lines not shown))
+ * Remove cookie
+ *
+ * Accepts the name of the cookie and expires it accordingly
+ *
+ * @param string the name of the cookie
+ * @return bool
+ */
+ public function delete_cookie($name = '')
+ {
+ if( isset($name) && !empty($name) && $this -> cookie( $name ) ) // Check if the cookie exists, just a failsafe, one never knows.... :)
+ {
+ $this -> set_cookie( $name, '', 0 );
+ return true; // Let the calling function know that it succeeded
+ }
+
+ return false; // Could not find the cookie, or no name provided
M. Vugteveen
it-can added a note

FALSE

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
M. Vugteveen

Is it also an idea to change the cookie_helper, delete function to use the input class so we don't have multiple functions doing the same thing, but still keep backwards compatiblilty?

John Clarke jc1arke Changed the format of the core Input class to conform to CI standards…
…, as well as changed the cookie_helper to make use of the new delete_cookie function in the core Input controller
079c419
John Clarke

Thanks for all the notes :) I have just pushed the update for the formatting as suggested and also setup the delete_cookie helper function to make use of the delete_cookie function in the core CI_Input controller

system/core/Input.php
@@ -226,6 +226,27 @@ public function cookie($index = '', $xss_clean = FALSE)
// ------------------------------------------------------------------------
/**
+ * Remove cookie
+ *
+ * Accepts the name of the cookie and expires it accordingly
+ *
+ * @param string the name of the cookie
+ * @return bool
+ */
+ public function delete_cookie($name = '')
+ {
+ if(isset($name) && !empty($name) && $this->cookie($name)) // Check if the cookie exists, just a failsafe, one never knows.... :)
Andrey Andreev Owner
narfbg added a note

isset($name) will always return TRUE, unless you pass NULL as it's value and ! empty($name) basically evaluates as isset($name) && $name != '' anyway ... you don't need isset() here.

Also you should have a space on each side of the exclamation mark and one after if as well.
Might be a good idea to move the comment on the previous line too, I doubt that anybody in our days would be reading that with a 480x320 resolution, but it still causes a need to use the scrollbar in e.g. this view we're currently looking at. :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
system/core/Input.php
@@ -226,6 +226,27 @@ public function cookie($index = '', $xss_clean = FALSE)
// ------------------------------------------------------------------------
/**
+ * Remove cookie
+ *
+ * Accepts the name of the cookie and expires it accordingly
+ *
+ * @param string the name of the cookie
+ * @return bool
+ */
+ public function delete_cookie($name = '')
+ {
+ if(isset($name) && !empty($name) && $this->cookie($name)) // Check if the cookie exists, just a failsafe, one never knows.... :)
+ {
+ $this->set_cookie($name, '', 0);
+ return TRUE; // Let the calling function know that it succeeded
Andrey Andreev Owner
narfbg added a note

It's good to have code commented, but ... well, we know what return TRUE/FALSE does in a 10-line method. :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Andrey Andreev
Owner

I like the idea of the cookie helper utilizing the new method instead, but if it's going to work this way - one of the two should change its behavior. The delete_cookie() function used to accept 4 parameters ... now it's only 1 that's actually used - doesn't make much sense.

Another thing is (although I'm probably just giving too much thought on this) - do we need that to return boolean instead of void? I really can't think of a case where such check would be needed and even if there is, just because we've sent an empty cookie, we're not guaranteed that the browser will delete it - that's out of our control.

... just some thoughts, not saying that it must be like I suggest.

John Clarke

Thanks mate for the comments. I have updated the code as you can see from the latest 2 commits. Also, returning boolean seems like a safer option. One doesn't have to check for output, such as

delete_cookie('thisIsMyCookie'); // No need to check output, just perform the action

All depends on your usage I suppose, but still a good point though

system/core/Input.php
@@ -226,6 +226,28 @@ public function cookie($index = '', $xss_clean = FALSE)
// ------------------------------------------------------------------------
/**
+ * Remove cookie
+ *
+ * Accepts the name of the cookie and expires it accordingly
+ *
+ * @param string the name of the cookie
+ * @return bool
+ */
+ public function delete_cookie($name = '')
+ {
+ // Check if the cookie exists, just a failsafe, one never knows.... :)
+ if (! empty($name) && $this->cookie($name))
Andrey Andreev Owner
narfbg added a note

Still need to put that space - if ( ! empty($name) ...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Andrey Andreev
Owner

I'm still concerned with backwards compatibility of the helper's function.

M. Vugteveen

Yes it should be full backwards compatible...

John Clarke

Okay lads, you need to make a decision here now. I removed the extra 3 parameters from the helper based on your comment that there is no need for them anymore. I can place them back for backward compatibility, but if we keep going "needs to be backward compatible" we will end up with another IE6 monster where we keep saying "but this needs to work in it" :)

Suggestions?

Andrey Andreev
Owner

Actually, nobody said that there's no need for it. I said that it doesn't make sense, because they're not used.

There are 2 problems with this one:

  • It does need to be fully backwards compatible. I understand what you mean with the IE6 analogy, but this is not the same case. We're talking about removing useful functionality here, not switching to more modern solutions. :)
  • It causes too much discussion for something that we can already do with the set_cookie() method.

I'd like to hear some other opinions before I ask you to do yet another change on it ... @philsturgeon @ericbarnes @katzgrau @seejohnrun ?

Here's what I personally think - revert the changes to the cookie helper function and stick to just adding a method to the Input library. For obvious reasons, it's indeed best to reuse code when possible, but in this case it's not the same logic to be executed, so that rule doesn't fit.

M. Vugteveen

@narfbg Think you're right... Leave the cookie helper like it was and add delete_cookie to input class...

Andrey Andreev
Owner

It's been awhile ... now looking at the setcookie() manual - you can pretty much do the same thing by just calling setcookie(<cookiename>);, so we don't need this.

Andrey Andreev narfbg closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Mar 4, 2012
  1. John Clarke

    Added the delete_cookie() function in order to help with the removal …

    jc1arke authored
    …of cookies that might have been set. Although one can make use of the set_cookie() function and pass an 0 via the expire field, this is a simpler method
Commits on Mar 5, 2012
  1. John Clarke

    Changed the format of the core Input class to conform to CI standards…

    jc1arke authored
    …, as well as changed the cookie_helper to make use of the new delete_cookie function in the core Input controller
  2. John Clarke
Commits on Mar 6, 2012
  1. John Clarke
  2. John Clarke

    Changed the helper function to only have a single parameter as oppose…

    jc1arke authored
    …d to the 4 parameters as current as there is no more need for the other 3
  3. John Clarke
Commits on Mar 8, 2012
  1. John Clarke
This page is out of date. Refresh to see the latest.
Showing with 22 additions and 0 deletions.
  1. +22 −0 system/core/Input.php
22 system/core/Input.php
View
@@ -226,6 +226,28 @@ public function cookie($index = '', $xss_clean = FALSE)
// ------------------------------------------------------------------------
/**
+ * Remove cookie
+ *
+ * Accepts the name of the cookie and expires it accordingly
+ *
+ * @param string the name of the cookie
+ * @return bool
+ */
+ public function delete_cookie($name = '')
+ {
+ // Check if the cookie exists, just a failsafe, one never knows.... :)
+ if ( ! empty($name) && $this->cookie($name))
+ {
+ $this->set_cookie($name, '', 0);
+ return TRUE;
+ }
+
+ return FALSE;
+ }
+
+ // ------------------------------------------------------------------------
+
+ /**
* Set cookie
*
* Accepts six parameter, or you can submit an associative
Something went wrong with that request. Please try again.