当前位置:首页 > PHP教程 > php应用 > 列表

php $_SESSION会员登录实例分享

发布:smiling 来源: PHP粉丝网  添加日期:2019-11-05 15:39:49 浏览: 评论:0 

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

login.php文件

  1. <?php 
  2.  
  3.   ob_start(); 
  4.  
  5.   session_start(); 
  6.  
  7. ?> 
  8.  
  9. <? 
  10.  
  11.   // error_reporting(E_ALL); 
  12.  
  13.   // ini_set("display_errors", 1); 
  14.  
  15. ?> 
  16.  
  17.    
  18.  
  19.     
  20.  
  21.    <title>Tutorialspoint.com</title> 
  22.  
  23.    <link href="css/bootstrap.min.css" rel="stylesheet">    
  24.  
  25.    <style> 
  26.  
  27.      body { 
  28.  
  29.       padding-top: 40px; 
  30.  
  31.       padding-bottom: 40px; 
  32.  
  33.       background-color: #ADABAB; 
  34.  
  35.      }      
  36.  
  37.      .form-signin { 
  38.  
  39.       max-width: 330px; 
  40.  
  41.       padding: 15px; 
  42.  
  43.       margin: 0 auto; 
  44.  
  45.       color: #017572; 
  46.  
  47.      }      
  48.  
  49.      .form-signin .form-signin-heading, 
  50.  
  51.      .form-signin .checkbox { 
  52.  
  53.       margin-bottom: 10px; 
  54.  
  55.      }      
  56.  
  57.      .form-signin .checkbox { 
  58.  
  59.       font-weight: normal; 
  60.  
  61.      }      
  62.  
  63.      .form-signin .form-control { 
  64.  
  65.       position: relative; 
  66.  
  67.       height: auto; 
  68.  
  69.       -webkit-box-sizing: border-box; 
  70.  
  71.       -moz-box-sizing: border-box; 
  72.  
  73.       box-sizing: border-box; 
  74.  
  75.       padding: 10px; 
  76.  
  77.       font-size: 16px; 
  78.  
  79.      }      
  80.  
  81.      .form-signin .form-control:focus { 
  82.  
  83.       z-index: 2; 
  84.  
  85.      }      
  86.  
  87.      .form-signin input[type="email"] { 
  88.  
  89.       margin-bottom: -1px; 
  90.  
  91.       border-bottom-right-radius: 0; 
  92.  
  93.       border-bottom-left-radius: 0; 
  94.  
  95.       border-color:#017572; 
  96.  
  97.      }      
  98.  
  99.      .form-signin input[type="password"] { 
  100.  
  101.       margin-bottom: 10px; 
  102.  
  103.       border-top-left-radius: 0; 
  104.  
  105.       border-top-right-radius: 0; 
  106.  
  107.       border-color:#017572; 
  108.  
  109.      }      
  110.  
  111.      h2{ 
  112.  
  113.       text-align: center; 
  114.  
  115.       color: #017572; 
  116.  
  117.      } 
  118.  
  119.    </style>    
  120.  
  121.       
  122.  
  123.        
  124.  
  125.    <h2>Enter Username and Password</h2>  
  126.  
  127.    <div class="container form-signin">      
  128.  
  129.      <?php 
  130.  
  131.       $msg = '';       
  132.  
  133.       if (isset($_POST['login']) && !emptyempty($_POST['username']) && !emptyempty($_POST['password'])) {         
  134.  
  135.         if ($_POST['username'] == 'tutorialspoint' && $_POST['password'] == '1234') { 
  136.  
  137.          $_SESSION['valid'] = true; 
  138.  
  139.          $_SESSION['timeout'] = time(); 
  140.  
  141.          $_SESSION['username'] = 'tutorialspoint'
  142.  
  143.          echo 'You have entered valid use name and password'
  144.  
  145.         } 
  146.  
  147.         else 
  148.  
  149.         { 
  150.  
  151.          $msg = 'Wrong username or password'
  152.  
  153.         } 
  154.  
  155.       } 
  156.  
  157.      ?> 
  158.  
  159.    </div> <!-- /container -->    
  160.  
  161.    <div class="container">    
  162.  
  163.      <form class="form-signin" role="form" action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>" method="post"
  164.  
  165.       <h4 class="form-signin-heading"><?php echo $msg; ?></h4> 
  166.  
  167.       <input type="text" class="form-control" name="username" placeholder="username = tutorialspoint" required="" autofocus=""><br> 
  168.  
  169.       <input type="password" class="form-control" name="password" placeholder="password = 1234" required=""
  170.  
  171.       <button class="btn btn-lg btn-primary btn-block" type="submit" name="login">Login</button> 
  172.  
  173.      </form>       
  174.  
  175.      Click here to clean <a href="logout.php" tite="Logout">Session.     
  176.  
  177.    </a></div><a href="logout.php" tite="Logout">     
  178.  
  179.    //phpfensi.com 
  180.  
  181. </a> 

Logout.php文件

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

Tags: $_SESSION会员登录

分享到: