以下是我的登陆页面的代码:<?php
session_start();
include("conn/config.php");if($_POST["username"])
{
$name=$_POST["username"];
$password=$_POST["password"];
$sql="select * from user where user_name='$name' and user_psw='$password'";
$query=mysql_query($sql);
$result=mysql_fetch_array($query);
if($result)
{
$_SESSION["user"]=$name;
$_SESSION["id"]=$result['user_id'];
if($_POST["code"]!=$_SESSION["authnum"]){echo "<script>alert('验证码错误,请重新输入!');location.href='login.php'</script>"; }
else{
echo "<script>alert('登陆成功');location.href='homepage.php'</script>";}
}
else
{
echo "<script>alert('登陆失败');location.href='login.php'</script>";
}
}
?><!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=gb2312" />
<title>立信羊城会计事务所</title>
<link href="css/style.css" rel="stylesheet" type="text/css" />
<script language="javascript">
function CheckForm(){
if (document.form.username.value==""){
alert("请填写用户名!");
return false;
}
if (document.form.password.value==""){
alert("请填写密码!");
return false;
}
return true;
}
</script>
</head><body style="margin:0;">
<div id="container">
<div id="blank"></div>
<div id="login">
<form action="login.php" name="form" method="post">
<table width="316" height="229" cellspacing="10" style="font-weight:bold;font-size:19px">
<tr>
<td width="66" height="46">用户名</td>
<td colspan="2"><input type="text" name="username" id="username" size=30 height="20" width="180"/></td>
</tr>
<tr>
<td height="50">密 码 </td>
<td colspan="2"><input type="password" name="password" id="password" size=32.5 height="20" width="180"/></td>
</tr>
<tr>
<td height="45">验证码</td>
<td width="96"><input type=text name="code" id="code" size="19"></td>
<td width="106"><img src=image.php><input type=hidden name=init value=1 ></td>
</tr>
<tr>
<td> </td>
<td height="36" ><input type="image" src="picture/login.png" name="submit" onclick="return CheckForm();" /></td>
<td height="36" align="left"><input type="image" src="picture/reset.png" name="submit2" onclick="reset();return false;" /></td>
</tr>
</table>
</form>
</div>
</div></body>
</html>应该如何修改,使得同一个账号不能同时使用?
session_start();
include("conn/config.php");if($_POST["username"])
{
$name=$_POST["username"];
$password=$_POST["password"];
$sql="select * from user where user_name='$name' and user_psw='$password'";
$query=mysql_query($sql);
$result=mysql_fetch_array($query);
if($result)
{
$_SESSION["user"]=$name;
$_SESSION["id"]=$result['user_id'];
if($_POST["code"]!=$_SESSION["authnum"]){echo "<script>alert('验证码错误,请重新输入!');location.href='login.php'</script>"; }
else{
echo "<script>alert('登陆成功');location.href='homepage.php'</script>";}
}
else
{
echo "<script>alert('登陆失败');location.href='login.php'</script>";
}
}
?><!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=gb2312" />
<title>立信羊城会计事务所</title>
<link href="css/style.css" rel="stylesheet" type="text/css" />
<script language="javascript">
function CheckForm(){
if (document.form.username.value==""){
alert("请填写用户名!");
return false;
}
if (document.form.password.value==""){
alert("请填写密码!");
return false;
}
return true;
}
</script>
</head><body style="margin:0;">
<div id="container">
<div id="blank"></div>
<div id="login">
<form action="login.php" name="form" method="post">
<table width="316" height="229" cellspacing="10" style="font-weight:bold;font-size:19px">
<tr>
<td width="66" height="46">用户名</td>
<td colspan="2"><input type="text" name="username" id="username" size=30 height="20" width="180"/></td>
</tr>
<tr>
<td height="50">密 码 </td>
<td colspan="2"><input type="password" name="password" id="password" size=32.5 height="20" width="180"/></td>
</tr>
<tr>
<td height="45">验证码</td>
<td width="96"><input type=text name="code" id="code" size="19"></td>
<td width="106"><img src=image.php><input type=hidden name=init value=1 ></td>
</tr>
<tr>
<td> </td>
<td height="36" ><input type="image" src="picture/login.png" name="submit" onclick="return CheckForm();" /></td>
<td height="36" align="left"><input type="image" src="picture/reset.png" name="submit2" onclick="reset();return false;" /></td>
</tr>
</table>
</form>
</div>
</div></body>
</html>应该如何修改,使得同一个账号不能同时使用?
转前辈的帖子:挤掉上一个已登陆的用户:
用户表加一个字段(last_session), 或加一个新表(user_id, last_session)
当同一个用户登入时
取得上一次的session id 并删除该id的内容
再更新成新的 session id 提示该帐号已经登陆:
用户表加2个字段(last_access,last_session), 或加一个新表(user_id,last_access ,last_session)
当同一个用户登入时
取得上一次的last_access time,session id
如果最后存取时间过久, 清除该session , 换新的
否则提示已登入 强行挤掉上一个会比较简单及少资源
第二个方法如果想用户体验比较好的话,你还需要用ajax 定时送keepalive