views:

54

answers:

3

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?

A: 

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

m1tk4
+2  A: 

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.

AvatarKava
A: 

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.

StudiousJoseph