php $_SESSION会员登录实例分享

所属分类: 网络编程 / PHP编程 阅读数: 1912
收藏 0 赞 0 分享

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实现的美国50个州选择列表实例

这篇文章主要介绍了php实现的美国50个州选择列表实例,可实现让当前州为选中状态的功能,非常具有实用价值,需要的朋友可以参考下
收藏 0 赞 0 分享

PHP使用递归生成文章树

写递归函数,可考虑缓存,定义一些静态变量来存上一次运行的结果,多程序运行效率很有帮助.大概步骤如下:首先到数据库取数据,放到一个数组,然后把数据转化为一个树型状的数组,最后把这个树型状的数组转为html代码。下面我们来看个实例
收藏 0 赞 0 分享

wordpress安装过程中遇到中文乱码的处理方法

这篇文章主要介绍了wordpress安装过程中遇到中文乱码的处理方法,是个人项目中遇到的一个奇葩事件,经过一番研究,终于解决,这里记录下来分享给大家,有需要的小伙伴可以参考下。
收藏 0 赞 0 分享

php的crc32函数使用时需要注意的问题(不然就是坑)

这篇文章主要介绍了php的crc32函数使用时需要注意的问题(不然就是坑) ,需要的朋友可以参考下
收藏 0 赞 0 分享

PHP中把对象转换为关联数组代码分享

这篇文章主要介绍了PHP中把对象转换为关联数组代码分享,本文直接给出实现代码,需要的朋友可以参考下
收藏 0 赞 0 分享

php检测url是否存在的方法

这篇文章主要介绍了php检测url是否存在的方法,涉及php中get_headers及正则匹配的技巧,非常具有实用价值,需要的朋友可以参考下
收藏 0 赞 0 分享

php获取twitter最新消息的方法

这篇文章主要介绍了php获取twitter最新消息的方法,涉及php操作curl及正则替换的技巧,非常具有实用价值,需要的朋友可以参考下
收藏 0 赞 0 分享

php遍历CSV类实例

这篇文章主要介绍了php遍历CSV类,实例分析了php针对csv文件的打开、读取及遍历的技巧,非常具有实用价值,需要的朋友可以参考下
收藏 0 赞 0 分享

PHP使用mysqldump命令导出数据库

最近用php写一个数据备份的功能。做法是使用php的system函数执行mysqldump命令,进行备份,这里分享给大家,有需要的小伙伴可以参考下。
收藏 0 赞 0 分享

PHP用反撇号执行外部命令

shell_exec() 命令行实际上仅是反撇号 ` 操作符的变体,如果您编写过 shell 或 Perl 脚本,您就知道可以在反撇号操作符内部捕捉其他命令的输出。
收藏 0 赞 0 分享
查看更多