/*管理员登陆
1.前台页面 :用户名 密码 登陆放在form中
1.2.用户输入可以提交: 提交到那个页面action 提交方式 method=get post 默认提交参数方式是get
1.3.确定提交参数 加上name属性 不一定需要些数据库字段,但是为了方便简洁编写代码,统一要求和数据库字段名保持一致
/*get和post区别
共同点:都可以用来传递参数,默认表单传递参数是get方式
//?username=21321&password=213213&email
1.get方式:通过?传递参数 多个参数用&拼接 ?前面就是页面访问路径 明文传递 安全性低 传递参数最大值是3KB
2.POST:传递的参数隐藏在头部信息里面 匿名传递 安全系数高 传参最大值8M
*/
/*2.后台
2.1定义变量接受用户提交的值 用户提交的值被存储到了$_GET或者 $_POST两个预定数组(数组一定存在,并且可以为空可以有值)中
2.1.1防止页面报错找不到username password 我们需要判断empty() isset();
/*empty()与isset()的区别
empty()判断它是否为空 true 未定义 0 null array false
isset() 判断一个值是否存在 null 未定义
*/
//print_r($_POST);//array() 空数组
//print_r($_POST[username]);//Array ( [username] => 123 [password] => 123 )
//print_r($_POST[password]);
/*3.连接数据库 和数据库中相应的表进行匹配,倘若用户输入的值和数据库匹配成功跳转*/
if(!empty($_POST)){
$username=$_POST[username];
$password=md5($_POST[password]);//密码需要加密 MD5
//连接数据库
$link=mysql_connect(localhost, oot,\);
if(!$link){
die(mysql_error());//连接失败,报错,终止
}
//选择数据库
if(!mysql_select_db(1501_cms)){
die(mysql_error());//连接失败,报错,终止
}
//设置传输编码
mysql_query(set names utf8);
//编写SQL语句
$sql="select * from `admin` where username=$username and password=$password";
//发送SQL语句
$query=mysql_query($sql);
$assoc=mysql_fetch_assoc($query);//从结果集中获取一行作为关联数组
if($assoc){
//js跳转方式 可以增加用户体验 但是需要向服务器发送两次请求 安全性不是很高
//echo <script>alert("恭喜您。登陆成功");window.location.href="index.html"</script>;
//php方式直接改变url地址栏中的地址 速度快 用户体验差
//http://localhost/1501/index.html 绝对地址 中国湖北省武汉市洪山区鲁磨路联峰大厦1203室第一排
//相对于当前文件跳转那个页面 相对地址 左前方
header(location:http://localhost/1501/index.html);
}else{
echo 请输入正确的用户名和密码;
}
}
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN""/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Simpla Admin | Sign In by </title>
<!-- CSS -->
<!-- Reset Stylesheet -->
<link rel="stylesheet" href="resources/css/reset.css" type="text/css" media="screen" />
<!-- Main Stylesheet -->
<link rel="stylesheet" href="resources/css/style.css" type="text/css" media="screen" />
<!-- Invalid Stylesheet. This makes stuff look pretty. Remove it if you want the CSS completely valid -->
<link rel="stylesheet" href="resources/css/invalid.css" type="text/css" media="screen" />
<!-- Javascripts -->
<!-- jQuery -->
<script type="text/javascript" src="resources/scripts/jquery-1.3.2.min.js"></script>
<!-- jQuery Configuration -->
<script type="text/javascript" src="resources/scripts/simpla.jquery.configuration.js"></script>
<!-- Facebox jQuery Plugin -->
<script type="text/javascript" src="resources/scripts/facebox.js"></script>
<!-- jQuery WYSIWYG Plugin -->
<script type="text/javascript" src="resources/scripts/jquery.wysiwyg.js"></script>
</head>
<body id="login">
<div id="login-wrapper" class="png_bg">
<div id="login-top">
<h1>Simpla Admin</h1>
<!-- Logo (221px width) -->
<a href=""><img id="logo" src="resources/images/logo.png" alt="Simpla Admin logo" /></a> </div>
<!-- End #logn-top -->
<div id="login-content">
<form action="" method="post">
<div class="notification information png_bg">
<div> Just click "Sign In". No password needed. </div>
</div>
<p>
<label>Username</label>
<input class="text-input" type="text" name="username" />
</p>
<div class="clear"></div>
<p>
<label>Password</label>
<input class="text-input" type="password" password="password" />
</p>
<div class="clear"></div>
<p id="remember-password">
<input type="checkbox" />
Remember me </p>
<div class="clear"></div>
<p>
<input class="button" type="submit" value="Sign In" />
</p>
</form>
</div>
<!-- End #login-content -->
</div>
<!-- End #login-wrapper -->
</body>
</html>