PHP session control example analysis

墨辰丷
Release: 2023-03-28 10:26:02
Original
1193 people have browsed it

This article mainly introduces PHP session control, and analyzes PHP's related operating techniques for cookies and sessions in the form of examples. Friends in need can refer to it

The details are as follows:

About Test code for cookie and session:

<?php
session_start();
define(&#39;u&#39;,&#39;a&#39;);
define(&#39;p&#39;,&#39;1&#39;);
if (isset($_GET[&#39;r&#39;]) && $_GET[&#39;r&#39;]== 1) {
  unset($_COOKIE[&#39;username&#39;]);
  unset($_COOKIE[&#39;password&#39;]);
  unset($_SESSION[&#39;valid_login&#39;]);
}
if (isset($_POST[&#39;username&#39;]) && isset($_POST[&#39;password&#39;])) {
  $username = $_POST[&#39;username&#39;];
  $password = $_POST[&#39;password&#39;];
  if ($username == u && $password == p) {
    $_SESSION[&#39;valid_login&#39;] = 1;
    setcookie(&#39;username&#39;, $username);
    setcookie(&#39;password&#39;, $password);
  } else {
    echo &#39;incorrect u or p&#39;;
  }
}
?>
<?php
if (isset($_SESSION[&#39;valid_login&#39;])) {
  printf(&#39;welcom, %s&#39;, $_COOKIE[&#39;username&#39;]);
} else {
?>
<form action="" method="post">
  <input type="text" name="username" value="" />
  <input type="password" name="password" value="" />
  <input type="submit" />
</form>
<?php
}
?>
<br />
<a href="cookie_session.php" title="">refresh</a>
<br />
<a href="cookie_session.php?r=1" title="">clear</a>
Copy after login

The above is the entire content of this article, I hope it will be helpful to everyone's learning.


Related recommendations:

php implements the message board function based on session control

PHP'sSession control

##PHPSession controlDetailed explanation of cookies

The above is the detailed content of PHP session control example analysis. 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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!