Home >Backend Development >PHP Tutorial >Introduction and example application of php setcookie_PHP tutorial
The setcookie() function sends an HTTP cookie to the client. A cookie is a variable sent to the browser by the server. Cookies are typically small text files that a server embeds on a user's computer. This cookie is sent each time the computer requests a page through the browser.
The setcookie() function sends an http cookie to the client.
A cookie is a variable sent by the server to the browser. Cookies are typically small text files that a server embeds on a user's computer. This cookie is sent each time the computer requests a page through the browser.
The name of the cookie is specified as a variable of the same name. For example, if the cookie being sent is named "name", a variable named $user will automatically be created containing the cookie's value.
The cookie must be assigned before any other output is sent.
This function returns true if successful, false otherwise.
Grammar
setcookie(name,value,expire,path,domain,secure)
*/
$cookieinfo=session_get_cookie_params(); //Get cookie information
if((empty($cookieinfo['domain']))&&(empty($cookieinfo['secure']))) //Check whether the result is empty
{
setcookie(session_name(),'',time()-3600,$cookieinfo['path']); //Set cookie
}
elseif(empty($cookieinfo['secure'])) //Check whether the option is empty
{
setcookie(session_name(),'',time()-3600,$cookieinfo['path'],$cookieinfo['domain']); //Set cookie
}
else
{
setcookie(session_name(),'',time()-3600,$cookieinfo['path'],$cookieinfo['domain'],$cookieinfo['secure']);
}
//session_destroy(); Log out session
print_r($_session);
print_r($_cookie);
session_set_cookie_params(0,'/yourpath/'); //Set the cookie lifetime and path
/*
Note: When sending a cookie, the cookie value is automatically url encoded. URL decoding is done on reception. If you don't need this, you can use setrawcookie() instead.
*/