Home >Backend Development >PHP Problem >What is the difference between cookie and session in PHP?
The previous article introduced you to "How to use bubble sorting in PHP? 》, this article continues to introduce to you what is the difference between cookie and session in PHP? It has certain reference value. Friends in need can refer to it. I hope it will be helpful to everyone.
The difference between cookie and session:
For PHP session management,
cookie: data stored in browsing On the server side, the characteristics are: convenient for exchanging data with javascript; convenient for obtaining user information; risks: cookies may be disabled when browsing; alternatives: url parameters;
session: data is stored on the server; features: efficient, safe, It does not rely on the browser environment. The server will identify each user with an ID;
For the specific differences between the two, we take the code as an example:
First we write a function (setcookie), then define a name in the function, and then we need to access this data through another page. At this time, we need to create a file and output (echo) $COOKIE['name'] in this file. , we use hearder to jump on the first page,
2.php
<?php setcookie('name','我喜欢你'); header('Location:1.php'); ?>
1.php
<?php echo $_COOKIE['name']; ?>
Running results:
When we write in html, we can get some results. First, we write a piece of HTML code.
In
we can write aThe above is the detailed content of What is the difference between cookie and session in PHP?. For more information, please follow other related articles on the PHP Chinese website!