Home  >  Article  >  Backend Development  >  Introduction and example application of php setcookie_PHP tutorial

Introduction and example application of php setcookie_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 16:55:12931browse

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);

//Use session_set_cookie_params to set the lifetime and path

session_set_cookie_params(0,'/yourpath/'); //Set the cookie lifetime and path

/*

Note: The value of the cookie named "user" can be accessed via $http_cookie_vars["user"] or $_cookie["user"].

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.

*/

http://www.bkjia.com/PHPjc/631713.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/631713.htmlTechArticleThe 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. Every...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn