登陆页面login.php
<!DOCTYPE html>
<html>
<head>
<title>登录</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
<div class="content" align="center">
<!--头部-->
<div class="header">
<h1>登录</h1>
</div>
<!--中部-->
<div class="middle">
<form id="loginform" action="loginaction.php" method="post">
<table border="0">
<tr>
<td>用户名:</td>
<td>
<input type="text" id="name" name="username" required
value="<?php echo isset($_COOKIE["wtcl_shyh"])?$_COOKIE["wtcl_shyh"]:"";?>">
</td>
</tr>
<tr>
<td>密 码:</td>
<td><input type="password" id="password" name="password"></td>
</tr>
<tr>
<td colspan="2">
<input name="remember" type="checkbox"><small>记住我
</td>
</tr>
<tr>
<td colspan="2" align="center" style="color:red;font-size:14px;">
<!--提示信息-->
<?php
$err=isset($_GET["err"])?$_GET["err"]:"";
switch($err) {
case 1:
echo "用户名或密码错误!";
break;
case 2:
echo "用户名或密码不能为空!";
break;
}
?>
</td>
</tr>
<tr>
<td colspan="2" align="center">
<input type="submit" id="login" name="login" value="登录">
<input type="reset" id="reset" name="reset" value="重置">
</td>
</tr>
<tr>
</tr>
</table>
</form>
</div>
<!--脚部-->
<div class="footer">
</div>
</div>
</body>
</html>
验证用户名和密码loginaction.php
<?php $username = isset($_POST['username'])?$_POST['username']:"";//声明变量
$password = isset($_POST['password'])?md5($_POST['password']):"";
$remember = isset($_POST['remember'])?$_POST['remember']:"";
//判断用户名和密码是否为空
if(!empty($username)&&!empty($password)) {
//准备SQL语句
require_once('comm.php');
mysql_select_db($database_wtcl_comm); //打开数据库
mysql_query("set names 'utf8'");
$sql_select = "SELECT * FROM wtcl_yh WHERE yh_ming='$username' AND yh_mi='$password'";
//执行SQL语句
$ret = mysql_query($sql_select) or die(mysql_error()); ;
$row = mysql_fetch_array($ret);
//判断用户名或密码是否正确
if($username==$row['yh_ming']&&$password==$row['yh_mi']) {
//选中“记住我”
if($remember=="on") {
//创建cookie
setcookie("wtcl_shyh", $username, time()+7*24*3600);
}
//开启session session_unset()
@session_start();
//创建session
$_SESSION['wtcl_user'] = $username;
if(!empty($_SESSION['wtcl_user'])){
date_default_timezone_set('PRC');
$zuijin = date('Y-m-d H:i:s');
mysql_select_db($database_wtcl_comm);
$query=mysql_query("update wtcl_yh set yh_zuijin='$zuijin' where yh_ming='$username'");//更新最近登陆时间
//跳转到loginsucc.php页面
/* echo '<SCRIPT LANGUAGE="javascript">location.href="0.php"</SCRIPT>'; */
header('Location:index.php');
}
//写入日志
/* $ip = $_SERVER['REMOTE_ADDR'];
$date = date('Y-m-d H:m:s');
$info = sprintf("当前访问用户:%s,IP地址:%s,时间:%s \n",$username, $ip, $date);
$sql_logs = "INSERT INTO Logs(username,ip,date) VALUES('$username','$ip','$date')";
//日志写入文件,如实现此功能,需要创建文件目录logs
$f = fopen('./logs/'.date('Ymd').'.log','a+');
fwrite($f,$info);
fclose($f); */
//关闭数据库
mysql_close();
}else {
//用户名或密码错误,赋值err为1
echo '<SCRIPT LANGUAGE="javascript">location.href="login.php?err=1"</SCRIPT>';
//header("Location:login.php?err=1");
}
}else {
//用户名或密码为空,赋值err为2
echo '<SCRIPT LANGUAGE="javascript">location.href="login.php?err=2"</SCRIPT>';
//header("Location:login.php?err=2");
}
?>
注销用户信息logout.php
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>注销</title>
</head>
<body>
<?php
@session_start('wtcl_user');
@session_destroy('wtcl_user');
echo '<SCRIPT LANGUAGE="javascript">location.href="login_err.php"</SCRIPT>';
?>
</body>
</html>
主页根据用户登陆情况显示信息index.php
<?php
set_time_limit(0);
@session_start();
$username=isset($_SESSION['wtcl_user'])?$_SESSION['wtcl_user']:"";//判断session是否为空
if(!empty($username)){
$yhming=$_SESSION['wtcl_user'];
mysql_query("set names 'utf8'"); //数据库输出编码 应该与你的数据库编码保持一致,建议用UTF-8 国际标准编码.
mysql_select_db($database_wtcl_comm); //打开数据库
$sql="select * from wtcl_yh where yh_ming='$yhming'";//从数据库中取出用户信息
$rs_yh = mysql_query($sql) or die(mysql_error());
$yonghu = mysql_fetch_assoc($rs_yh);
$_SESSION['wtcl_user']=$yonghu['yh_ming'];}
else{echo '<SCRIPT LANGUAGE="javascript">location.href="login_err.php"</SCRIPT>';}
?>
<?php $yhming=$_SESSION['wtcl_user'];
if($yhming==$yonghu['yh_ming']&&$yonghu['yh_add'] == 1){echo '欢迎你:'.$yonghu['yh_mingzi'].',<a href="logout.php">退出</a>|<a href=../wtcl/admin/index.html target="_blank">管理</a>';}
elseif($yhming==$yonghu['yh_ming']&&$yonghu['yh_add'] != 1){echo '欢迎你:'.$yonghu['yh_mingzi'].',<a href="logout.php">退出</a>';}
elseif(!$yhming){echo '<SCRIPT LANGUAGE="javascript">location.href="login_err.php"</SCRIPT>';} ?>
登陆错误页面login_err.php
<?php require_once('comm.php'); ?>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>请登陆</title>
<link rel="shortcut icon" href="../wtcl/tb.ico" />
<link href="style.css" rel="stylesheet" type="text/css" />
</head>
<body>
<table width="960" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td height="60" align="center" valign="top" class="biaoti">
<p>用户名或密码有误</p>
<p>或登陆超时</p>
<p>请重新<a href="login.php" style="font-size: 20px"> 登陆</a></p>
<p> </p></td>
</tr>
</table>
</body>
</html>