douxiuyi6529 2013-02-01 05:43
浏览 54

用户登录脚本:用户可以使用任何用户名和密码登录

I am trying to make a simple user - login system. However, I have everything with sessions, and stuff working but the user login is not working with the database. I can input any username and password and login.

Here is my login page:

<?php
session_start();


$db_host = "localhost";             // Place the database host here
$db_username = "data_user";  // Place the username for the MySQL database here
$db_pass = "password";          // Place the password for the MySQL  database here
$db_name = "database_name";     // Place the name for the MySQL database here

if (isset($_POST['username']))
{
// MySQL Connection
$db_link = mysql_connect("$db_host","$db_username","$db_pass") or die ("could not connect to mysql. Make sure you have correctly inputed your host, username, and password.");
mysql_select_db("$db_name") or die ("no database could be found.");

$username = mysql_real_escape_string($_POST['username']);
$password = mysql_real_escape_string($_POST['password']);

// MySQL Query
$result = mysql_query("SELECT * FROM users WHERE
username = '$username' AND password = '$password' ");

if(!$result) {
    $_SESSION['error'] = '<span style="color:red">Login Failed</span>';
} else 
    {
    $row = mysql_fetch_assoc($result);
        $_SESSION['userid'] = $row['id'];
        $_SESSION['username'] = $username;
    }
    mysql_close($db_link);
}
header('Location: ./')
?>

I am using Bootstraps login. So this is my login html:

                <div id="login" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
  <div class="modal-header">
   <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
    <h3 id="myModalLabel">Login</h3>
  </div>
  <div class="modal-body">

    <form class="form-horizontal" action="login.php" method="post">
  <div class="control-group">
    <label class="control-label" for="inputUsername">Username</label>
    <div class="controls">
      <input name="username" type="text" id="inputUsername" placeholder="Username">
    </div>
  </div>
  <div class="control-group">
    <label class="control-label" for="inputPassword">Password</label>
    <div class="controls">
      <input name="password" type="password" id="inputPassword" placeholder="Password">
    </div>
  </div>
  <div class="control-group">
    <div class="controls">
      <label class="checkbox">
        <input type="checkbox"> Remember me
      </label>
    </div>
  </div>
  </div>
   <div class="modal-footer">
    <button type="submit" class="btn btn-primary">Sign in</button>
    </form>
  </div>
</div>

It is the very basic login using bootstrap. I got the log out working fine so thats good, however anyways can anyone explain to me why it is not connected to the database and you can login with any username and password that you can imagine?

  • 写回答

6条回答 默认 最新

  • dq8081 2013-02-01 05:47
    关注

    print_r($row);

    what that display when you print $row row does that return the data

    评论

报告相同问题?

悬赏问题

  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据