Welcome to HBH! If you have tried to register and didn't get a verification email, please using the following link to resend the verification email.

Cookies in PHP


BluePain's Avatar
Member
0 0

If I have set an cookie and then want to read it. Lets say it has the name user and value 1. Then I want a script to read it and if it has the correct values and names it does something. How can I make that happen in PHP?


ghost's Avatar
0 0
$user = $_COOKIE['user'];
if($user == 1)
{
//Do Something
}
?>```

ghost's Avatar
0 0

mastergamer wrote:

$user = $_COOKIE['user'];
if($user == 1)
{
//Do Something
}
?>```

If you do this, you only check for the value of $_COOKIE['user'] , if you want to check if the cookie has been created too you could use something like this:




```markup<?php
	if(isset($_COOKIE['user']) && $_COOKIE['user']==1)
		{
			print("The cookie has been set and the value= 1" );
		}
		
	else
		{
			print("No cookies for you today!" );
		}
		
?>```


Note that the following line that mastergamer wrote GENARATES $_COOKIE['user'], so checking for the existance of the cookie would be pointless...

Hope that helps,

S-H

ghost's Avatar
0 0

You should replace print by echo, echo are faster than print to execute.


ghost's Avatar
0 0

Yeah man, don't you just hate it when you need to wait a whole friggin millisecond longer because some lame coder used "print" instead of "echo"!? :xx::p


ghost's Avatar
0 0

Soulhunter wrote: Yeah man, don't you just hate it when you need to wait a whole friggin millisecond longer because some lame coder used "print" instead of "echo"!? :xx::p

Yes. :|


ghost's Avatar
0 0

They have different uses, therefore the appropriate function should be used respectively.

~T