delight-im / PHP-Cookie

Modern cookie management for PHP
MIT License
165 stars 48 forks source link

Added call to unset() in delete() function #28

Closed Gsinti closed 4 years ago

Gsinti commented 4 years ago

Unset the cookie name in PHP's $_COOKIE variable to prevent it being referenced before headers can be sent.

Gsinti commented 4 years ago

This is helpful in order to reduce boilerplate code as I'm currently calling the unset() function myself after every call to the delete() function in the \Delight\Cookie\Cookie class.

ocram commented 4 years ago

Thank you for your feedback and the pull request!

You are right about this. But just the same is true for the Cookie#save method, isn’t it?

So while you’d like to have Cookie#delete also unset the $_COOKIE variable in the same request, I might like to have Cookie#save create the variable inside $_COOKIE right away.

Do you agree?

So instead of this breaking change, what about two additional methods, Cookie#saveAndSet and Cookie#deleteAndUnset?

Gsinti commented 4 years ago

Good point about the save method lacking this functionality! It too would benefit from being set immediately so that it is available in PHP memory before headers are sent.

I like the idea of two additional methods that can serve this purpose.

ocram commented 4 years ago

Thanks again, @Gsinti!

Implemented in https://github.com/delight-im/PHP-Cookie/commit/103ce742d964457d03edadb3276aead28a36505f and https://github.com/delight-im/PHP-Cookie/commit/83c30014b35b9f690841f879fa77d72a51d94d52

By the way, a minor correction, @Gsinti, the question is not “before headers are sent” vs. “after headers have been sent”. Instead, it’s “before the next line in the script (in the current HTTP request)” vs. “at the start of the next HTTP request”.

Gsinti commented 4 years ago

You're welcome!

I wasn't sure about the exact terminology and I appreciate the lesson in semantics. Thanks for the clarification!