PHP学习-验证用户名密码

登录页:login.php

 1 <?php
 2 //登录
 3 if(!isset($_POST['submit'])){exit('非法访问!');}
 4 $username = $_POST['adname'];
 5 $password = $_POST['adpass'];
 6 //包含数据库连接文件
 7 include('conn.php');
 8 //检测用户名及密码是否正确
 9 $check_query = mysql_query("select * from admin where ad_name='$username' and ad_code='$password' limit 1");
10 if($result = mysql_fetch_array($check_query)){
11     //登录成功
12     session_start();
13     $_SESSION['username'] = $username;
14     $_SESSION['userid'] = $result['ad_id'];
15     echo $username,' 欢迎你!进入 <a href="my.php">用户中心</a><br />';
16     echo '点击此处 <a href="login.html">注销</a> 登录!<br />';
17     exit;
18 } else {
19     exit('登录失败!点击此处 <a href="javascript:history.back(-1);">返回</a> 重试');
20 }
21 //注销登录
22 if($_GET['action'] == "logout"){
23     unset($_SESSION['userid']);
24     unset($_SESSION['username']);
25     echo '注销登录成功!点击此处 <a href="login.html">登录</a>';
26     exit;
27 }
28 ?>

$_POST["a"]:获取post提交的数据a的值

mysql_query("select * from admin where ad_name='$username' and ad_code='$password' limit 1");执行一条MySQL查询

mysql_fetch_array($check_query):获取根据如上数据表查询的一行信息

 

posted @ 2016-03-23 15:41  小古哥  阅读(3592)  评论(0编辑  收藏  举报