一 代码
conn.php
<?php $conn = mysql_connect("localhost", "root", "root") or die("连接数据库服务器失败!".mysql_error()); //连接MySQL服务器 mysql_select_db("db_database17",$conn); //选择数据库db_database13 mysql_query("set names utf8"); //设置数据库编码格式utf8 ?>
login.php
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <script type="text/javascript"> function checkform(form){//检测表单内容是否为空 if(form.user.value==""){ alert("请输入用户名"); form.user.focus(); return false; } if(form.pwd.value==""){ alert("请输入密码"); form.pwd.focus(); return false; } } </script> <form id="form1" name="form1" method="post" action="login_ok.php" onsubmit="return checkform(form1)"> <fieldset style="width:500px"><legend style="font-size:16px">用户登录</legend><table width="300" border="0" align="center"> <tr> <td width="77" align="right">用户名:</td> <td width="213"><input name="user" type="text" id="user" size="24" /></td> </tr> <tr> <td align="right">密码:</td> <td><input name="pwd" type="password" id="pwd" size="25" /></td> </tr> <tr> <td> </td> <td><input type="submit" name="sub" value="登录" /> <input type="reset" name="res" value="重置" /></td> </tr> </table> </fieldset> </form>
login_ok.php
<?php session_start();//开启SESSION header("content-type:text/html;charset=utf-8");//设置编码格式 include("conn/conn.php");//包含数据库连接文件 $name=$_POST['user']; $pwd=$_POST['pwd']; $sql=mysql_query("select * from tb_member where name='".$name."' and password='".$pwd."'");//执行sql语句 if(mysql_num_rows($sql)>0){//判断数据库中是否有记录 $_SESSION['name']=$name;//为SESSION变量赋值 $_SESSION['time']=time();//为SESSION变量赋值 echo "<script>alert('登录成功!');location='show.php';</script>";//提示登录成功 }else{ echo "<script>alert('用户名或密码错误!');location='login.php';</script>";//提示用户名或密码错误 } ?>
show.php
<?php session_start();?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>学涯在线</title> </head> <body> <?php if($_SESSION['time']==""){//判断SESSION变量是否为空 echo "<script>alert('您无权限查看本页面,请先登录!');location='login.php';</script>";//不允许直接登录 }elseif((time()-$_SESSION['time'])<60){//如果登录时间没有超过1分钟 $_SESSION['time']=time();//把当前时间戳赋给SESSION变量 ?> <table width="469" border="0" align="center"> <tr> <td colspan="3"><img src="images/mysql_01.gif" width="464" height="139" /></td> </tr> <tr> <td width="81"><img src="images/mysql_02.gif" width="78" height="136" /></td> <td width="301" align="center" style="font-size:24px; color:#CC00CC; font-weight:bolder">欢迎来到学涯在线!</td> <td width="74"><img src="images/mysql_04.jpg" width="74" height="136" /></td> </tr> <tr> <td height="63" colspan="3"><img src="images/mysql_05.gif" width="464" height="61" /></td> </tr> </table> <?php }else{//如果登录时间超过10分钟并且10分钟内没有刷新页面则提示登录超时 ?> <table width="469" border="0" align="center"> <tr> <td colspan="3"><img src="images/mysql_01.gif" width="464" height="139" /></td> </tr> <tr> <td width="81"><img src="images/mysql_02.gif" width="78" height="136" /></td> <td width="301" align="center" style="font-size:24px; color:#339966; font-weight:bolder">您已登录超时!请重新登录!</td> <td width="74"><img src="images/mysql_04.jpg" width="74" height="136" /></td> </tr> <tr> <td height="63" colspan="3"><img src="images/mysql_05.gif" width="464" height="61" /></td> </tr> </table> <?php } ?> </body> </html>
二 运行结果
在登录1分钟内,如果没有刷新动作,将会提示超时