php浣跨敤cookie瀹炵幇璁颁綇鐧诲綍鐘舵€侊紝phpcookie鐧诲綍鐘舵€乢PHP鏁欑▼ | 甯涔嬪
php浣跨敤cookie瀹炵幇璁颁綇鐧诲綍鐘舵€侊紝phpcookie鐧诲綍鐘舵€?/h2>
瑕佸疄鐜拌浣忓瘑鐮佽嚜鍔ㄧ櫥褰曠殑鍔熻兘鎴戜滑澶у鏁版嵁閮芥槸鍒╃敤浜嗗鎴风鐨刢ookies鏉ュ疄鐜帮紝鎴戜滑鍒╃敤php涔熶笉渚嬪锛屾湁闇€瑕佺殑鏈嬪弸鍙互鍙傝€冧竴涓嬨€?/p>
php鍒朵綔璁颁綇瀵嗙爜鑷姩鐧诲綍鐨勮В鍐虫€濊矾锛屽叾瀹炰篃灏辨槸瀵箂ession,cookies鐨勬搷浣?/p>
涓€銆佹鏌ョ敤鎴锋槸鍚︾櫥褰?/strong>
澶嶅埗浠g爜 浠g爜濡備笅:
//妫€鏌ョ敤鎴锋槸鍚︾櫥褰?
function checklogin(){
if(empty($_SESSION['user_info'])){ //妫€鏌ヤ竴涓媠ession鏄笉鏄负绌?nbsp;
if(empty($_COOKIE['username']) || empty($_COOKIE['password'])){ //濡傛灉session涓虹┖锛屽苟涓旂敤鎴锋病鏈夐€夋嫨璁板綍鐧诲綍鐘?nbsp;
header("location:login.php?req_url=".$_SERVER['REQUEST_URI']); //杞埌鐧诲綍椤甸潰锛岃褰曡姹傜殑url锛岀櫥褰曞悗璺宠浆杩囧幓锛岀敤鎴蜂綋楠屽ソ銆?nbsp;
}else{ //鐢ㄦ埛閫夋嫨浜嗚浣忕櫥褰曠姸鎬?nbsp;
$user = getUserInfo($_COOKIE['username'],$_COOKIE['password']); //鍘诲彇鐢ㄦ埛鐨勪釜浜鸿祫鏂?nbsp;
if(empty($user)){ //鐢ㄦ埛鍚嶅瘑鐮佷笉瀵规病鍒板彇鍒颁俊鎭紝杞埌鐧诲綍椤甸潰
header("location:login.php?req_url=".$_SERVER['REQUEST_URI']);
}else{
$_SESSION['user_info'] = $user; //鐢ㄦ埛鍚嶅拰瀵嗙爜瀵逛簡锛屾妸鐢ㄦ埛鐨勪釜浜鸿祫鏂欐斁鍒皊ession閲岄潰
}
}
}
}
浜岋紝鐢ㄦ埛鎻愪氦鐧诲綍淇℃伅
澶嶅埗浠g爜 浠g爜濡備笅:
username = trim($_POST['username']);
$password = md5(trim($_POST['password']));
$validatecode = $_POST['validateCode'];
$ref_url = $_GET['req_url'];
$remember = $_POST['remember'];
$err_msg = '';
if($validatecode!=$_SESSION['checksum']){
$err_msg = "楠岃瘉鐮佷笉姝g‘";
}elseif($username=='' || $password==''){
$err_msg = "鐢ㄦ埛鍚嶅拰瀵嗙爜閮戒笉鑳戒负绌?;
}else{
$row = getUserInfo($username,$password);
if(empty($row)){
$err_msg = "鐢ㄦ埛鍚嶅拰瀵嗙爜閮戒笉姝g‘";
}else{
$_SESSION['user_info'] = $row;
if(!empty($remember)){ //濡傛灉鐢ㄦ埛閫夋嫨浜嗭紝璁板綍鐧诲綍鐘舵€佸氨鎶婄敤鎴峰悕鍜屽姞浜嗗瘑鐨勫瘑鐮佹斁鍒癱ookie閲岄潰
setcookie("username", $username, time()+3600*24*365);
setcookie("password", $password, time()+3600*24*365);
}
if(strpos($ref_url,"login.php") === false){
header("location:".$ref_url);
}else{
header("location:main_user.php");
}
}
}
涓夛紝褰撶敤鎴风偣閫€鍑烘椂锛屾竻鍑鸿褰曠櫥褰曠姸鎬?/strong>
澶嶅埗浠g爜 浠g爜濡備笅:
//閫€鍑虹櫥褰?
function logout(){
unset($_SESSION['user_info']);
if(!empty($_COOKIE['username']) || !empty($_COOKIE['password'])){
setcookie("username", null, time()-3600*24*365);
setcookie("password", null, time()-3600*24*365);
}
}
鍥涖€佺畝娲佺増瀹炰緥
澶嶅埗浠g爜 浠g爜濡備笅:
//璇诲彇COOKIE鐨勭敤鎴峰悕鍜屽瘑鐮佺殑鍊煎嵆鍙?br />
if($_COOKIE['uname']!=''){$CKUNAME = $_COOKIE['uname'];}
if($_COOKIE['pwd']!=''){$CKPWD = $_COOKIE['pwd'];}
echo $CKUNAME;
echo '
';
echo $CKPWD;
?>
//鐧诲綍锛屽皢鐢ㄦ埛鍚嶅拰瀵嗙爜瀛樺叆鍒癈OOKIE
if($_POST['button']!=''){
$uname = $_POST['uname'];
$pwd = $_POST['pwd'];
//濡傛灉杈撳叆鐨勫姞瀵嗗瘑鐮佸拰COOKIE涓笉涓€鏍凤紝閭d箞灏卞姞瀵?br /> if($pwd!=$CKPWD){$pwd = md5($pwd);}
$remember = $_POST['remember'];
if($remember==1){
setcookie("uname", $uname, time()+3600*24*30);
setcookie("pwd", $pwd, time()+3600*24*30);
}
}
?>