<?php
    session_start();
//mysql信息
    $db_host = 'db_host';
    $db_user = 'db_user';
    $db_pwd = 'db_pwd';
    $db_name = 'db_name';
//连接数据库
    $conn = mysql_connect($db_host,$db_user,$db_pwd) or die('mysql连接失败:'.mysql_error());
    mysql_select_db($db_name);
    mysql_query('set names utf8');
//对比数据
    if ($_POST['user'] and $_POST['passwd'] != ''){    //判断提交的数据是否为空
        $user = $_POST['user'];    
        $passwd = $_POST['passwd'];
        $searchsql = mysql_query("SELECT * FROM db_user WHERE username like '$user' and passwd like '$passwd'");    //搜索数据库
        $showsql = mysql_fetch_array($searchsql);    //显示数据库
            if ($showsql){    //判断数据库是否返回数据
                $_SESSION['user'] = $user;  //如果都正确,那么设置SESSION
                $_SESSION['passwd'] = $passwd;    //同理
                $_SESSION['username'] = $showsql['name'];    //同理
                $_SESSION['date'] = $showsql['date'];    //同理
                $login = true;    //设置个变量,方便HTML上做判断
                //echo $showsql['name'];
                header("location:index.php");   //并且跳转会index.php页面
                //echo "登录成功";
            }else {
                $login = false;
                //echo '';
            }
    }
?>

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">

<!-- 新 Bootstrap 核心 CSS 文件 -->
<link rel="stylesheet" href="http://cdn.bootcss.com/bootstrap/3.3.5/css/bootstrap.min.css">

<!-- 可选的Bootstrap主题文件(一般不用引入) -->
<link rel="stylesheet" href="http://cdn.bootcss.com/bootstrap/3.3.5/css/bootstrap-theme.min.css">
<!-- 本地 核心 JavaScript 文件 -->
<link href="./css/css.css" rel="stylesheet">

<!-- jQuery文件。务必在bootstrap.min.js 之前引入 -->
<script src="http://cdn.bootcss.com/jquery/1.11.3/jquery.min.js"></script>

<!-- 最新的 Bootstrap 核心 JavaScript 文件 -->
<script src="http://cdn.bootcss.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
<script type="text/javascript">
 </script>

<title>系统登录</title>
</head>

<body>

    <div class="container">
      <form method="post" class="form-signin">
        <h2 class="form-signin-heading">系统登录</h2>
        <label for="inputEmail" class="sr-only">用户名</label>
        <input type="text" id="text" class="form-control" placeholder="用户名" name="user" required autofocus>
        <label for="inputPassword" class="sr-only">密码</label>
        <input type="password" id="inputPassword" class="form-control" placeholder="密码" name="passwd" required>
        <div class="checkbox">
          <label>
            <input type="checkbox" value="remember-me">记住我
          </label>
        </div>
        <button class="btn btn-lg btn-primary btn-block" type="submit" name="submit" value="登录">登录</button>
        <a class="btn btn-success btn-primary btn-block" href="index.php">返回主页</a>
        <?php
      if ($login == false and $_POST['submit'] == '登录'){
      ?>
      <br>
      <div class="alert alert-danger" role="alert">亲,你家呢密码或者用户名怕是输错了!</div>
      <?php
      }
      ?>
      </form>
    </div> <!-- /container -->


  </body>
</html>

<?php
mysql_close($conn); //关闭数据库
?>
最后修改:2015 年 07 月 13 日
如果觉得我的文章对你有用,请随意赞赏