php $_SESSION会员登录实例分享

yipeiwu_com5年前PHP代码库

php会员登录模块是网站开发中很简单的一个模块,本实例主要给php初学者一个简单的参考,其中的逻辑还是要读者自己领会,多编多思考。
login.php文件

<?php
  ob_start();
  session_start();
?>
<?
  // error_reporting(E_ALL);
  // ini_set("display_errors", 1);
?>
<html lang="en"> 
  <head>
   <title>Tutorialspoint.com</title>
   <link href="css/bootstrap.min.css" rel="stylesheet">   
   <style>
     body {
      padding-top: 40px;
      padding-bottom: 40px;
      background-color: #ADABAB;
     }     
     .form-signin {
      max-width: 330px;
      padding: 15px;
      margin: 0 auto;
      color: #017572;
     }     
     .form-signin .form-signin-heading,
     .form-signin .checkbox {
      margin-bottom: 10px;
     }     
     .form-signin .checkbox {
      font-weight: normal;
     }     
     .form-signin .form-control {
      position: relative;
      height: auto;
      -webkit-box-sizing: border-box;
      -moz-box-sizing: border-box;
      box-sizing: border-box;
      padding: 10px;
      font-size: 16px;
     }     
     .form-signin .form-control:focus {
      z-index: 2;
     }     
     .form-signin input[type="email"] {
      margin-bottom: -1px;
      border-bottom-right-radius: 0;
      border-bottom-left-radius: 0;
      border-color:#017572;
     }     
     .form-signin input[type="password"] {
      margin-bottom: 10px;
      border-top-left-radius: 0;
      border-top-right-radius: 0;
      border-color:#017572;
     }     
     h2{
      text-align: center;
      color: #017572;
     }
   </style>   
  </head>  
  <body>   
   <h2>Enter Username and Password</h2> 
   <div class="container form-signin">     
     <?php
      $msg = '';      
      if (isset($_POST['login']) && !empty($_POST['username']) && !empty($_POST['password'])) {        
        if ($_POST['username'] == 'tutorialspoint' && $_POST['password'] == '1234') {
         $_SESSION['valid'] = true;
         $_SESSION['timeout'] = time();
         $_SESSION['username'] = 'tutorialspoint';
         echo 'You have entered valid use name and password';
        }
        else 
        {
         $msg = 'Wrong username or password';
        }
      }
     ?>
   </div> <!-- /container -->   
   <div class="container">   
     <form class="form-signin" role="form" action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>" method="post">
      <h4 class="form-signin-heading"><?php echo $msg; ?></h4>
      <input type="text" class="form-control" name="username" placeholder="username = tutorialspoint" required autofocus></br>
      <input type="password" class="form-control" name="password" placeholder="password = 1234" required>
      <button class="btn btn-lg btn-primary btn-block" type="submit" name="login">Login</button>
     </form>      
     Click here to clean <a href="logout.php" tite="Logout">Session.    
   </div>    
  </body>
</html>

Logout.php文件

<?php
  session_start();
  unset($_SESSION["username"]);
  unset($_SESSION["password"]); 
  echo 'You have cleaned session';
  header('Refresh: 2; URL=login.php');
?>

以上就是本文的全部内容,希望对大家的学习有所帮助。

相关文章

PHP的邮件群发系统phplist配置方法详细总结

本文实例讲述了PHP的邮件群发系统phplist配置方法。分享给大家供大家参考,具体如下: phplist是一个十分不错的邮件群发系统,让我们可以很方便地群发电子杂志、小广告(^_^)等...

PHP Ajax实现无刷新附件上传

对一个网站而言,有一个基本的不可缺少的功能,那就是文件上传。使用PHP预压来实现文件上传可谓是有得天独厚的优势的,那么今天,就一起来做一个关于无刷新实现的文件上传吧。 ---------...

封装一个PDO数据库操作类代码

复制代码 代码如下:<?php /**  * 数据库PDO操作  */ class MysqlPdo { public static $PDOStatement = null; /...

PHP中函数gzuncompress无法使用的解决方法

前言 gzuncompress函数不是php自带的系统函数而是一个插件函数了所以要使用 gzuncompress函数我们必须安装一个插件,下面来看看PHP函数gzuncompress无法...

php匹配字符中链接地址的方法

本文实例讲述了php匹配字符中链接地址的方法。分享给大家供大家参考。具体如下: 判断一个字符串是否含有超级链接,代码如下: 复制代码 代码如下:$str="ssdsf<a targ...