php实现登陆限制,只允许一台机器在线
使用session 跟file控制
[php]
<?php
ob_start();
session_start();
class cc{
// private $refurl="http://www.abc.com"; //登陆成功后转换页面
private $loginflag=false; //登陆成功标志
private $loginfile="loginmsg.txt"; //信息文件
function setflag($flag){
$this->loginflag=$flag;
}
function check(){
global $_post;
if(file_exists($this->loginfile)&& (time()-filemtime($this->loginfile))<60){//用户登陆存在而且有效
$info=file($this->loginfile);
$username=trim($info[0]); //已在线的登陆用户名
$password=trim($info[1]); //已在线密码(可以不保存)
$ip =trim($info[2]); //已在线ip
$sid =trim($info[3]); //已在线port
if(strcmp($_SESSION['loginuser'],$username)==0){
if(strcmp($_SESSION['loginpw'],$password)==0){
if(strcmp($_SERVER['REMOTE_ADDR'],$ip)==0){
if(strcmp(session_id(),$sid)==0){
$this->setflag(true);
echo "<h3>您的帐户可以确定是唯一的!</h3>";
$cf=fopen($this->loginfile,"a+");
fputs($cf,"\r\na");
fclose($cf);
echo "<meta http-equiv=refresh content=\"10;url=index.php\">";
//echo "<iframe src=\"t.php\" frameborder=0 width=0 height=0></iframe>";
}else{
echo "不允许帐户在局网上同时登陆.. ".$_SERVER[REMOTE_PORT];
$this->loginflag=false;
}
}else{
echo "不允许使用帐户同时登陆..<br>";
$this->loginflag=false;
}
}else{
echo "密码错误..<br>";
$this->loginflag=false;
}
}else{
//这里验证身份如果正确则
echo "帐户登陆时发生错误!用户名错误<br><pre>";
}
}else{
$_post =$_POST;
var_dump($_post);
if(isset($_post['loginuser'])){
$_SESSION['loginuser']=$_post['loginuser'];
$_SESSION['loginpw']=$_post['loginpw'];
$fp=fopen($this->loginfile,"w");
$msg=$_post['loginuser']."\r\n".$_post['loginpw']."\r\n".$_SERVER['REMOTE_ADDR']
."\r\n".session_id();;
fputs($fp,$msg);
fclose($fp);
}else{
$outtime=time()-filemtime($this->loginfile)-60;
echo "登陆不存在或您已经超时(".$outtime."秒)...";
}
}
}
function wfrom(){
global $_post;
if(!file_exists($this->loginfile) ||(time()-filemtime($this->loginfile))>60){ //登陆失败
echo <<<loginform
<form action="$_SERVER[PHP_SELF]" method="post" name="loginform">
<table cellpadding=0 border=0>
<tr><td>
用户名:<td><input type="text" name="loginuser"></span><br>
<tr><td>密码:<td><input type="password" name="loginpw"></span><br>
<tr><td> <td><input type="button" value=" login " onclick="if(this.form.loginuser.length*this.form.loginpw.length!=0){this.form.submit();}else{return false;}">
</table>
</form>
loginform;
}else{
echo "已有用户登陆";
}
}
}
$d=new cc;
$d->check();
$d->wfrom();
?>
[/php]