Home > Backend Development > PHP Tutorial > Variables in PHP Sessions

Variables in PHP Sessions

墨辰丷
Release: 2023-03-25 20:34:02
Original
1556 people have browsed it

This article mainly introduces the variable Sessions in PHP. Interested friends can refer to it. I hope it will be helpful to everyone.

Before you can store user information in a PHP session, you must first start the session.

Comments: session_start() The function must be located before the tag:

<?php session_start(); ?>
<html>
<body>
</body>
</html>
Copy after login


#Storing and retrieving Session variables

The correct way to store and retrieve session variables is to use PHP$_SESSION Variable:

<?php
session_start();
// store session data
$_SESSION[&#39;views&#39;]=1;
?>
<html>
<body>
<?php
//retrieve session data
echo "Pageviews=". $_SESSION[&#39;views&#39;];
?>
</body>
</html>
Copy after login



#Destroy Session

If you want to delete some session data, you can use the unset() or session_destroy() function.

unset() function is used to release the specified session variable:

<?php
session_start();
if(isset($_SESSION[&#39;views&#39;]))
unset($_SESSION[&#39;views&#39;]);
?>
Copy after login

Completely destroy the session by calling the

session_destroy() function: ##

<?php
session_destroy();
?>
Copy after login

Comments:

session_destroy( ) The session will be reset and you will lose all stored session data.
E-mail Prevent injection


##FILTER_SANITIZE_EMAIL filter from string Remove illegal characters from email

  • FILTER_VALIDATE_EMAIL Filter validates the value of email address

  • Related recommendations:

33 PHP Sessions

PHP Sessions

HTMl5 storage methods sessionStorage and localStorage detailed explanation

The above is the detailed content of Variables in PHP Sessions. 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