当前位置:Gxlcms > PHP教程 > php浣跨敤cookie瀹炵幇璁颁綇鐧诲綍鐘舵€侊紝phpcookie鐧诲綍鐘舵€乢PHP鏁欑▼|甯涔嬪

php浣跨敤cookie瀹炵幇璁颁綇鐧诲綍鐘舵€侊紝phpcookie鐧诲綍鐘舵€乢PHP鏁欑▼|甯涔嬪

时间:2021-07-01 10:21:17 帮助过:1人阅读

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);
}
}
?>

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/990543.htmlTechArticlephp浣跨敤cookie瀹炵幇璁颁綇鐧诲綍鐘舵€侊紝phpcookie鐧诲綍鐘舵€?瑕佸疄鐜拌浣忓瘑鐮佽嚜鍔ㄧ櫥褰曠殑鍔熻兘鎴戜滑澶у鏁版嵁閮芥槸鍒╃敤浜嗗鎴风鐨刢ookies鏉ュ疄鐜帮紝鎴戜滑鍒╃敤...

人气教程排行