Home  >  Article  >  Backend Development  >  What is the difference between cookie and session in PHP?

What is the difference between cookie and session in PHP?

慕斯
慕斯Original
2021-06-16 15:29:222666browse

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.

What is the difference between cookie and session in PHP?

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(&#39;name&#39;,&#39;我喜欢你&#39;);
header(&#39;Location:1.php&#39;);
?>

1.php

<?php
echo $_COOKIE[&#39;name&#39;];
?>

Running results:

What is the difference between cookie and session in PHP?

When we write in html, we can get some results. First, we write a piece of HTML code.

In

we can write a

The 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!

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