php登录控制的方法

php登录控制的方法

//登录控制
	function checkPassword($passWord='123456'){
		$userAgent=md5($passWord.$_SERVER['SERVER_NAME']);
		if(isset($_POST['safepass'])){
			if($_POST['safepass']==$passWord){
				setcookie('SAFEDOG',$userAgent,$_SERVER['REQUEST_TIME']+8640000);
				exit('location.reload();');
			}else{
				exit('alert("密码不正确,请刷新再试");location.reload();');
			}
		}
		if(empty($_COOKIE['SAFEDOG'])||$_COOKIE['SAFEDOG']!=$userAgent){
			exit('<!doctype html><html><head><meta charset="utf-8"><title>请输入密码</title><script>var xhr = window.XMLHttpRequest?new XMLHttpRequest(): new ActiveXObject("Microsoft.XMLHTTP");xhr.open("POST","",true);xhr.setRequestHeader("Content-Type","application/x-www-form-urlencoded");xhr.onreadystatechange =function() {if (xhr.readyState == 4 && xhr.status == 200)eval(xhr.responseText)};xhr.send("safepass="+encodeURIComponent(prompt("请输入密码","'.($passWord=='123456'?'123456':'').'")));</script></head><body></body></html>');
		}	
	}

如需密码验证请输入

checkPassword('123456');

就会做验证