当前位置:网站首页>PHP Cookie
PHP Cookie
2022-07-22 19:36:00 【I can't understand your code】
cookie Often used to identify users .
Cookie What is it? ?
cookie Often used to identify users .cookie It's a small file that the server leaves on the user's computer . Whenever the same computer requests a page through a browser , This computer will send cookie. adopt PHP, You can create and retrieve cookie Value .
How to create Cookie?
setcookie() Function to set cookie.
notes :setcookie() Function must be in Before the label .
grammar
setcookie(name, value, expire, path, domain);
example 1
In the following example , We're going to create something called "user" Of cookie, And assign it a value "runoob". We also stipulate this cookie Expire in an hour :
<?php
setcookie("user", "runoob", time()+3600);
?>
<html>
.....
notes : Sending cookie when ,cookie The value of will be automatically URL code , Automatically decode when retrieving .( To prevent URL code , Please use setrawcookie() Instead, .)
example 2
You can also set... In another way cookie The expiration time of . This may be simpler than using seconds .
<?php
$expire=time()+60*60*24*30;
setcookie("user", "runoob", $expire);
?>
<html>
.....
In the example above , The expiration time is set to one month (60 second * 60 branch * 24 Hours * 30 God ).
How to retrieve Cookie Value ?
PHP Of $_COOKIE Variables are used to retrieve cookie Value .
In the following example , We got back the name "user" Of cookie Value , And show it on the page :
<?php
// Output cookie value
echo $_COOKIE["user"];
// View all cookie
print_r($_COOKIE);
?>
In the following example , We use isset() Function to confirm whether cookie:
<html>
<head>
<meta charset="utf-8">
<title> Novice tutorial (runoob.com)</title>
</head>
<body>
<?php
if (isset($_COOKIE["user"]))
echo " welcome " . $_COOKIE["user"] . "!<br>";
else
echo " Ordinary visitors !<br>";
?>
</body>
</html>
How to delete Cookie?
When the delete cookie when , You should change the expiration date to a point in the past .
Deleted instance :
<?php
// Set up cookie Expire in the past 1 Hours
setcookie("user", "", time()-3600);
?>
If the browser does not support Cookie What should I do ?
If your application needs and does not support cookie Dealing with your browser , Then you have to use other methods to pass information between pages in your application . One way is to pass data through a form ( About forms and user input , We've covered... In the previous chapters of this tutorial ).
The following form click on the user list "Submit" Button , towards "welcome.php" Submitted user input :
<html>
<head>
<meta charset="utf-8">
<title> Novice tutorial (runoob.com)</title>
</head>
<body>
<form action="welcome.php" method="post">
name : <input type="text" name="name">
Age : <input type="text" name="age">
<input type="submit">
</form>
</body>
</html>
Retrieve "welcome.php" Value in file , As shown below :
<html>
<head>
<meta charset="utf-8">
<title> Novice tutorial (runoob.com)</title>
</head>
<body>
welcome <?php echo $_POST["name"]; ?>.<br>
you <?php echo $_POST["age"]; ?> Year old .
</body>
</html>
边栏推荐
猜你喜欢
2022版Centos8 yum镜像安装&阿里云安装Mysql 5.7教程与问题解决
用指针遍历数组
Introduction to arrays
Laravel solves [1045] access denied for user 'homestead' @ 'localhost' (usin g password: yes)
arguments
2022 centos8 Yum image installation & Alibaba cloud MySQL 5.7 tutorial and problem solving
Nacos persistent connection MySQL database SM4 encryption scheme
【带你学c带你飞】第3章 分支结构(练习3.1 简单的猜数游戏)
Matlab plot子图的间距和边缘距离如何调整(已解决)
“35岁,我退休了”:关于中年危机,这是最靠谱的回答
随机推荐
DOM introduction and query
LeetCode 每日一题 2022/3/7-2022/3/13
JS advanced - basic data type and reference data type
Force deduction solution summary 1260 two dimensional mesh migration
Flutter 第一個程序Hello World!
Renjie, chief scientist of rongyun: experience produces talents, and career "experience > experience"
LeetCode 每日一题 2022/1/17-2022/1/23
Atmospheric environment monitoring scheme for 4G industrial router
Force deduction solution summary 1051 height checker
grafana面板-关于转换
Constructor
"35 years old, I retired": This is the most reliable answer to the midlife crisis
JS advanced - understanding of objects
Force deduction solution summary 648 word replacement
2022版Centos8 yum镜像安装&阿里云安装Mysql 5.7教程与问题解决
Day5:构造程序逻辑
由浅入深详解NLP中的Adapter技术
最强屏幕工具ShareX v14.0.1
Grafana panel - override field values
MySQL implements querying data from other tables and inserting another table