应用Session变量控制用户登录时间

1.创建conn.php文件,实现连接数据库的操作

  1. <?php  
  2. $dbhost='localhost';//数据库服务器名称  
  3. $dbuser='root';// 连接数据库用户名  
  4. $dbpass='';// 连接数据库密码  
  5. $dbname='online';// 数据库的名字  
  6. // 连接到数据库  
  7. error_reporting(E_ALL ^ E_DEPRECATED);//解决报错问题  
  8. $connect=mysql_connect($dbhost,$dbuser,$dbpass);  
  9. if(!$connect) exit('数据库连接失败!');  
  10. mysql_select_db($dbname,$connect);  
  11. mysql_query('set names gbk');//设置编码  
  12. ?>  


2.创建登录页面login.php,在页面中创建表单及表单元素,在该页面使用javascript脚本对用户输入的用户名和密码进行验证

[html] view plain copy
  1. <?php   
  2. ?>  
  3. <html>  
  4. <script type="text/javascript">  
  5.     function checkform(form){  
  6.         if(form.user.value==""){  
  7.             alert('请输入用户名');  
  8.             form.user.focus();  
  9.         return false;  
  10.         }  
  11.         if(form.pwd.value==""){  
  12.             alert('请输入密码');  
  13.             form.pwd.focus();  
  14.         return false;  
  15.         }  
  16.     }  
  17. </script>  
  18. <form id="form1" name="form1" method="POST" action="login_ok.php" onsubmit="return checkform(form1)">  
  19.     <fieldset style="500px;"><legend style="font-size: 16px;">用户登录</legend><table width="300" border="0" align="center">  
  20.         <tr>  
  21.            <td width="77" align="right">用户名:</td>  
  22.            <td width="213"<input type="text" name="user" id="user"size="24"></td>  
  23.          </tr>  
  24.          <tr>  
  25.             <td align="right">密码:</td>  
  26.             <td><input name="pwd" type="password" id="pwd" size="25"></td>  
  27.          </tr>  
  28.          <tr>  
  29.             <td</td>  
  30.             <td><input type="submit" name="sub" value="登录">  
  31.             <input type="reset" name="res" value="重置"></td>  
  32.     </table>  
  33.     </fieldset>  
  34. </form>  
  35. </html>  

3.创建login_ok.php页面,在页面中通过查询数据库来判断用户名密码是否正确,如果正确,则为Session变量赋值

[html] view plain copy
  1. <?php  
  2.     error_reporting(0);  
  3.     session_start();   //开启session  
  4.     header("content-type:text/html;charset=utf-8");//设置编码格式  
  5.     include 'conn.php';    //包含数据库连接文件  
  6.     $name=$_POST['user'];  
  7.     $pwd=$_POST['pwd'];  
  8.     $sql=mysql_query("select *from information where name='".$name."' and passworld='".$pwd."'");//执行sql语句判断数据库内是否含有   
  9.     if(mysql_num_rows($sql)>0){  
  10.         $_SESSION['name']=$name;  
  11.         $_SESSION['time']=time();  
  12.         echo "<script> alert('登录成功!'); location='index.php'</script>";   //提示登录成功  
  13.     }   
  14.     else{  
  15.         echo "<script> alert('用户名密码错误!'); location='login.php'</script>";  //提示用户名密码错误  
  16.     }  
  17. ?>  


4.创建index.php页面,在页面中进行判断,如果Session变量有值且登录时间没有超过10分钟显示欢迎回来,如果十分钟内未操作,则显示登录超时,请重新登录

[html] view plain copy
  1. <?php  
  2.      error_reporting(0);  
  3.      session_start();  
  4.      if($_SESSION['time']==""){  
  5.          echo "<script> alert('您无权限查看,请登录!'); location='login.php'</script>";  //不允许直接访问  
  6.      }  
  7.      elseif((time()-$_SESSION['time'])<60){    //如果登录时间没有超过1分钟  
  8.          $_SESSION['time']=time();              //白当前的时间戳赋值给Session变量  
  9.          
  10. ?>  
  11.   
  12. <table width="469" border="0" align="center">  
  13.     <tr>  
  14.         <td colspan="3"><img src="../courseplay/images/close.png"  width="464" height="139"/></td>  
  15.     </tr>  
  16.     <tr>  
  17.          <td width="81"><img src="../courseplay/images/close.png" width="78" height="136"></td>  
  18.          <td width="301" align="center" style="font-size:24px; color:#CC00CC; font-weight:bolder"> 欢迎来到学涯在线</td>  
  19.          <td width="74"><img src="../courseplay/images/close.png" width="74" height="136"></td>  
  20.     </tr>  
  21.     <tr>  
  22.          <td height="63" colspan="3"><img src="../courseplay/images/close.png" width="464" height="61"></td>  
  23.     </tr>  
  24. </table>  
  25. <?php  
  26.     }   
  27.     else {  //一分钟内未登录,提示重新登录  
  28. ?>  
  29.     }  
  30.         <table width="469" border="0" align="center">  
  31.         <tr>  
  32.         <td colspan="3"><img src="../courseplay/images/close.png" width="464" height="139"/></td>  
  33.         </tr>  
  34.         <tr>  
  35.         <td width="81"><img src="../courseplay/images/close.png" width="78" height="136"></td>  
  36.         <td width="301" align="center" style="font-size:24px; color:#339966; font-weight:bolder">您登录已超时,请重新登陆!</td>  
  37.         <td width="74"><img src="../courseplay/images/close.png" width="74" height="136"></td>  
  38.         </tr>  
  39.         <tr>  
  40.         <td height="63" colspan="3"><img src="../courseplay/images/close.png" width="464" height="61"></td>  
  41.         </tr>  
  42.         </table>  
  43.          
  44. <?php  
  45.     echo "<script>location.href='login.php'</script>";  
  46.     }   
  47. ?>  


这是今天的一个小例子哈.....

原文地址:https://www.cnblogs.com/haohaoyuan/p/7797820.html