以下是login_users_process.php的代码:
<?php
session_start();
include_once("functions/database.php");
$name = $_POST["name"];
if($_POST["checknum"] != $_SESSION["checknum"]){
header("Location:index_users.php?login_message=checknum_error");
return;
}
if(isset($_COOKIE["password"])){
$first_password = $_COOKIE["password"];
}else{
$first_password = md5($_POST["password"]);
}
if(empty($_POST["expire"])){
setcookie("name",$name,time()-1);
setcookie("password",$first_password,time()-1);
}
$password = md5($first_password);
$sql = "select * from users where name='$name' and password ='$password'";
get_connection();
$result_set = mysql_query($sql);
if(mysql_num_rows($result_set)>0){
if(isset($_POST["expire"])){
$expire = time()+intval($_POST["expire"]);
setcookie("name",$name,$expire);
setcookie("password",$first_password,$expire);
}
$users = mysql_fetch_array($result_set);
$_SESSION['user_id'] = $users['user_id'];
$_SESSION['name'] = $users['name'];
header("Location:index_users.php?login_message=password_right");
}else{
header("Location:index_users.php?login_message=password_error");
}
close_connection();
?>
以下是login_users.php 的代码
<?php
session_start();
include_once("functions/database.php");
include_once("functions/is_login.php");
if(isset($_GET["login_message"])){
if($_GET["login_message"]=="checknum_error"){
echo "验证码错误,重新登录!
";
}else if($_GET["login_message"]=="password_error"){
echo "密码错误,重新登录!
";
}else if($_GET["login_message"]=="password_right"){
echo "登录成功!
";
}
}
if(is_login()){
echo "欢迎".$_SESSION['name']."访问系统!
";
echo "注销";
return;
}
$name = "";
if(isset($_COOKIE["name"])){
$name = $_COOKIE["name"];
}
$password = "";
if(isset($_COOKIE["password"])){
$password = $_COOKIE["password"];
}
?>
密 码 :
验证码:<?php $checknum = ""; $checknum .= mt_rand(0,9); $checknum .= mt_rand(0,9); $checknum .= mt_rand(0,9); $checknum .= mt_rand(0,9); $_SESSION['checknum'] = $checknum; echo $checknum;?>
Cookie保存1小时