PHP代码设置用户登陆限制怎么办?
$_SESSION['num'] += 1; if($_SESSION['num'] >= 3){此处操作数据库 帐号状态改为锁定 并记录当前锁定时间。
if($_SESSION[$ip] < 3){
if(isset($_POST['password']) && $_POST['password'] == '123456'){
$_SESSION['ok'] = 1;
$_SESSION[$ip] = 0;//登录成功,IP登录次数归零
header('location:?');}
if(!isset($_SESSION['ok'])){
$_SESSION[$ip] = $_SESSION[$ip] + 1;//累积登录错误次数 exit('<form method="post">
password:<input type="password" name="password" /><input type="submit" value="login" />
</form');}
// cookie保存时间,分钟
$lifeTime = 1800;
setcookie(session_name(), session_id(), time() + $lifeTime, "/"); }
else
{echo '你已经输错3次密码,请等5分钟再试试';}
//获取用户真实IP
function getIp(){ $onlineip='';
if(getenv('HTTP_CLIENT_IP')&&strcasecmp(getenv('HTTP_CLIENT_IP'),'unknown')){
$onlineip=getenv('HTTP_CLIENT_IP'); } elseif(getenv('HTTP_X_FORWARDED_FOR')&&strcasecmp(getenv('HTTP_X_FORWARDED_FOR'),'unknown')){
$onlineip=getenv('HTTP_X_FORWARDED_FOR'); } elseif(getenv('REMOTE_ADDR')&&strcasecmp(getenv('REMOTE_ADDR'),'unknown')){
$onlineip=getenv('REMOTE_ADDR'); elseif(isset($_SERVER['REMOTE_ADDR'])&&$_SERVER['REMOTE_ADDR']&&strcasecmp($_SERVER['REMOTE_ADDR'],'unknown')){ $onlineip=$_SERVER['REMOTE_ADDR']; } return $onlineip;
} ?>通过SESSION和cookie可以简单的就实现了。