setcookie tutorial with login example and syntax of function
setcookie description
It simply sets a cookie to be send with HTTP headers. one cookie is set we can access it later with help of following
$_COOKIE['cookie_name']
$_REQUEST['cookie_name']
setcookie Syntax
Boolean setcookie(string $name,string $value,int $expire,string $path,string $domain,bool $secure,bool $httponly );
Parameters to the function
some tips first.
There is only on parameter which is compulsory that is $name of cookie.
We can skip the parameter by simply putting ("") and 0 for $expire as its a integer.
string $name : Name of cookie
string $value : Value for cookie.
int $expire : Unix timestamp specifying no of seconds the cookie is valid.
one day : time()+60*60*24 .. so simple
If its set to 0 then cookie expires after browser close.
string $path : The cookie will be available to the specified path only(if specified).
string $domain : The domain that the cookie is available.\
bool $secure : If its TRUE then it can be set only on secure connection.
bool $httponly( > php5.2.0) : This option is added in php 5.2.0 and if its set as TRUE then cookie is accessible only through http protocol n not for the external ecripts like javascript. But not supported in all browsers. So use it on your own risk. But it helps against XSS attacks.
example n sample code
setcookie('cookiename','cokievalue',0);
This will set a cookie with name "cookiename" with value cookivalue and will expire on browser close.
This can be access at server side like this
$_COOKIE['cookiename'];
?>

0 comments:
Post a Comment
Post your helpful suggestions on this...