Tutorial :Better to get value from PHP variable or from cookie?



Question:

Assume that there is a PHP variable:

$variable = 'username';  cookie value = 'username'  

Which one is better to access? get_cookie() or just use $variable?

I have a lot of code using get_cookie('cookie_name') (a CodeIgniter function) instead of using variables.

Will it increase performance if I change it to access $variable instead of cookie?


Solution:1

Cookies are passed from the user's browser to the server every page load, whether you use them or not.

There's really going to be very little performance difference.

What you may want to investigate is whether the data that is being stored in cookies (if you say it's a lot) should be there or not. Many people have cookies turned off, so you only want to store nonessential information (and information that doesn't have to be secure) in them.


Solution:2

Yes, it will, but the increase will be infinitesimally small.


Solution:3

Not only the $variable is somewhat faster, but also consider that a user may simply not accept cookies of any kind, so in general the $variable approach is much better.

I've been developing web apps for a couple of years and never had have to use cookies for anything. I know a lot of people would not agree with me on this, but I've been doing great for now without them.

Regards.


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »