Home  >  Article  >  Backend Development  >  PHP cookie settings delete login detailed explanation_PHP tutorial

PHP cookie settings delete login detailed explanation_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 17:14:38759browse

Cookies are something that are present in all program development. Let me introduce cookie settings (SetCookie) and delete ($_COOKIE) login usage.

1. Set Cookie

PHP uses the SetCookie function to set cookies. One thing that must be noted is that cookies are part of the HTTP protocol header and are used to transfer information between the browser and the server, so the Cookie function must be called before any content belonging to the HTML file itself is output.

The SetCookie function defines a Cookie and appends it to the end of the HTTP header. The prototype of the SetCookie function is as follows:

int SetCookie(string name, string value, int expire, string path, string domain, int secure);
All parameters except name are optional. The three parameters value, path, and domain can be replaced with empty strings, indicating that they are not set; the expire and secure parameters are numerical and can be represented by 0. The expire parameter is a standard Unix time stamp, which can be obtained using the time() or mktime() function, in seconds. The secure parameter indicates whether this cookie is transmitted over the network through the encrypted HTTPS protocol.

The currently set cookie does not take effect immediately, but will not be visible until the next page. This is because the cookie is passed from the server to the client's browser on the set page, and the browser can only save the cookie on the next page. The reason is retrieved from the client's machine and returned to the server.

Setting cookies on the same page is actually from back to front, so if you want to delete a cookie before inserting a new one, you must first write the insertion statement, and then write the deletion statement, otherwise there may be inaccuracies. desired results.

Let’s look at a few examples:

How to create cookies?
The setcookie() function is used to set cookies.

Note: The setcookie() function must be placed before the tag.

Grammar
setcookie(name, value, expire, path, domain);

Simple:

The code is as follows Copy code
 代码如下 复制代码

SetCookie(“MyCookie”, “Value of MyCookie”);

带失效时间的:

SetCookie(“WithExpire”, “Expire in 1 hour”, time()+3600);//3600秒=1小时

什么都有的:

SetCookie(“FullCookie”, “Full cookie value”, time()+3600, “/forum”, “.phpuser.com”, 1);

SetCookie(“MyCookie”, “Value of MyCookie”);

With expiration time:

SetCookie(“WithExpire”, “Expire in 1 hour”, time()+3600);//3600 seconds=1 hour

Everything is available:
 代码如下 复制代码

SetCookie(“CookieArray[]“, “Value 1″);

SetCookie(“CookieArray[]“, “Value 2″);

SetCookie(“CookieArray[0]“, “Value 1″);

SetCookie(“CookieArray[1]“, “Value 2″);

SetCookie(“FullCookie”, “Full cookie value”, time()+3600, “/forum”, “.phpuser.com”, 1);
There is another point to note here. For example, if your site has several different directories, then if you only use cookies without a path, the cookies set in a page in one directory will be in a page in another directory. It is invisible, that is to say, the cookie is path-oriented. In fact, even if the path is not specified, the WEB server will automatically pass the current path to the browser, and specifying the path will force the server to use the set path. The way to solve this problem is to add the path and domain name when calling SetCookie. The format of the domain name can be "www.phpuser.com" or ".phpuser.com". The part representing the value in the SetCookie function will be automatically encoded when passed. In other words, if the value of the value is "test value", it will become "test%20value" when passed, the same as the URL method. . Of course, this is transparent to the program, because PHP automatically decodes the cookie value when it receives it. If you want to set multiple cookies with the same name, use an array. The method is:
The code is as follows Copy code
SetCookie(“CookieArray[]“, “Value 1″); SetCookie(“CookieArray[]“, “Value 2″); or SetCookie(“CookieArray[0]“, “Value 1″); SetCookie(“CookieArray[1]“, “Value 2″);

2. Receive and process Cookies

PHP has very good support for receiving and processing cookies. It is completely automatic and has the same principle as FORM variables. It is very simple.

For example, if you set a cookie named MyCookier, PHP will automatically analyze it from the HTTP header received by the WEB server and form a variable like an ordinary variable named $myCookie. The value of this variable is the cookie. value. The same applies to arrays. Another way is to reference PHP's global variable $HTTP_COOKIE_VARS array.

Examples are as follows: (assuming these have been set in previous pages and are still valid)

The code is as follows Copy code
 代码如下 复制代码

echo $MyCookie;

echo $CookieArray[0];

echo count($CookieArray

echo $MyCookie;


echo $CookieArray[0];

echo count($CookieArray

 代码如下 复制代码

// Print a cookie
echo $_COOKIE["user"];

// A way to view all cookies
print_r($_COOKIE);
?>

How to retrieve the value of Cookie?

PHP's $_COOKIE variable is used to retrieve the value of the cookie.

 代码如下 复制代码


if (isset($_COOKIE["user"]))
echo "Welcome " . $_COOKIE["user"] . "!
";
else
  echo "Welcome guest!
";
?>


In the following example, we retrieve the value of the cookie named "user" and display it on the page:

The code is as follows Copy code

// Print a cookie

echo $_COOKIE["user"];

代码如下 复制代码

// set the expiration date to one hour ago
setcookie("user", "", time()-3600);
?>

// A way to view all cookies

print_r($_COOKIE);

?>

In the following example, we use the isset() function to confirm whether the cookie has been set:
 代码如下 复制代码

function loginCookie($uid,$name,$group,$ip,$time)
{
 global $site_domain,$login_key;
 
 $domain = (substr($site_domain,0,4) == "www.") ? substr($site_domain,3) : ".".$site_domain;

 $secure = Xxtea::encrypt($uid."|".$name."|".$group."|".$ip,$login_key);

 setcookie("userId",$uid,$time+86400,"/",$domain);

 setcookie("userName",$name,$time+86400,"/",$domain);

 setcookie("userGroup",$group,$time+86400,"/",$domain);
  
 setcookie("userSecure",$secure,$time+86400,"/",$domain);
}

The code is as follows Copy code
"; else echo "Welcome guest!
"; ?>
How to delete cookies? When deleting a cookie, you should change the expiry date to a point in time in the past. Example of deletion:
The code is as follows Copy code
// set the expiration date to one hour ago<🎜> setcookie("user", "", time()-3600);<🎜> ?>
Example php cookie to set the user login time and expiration time code
The code is as follows Copy code
function loginCookie($uid,$name,$group,$ip,$time) { global $site_domain,$login_key; $domain = (substr($site_domain,0,4) == "www.") ? substr($site_domain,3) : ".".$site_domain; $secure = Xxtea::encrypt($uid."|".$name."|".$group."|".$ip,$login_key); setcookie("userId",$uid,$time+86400,"/",$domain); setcookie("userName",$name,$time+86400,"/",$domain); setcookie("userGroup",$group,$time+86400,"/",$domain); setcookie("userSecure",$secure,$time+86400,"/",$domain); }

For more details, please see: http://www.bKjia.c0m/phper/18/1b5df18d38cfea1a63282c367f6cdf63.htm

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/628980.htmlTechArticleCookie is a thing that exists in all program development. Let me introduce cookie settings (SetCookie) Delete ( $_COOKIE) login usage. 1. Set Cookie PHP uses SetCookie function to set Cookie...
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