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

What is the difference between cookie and session in PHP?

慕斯
Release: 2023-03-10 16:26:02
Original
2722 people have browsed it

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;);
?>
Copy after login

1.php

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

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

We can also access the cookie data using client-side scripts

<?php
setcookie(&#39;name&#39;,&#39;我喜欢你&#39;);
//header(&#39;Location:1.php&#39;);
?>
<!DOCTYPE html>
<htmL>
<head>
     <meta charset="UTF-8">
     <title>Document</title>
     <script>
            alert (document.cookie) ;
</script>
</head>
<body>
</body>
</html>
Copy after login

Sometimes the cookies on our browser will be disabled. But we also want to transfer data between multiple pages. When we create a new file, we still jump,

<?php
setcookie(&#39;name&#39;,&#39;我喜欢你&#39;);
header("Location:3.php?name=我喜欢你");
?>
Copy after login
<?php
echo $_GET[&#39;name&#39;];
?>
Copy after login

session;

First we write a function session_start(), We use session_id to access, and then we look at the current user's status in the browser,

<?php
session_start();
echo session_id();
?>
Copy after login

How to use session to share between multiple data,

<?php
session_start();
$_SESSION[&#39;name&#39;] = &#39;我喜欢你&#39;;
//echo session_id();
?>
             
<?php
session_start();
echo $_SESSION[I&#39;name&#39; I ;|
?>
Copy after login
<?php
session_start();
$_SESSION[&#39;name&#39;] = &#39;我喜欢你&#39;;
//echo session_id();
header( &#39;Location:1.php&#39; );
?>
Copy after login

What is the difference between cookie and session in PHP?

Recommended learning: php video tutorial

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!

Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template