Cookies in PHP are no different from those in other programs. Cookies are used to store information on the client. They are commonly used in some applications with low security requirements, such as users logging in and remembering passwords. Below I Let me introduce to you the php cookie study notes.
PHP 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 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 is automatically created containing the cookie's value.
The cookie must be assigned before any other output is sent. The function returns true if successful, false otherwise.
1 setcookie(name, value, expire, path, domain, secure)
•name required. Specifies the name of the cookie.
•value required. Specifies the value of the cookie.
•expire Optional. Specifies the validity period of the cookie.
•path optional. Specifies the server path for cookies.
•domain optional. Specifies the domain name for the cookie.
•secure Optional. Specifies whether cookies are transmitted over a secure HTTPS connection.
The value of the cookie named "user" can be accessed via $HTTP_COOKIE_VARS["user"] or $_COOKIE["user"]. 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.
Example, php setting and getting cookies
The code is as follows | Copy code | ||||||||
//Function prototype: int setcookie(string name, string value, int expire, string path, string domain, int secure)
echo($mycookie);
|
Delete Cookies
代码如下 | 复制代码 |
setcookie('mycookie','',time()-3600); |
(2) Make the expiration time be time() or time-1;
The code is as follows | Copy code | ||||
setcookie('mycookie'); or setcookie('mycookie',''); or setcookie("mycookie",false);
|
The code is as follows | Copy code |
setcookie('mycookie','',time()-3600); |
The code is as follows | Copy code |
$y2k = mktime(0,0,0,1,1,2000); setcookie('name','value',$y2k); setcookie('name', 'value', time+3600); setcookie('name', 'value', $y2k, '~/myhome', '.domain.com'); |
How to get COOKIE expiration time
The code is as follows | Copy code | ||||
setcookie ("var_name", "var_value", $expire); // Set a cookie named var_name and set the validity period setcookie ("var_name_expire", $expire, $expire); // Set the expiration time into the cookie so that you can know the expiration time of var_name
|
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.
For example, cookie to save user login information
代码如下 | 复制代码 |
//数据库的位置 |
The code is as follows | Copy code |
//Database location<🎜> define('DB_HOST', 'localhost');<🎜> //Username<🎜> define('DB_USER', 'root');<🎜> //Password<🎜> define('DB_PASSWORD', '19900101');<🎜> //Database name<🎜> define('DB_NAME','test') ;<🎜> ?> |
2. Login page: logIn.php
The code is as follows | Copy code |
//Insert relevant information about connecting to the database $error_msg = ""; if(!empty($user_username)&&!empty($user_password)){ & Lt;!-Through the $ __Cookie ['user_id'], it is judged. If the user fails to log in, the login form is displayed so that the user enters the username and password-& gt; If(empty($_COOKIE['user_id'])){ echo ' '.$error_msg.' ';?> } ?> |
3、登入页面:loged.php
代码如下
|
复制代码
|
||||||||
//已登录页面,显示登录用户名
if(isset($_COOKIE['username'])){
'; //点击“Log Out”,则转到logOut.php页面进行cookie的注销 echo ' Log Out('.$_COOKIE['username'].')'; } /**In the logged in page, you can use the user's cookies such as $_COOKIE['username'], * $_COOKIE['user_id'] queries the database and can do many things*/ ?> 4、注销cookie页面:logOut.php(注销后重定向到lonIn.php)
Previous article:Some basic knowledge of session in php_PHP tutorial
Next article:Detailed explanation of polymorphism in PHP object-oriented development_PHP tutorial
Statement of this Website
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
Latest Articles by Author
Latest Issues
Group MySQL results by ID for looping over
I have a table with flight data in mysql. I'm writing a php code that will group and displ...
From 2024-04-06 17:27:56
0
1
406
Related Topics
More>
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
|