if(!empty($user_username)&&!empty($user_password)){ //MySql中的SHA()函数用于对字符串进行单向加密 $query = "SELECT user_id, username FROM mismatch_user WHERE username = '$user_username' AND "."password = SHA('$user_password')"; //用用户名和密码进行查询 $data = mysqli_query($dbc,$query); //若查到的记录正好为一条,则设置COOKIE,同时进行页面重定向 if(mysqli_num_rows($data)==1){ $row = mysqli_fetch_array($data); setcookie('user_id',$row['user_id']); setcookie('username',$row['username']); $home_url = 'loged.php'; header('Location: '.$home_url); }else{//若查到的记录不对,则设置错误信息 $error_msg = 'Sorry, you must enter a valid username and password to log in.'; } }else{ $error_msg = 'Sorry, you must enter a valid username and password to log in.'; } } }else{//如果用户已经登录,则直接跳转到已经登录页面 $home_url = 'loged.php'; header('Location: '.$home_url); } ?>
Mismatch - Log In
Msimatch - Log In
if(empty($_COOKIE['user_id'])){ echo '
'.$error_msg.'
'; ?>
} ?>
效果图:
3、登入页面:loged.php
复制代码 代码如下:
//已登录页面,显示登录用户名 if(isset($_COOKIE['username'])){ echo 'You are Logged as '.$_COOKIE['username'].' '; //点击“Log Out”,则转到logOut.php页面进行cookie的注销 echo ' Log Out('.$_COOKIE['username'].')'; } /**在已登录页面中,可以利用用户的cookie如$_COOKIE['username']、 * $_COOKIE['user_id']对数据库进行查询,可以做好多好多事情*/ ?>