Home  >  Article  >  Backend Development  >  PHP session control example analysis

PHP session control example analysis

墨辰丷
墨辰丷Original
2018-05-29 10:58:131212browse

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>

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!

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