21,886
社区成员
发帖
与我相关
我的任务
分享
<?php
require_once 'adminService.calss.php';
$id=$_POST['id'];
$password=$_POST['password'];
//实例化一个adminService方法
$adminService=new adminService();
if ($name=$adminService->checkAdmin($id,$password)) {
//合法
header('Location:empManage.php?name=$name');
exit();
}else{
//非法
header('Location:login.php?err=1');
}
?>
<?php
require_once 'sqlHelper.class.php';
//该类是一个业务逻辑处理类,完成对admin表的各项操作
class adminService{
//提供一个验证用户是否合法的方法
public function checkAdmin($id,$password){
$sql="select password,name from admin where id=$id";
$sqlHelper=new sqlHelper();
$res=$sqlHelper->excute_dql($sql);
if ($row=mysql_fetch_assoc($res)) {
if (md5($password)==($row['password'])) {
return $row['name'];
}
}
mysql_free_result($res);
$sqlHelper->close();
return "";
}
}
?>
<?php
class sqlHelper{
public $conn;
public $host='localhost';
public $username='root';
public $password='root';
public $dbname='empManage';
public function db_connect(){
$this->conn=mysql_connect($this->host,$this->username,$this->password);
if (!$this->conn) {
die("数据库连接失败".mysql_errno());
}
mysql_select_db($this->dbname,$this->conn);
}
//执行dql语句
public function excute_dql($sql){
$res=mysql_query($sql,$this->conn);
return $res;
}
//执行dml语句
public function excute_dml($sql){
$b=mysql_query($sql,$this->conn);
if(!$b){
return 0;
}else{
if(mysql_fetch_row($b)>0){
return 1; //表示执行ok
}else{
return 2; //表示没有行收到影响
}
}
}
//关闭连接
public function close(){
mysql_close($this->conn);
}
}
?>
<html>
<head><meta http-equiv="content-type" content="text/html;charset=utf-8"></head>
<h1>管理员登陆系统</h1>
<form action="loginProcess.php" method="post">
<tr>
<td>用户名:</td>
<td><input type="text" name="id"></td>
</tr>
<tr>
<td>密 码:</td>
<td><input type="password" name="password"></td>
</tr>
<tr>
<td><input type="submit" value="用户登陆"></td>
<td><input type="reset" value="重新填写"></td>
</tr>
</form>
<?php
if (!empty($_GET['err'])) {
$err=$_GET['err'];
if ($err==1) {
echo "<font color=red size=5>用户名密码错误!</font>";
}
}
?>
</html>