Forum Moderators: coopster
Quick question though. Now I'm finding myself setting a variable with a value a little more complicated.
I set a variable in PHP equal to what I want the cookie to be, and then I have the statement setting the cookie by the name of that new variable I created.
I wanted to be able to reduce my code, so I can just set the cookie directly, without having to make a variable, but my code keeps breaking. Here's what it looks like:
$rand=rand(100,999)."-".substr(ereg_replace("[^0-9]","",$REMOTE_ADDR),-8).substr(time(),-4);
if(!$HTTP_COOKIE_VARS["test"]){setcookie("test",$rand,time()+604800);}
And here's what I'm trying to make it look like:
if(!$HTTP_COOKIE_VARS["test"]){setcookie("test",rand(100,999)."-".substr(ereg_replace("[^0-9]","",$REMOTE_ADDR),-8).substr(time(),-4),time()+604800);}
If anybody has some tips why it's breaking, I would greatly appreciate it. Thanks
Also, regarding scripty's method, that relies on register_globals in the ini file being set to 1, which is considered a security risk, and has been defaulted to 0 in recent versions of PHP.
Best bet is
if (!isset($_COOKIE['user']) ) { setcookie('user',$username,time()+604800); }
Although the {} isn't really needed since it's just one statement after the if.
I can't call it on the code breaking, but, I think it might be better to do the variable method. It's easier to read your code later that way.