使用Sessions / SQL进行简单登录

时间:2014-12-07 17:59:39

标签: php mysql login

我正在尝试在我的网站上创建一个简单的登录体验。数据来自phpmyadmin。我遇到了麻烦而且不确定我到底哪里出错了。我希望现在尽可能保持这个简单,只是为了让它开始。

HTML

    <div class="container">
        <div class="row">
          <div class="col-md-8 col-md-offset-2">
            <div class="well well-sm">
              <form class="form-horizontal" action="" method="post">
              <fieldset>
                <legend class="text-center">Sign In</legend>


                <!-- Message body -->
                <div class="form-group">
                  <label class="col-md-4 control-label" for="Username">Username</label>
                  <div class="col-md-8">
                    <input id="username" name="username" type="text" placeholder="Your email" class="form-control">
                  </div>
                </div>

                <div class="form-group">
                  <label class="col-md-4 control-label" for="Password">Password</label>
                  <div class="col-md-8">
                    <input id="password" name="password" type="text" placeholder="Your email" class="form-control">
                  </div>
                </div>

                <!-- Form actions -->
                <div class="form-group">
                  <div class="col-md-12 text-right">
                    <button onClick="return validateForm()" type="submit" class="btn btn-primary btn-lg">Submit</button>
                 ` </div>
                </div>
              </fieldset>
              </form>
            </div>
          </div>
        </div>
    </div>

PHP

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

// Connection
$conn = mysql_connect("localhost", "root", "MIS42520!$") or die (mysql_error());

//Select the database to use
mysql_select_db ("cookie", $conn);

// SQL query to fetch information of registerd users and finds user match.
$sql = mysql_query("select * from login where password='$password' AND username='$username'",     $connection);

$result = mysql_query($sql, $conn) or die(mysql_error());

$row  = mysql_fetch_array($sql);

if(is_array($row)) {
$_SESSION["username"] = $row[username];
$_SESSION["password"] = $row[password];
} else {
$message = "Invalid Username or Password!";
}

if(isset($_SESSION["user_id"])) {
header("Location:user_dashboard.php");
}

1 个答案:

答案 0 :(得分:0)

我认为这是您的问题,因为我注意到您的表单操作是针对同一页面的。

if(isset($_POST['username']) && isset($_POST['password'])){

//grab all your php code here
}

请不要使用mysql_ *函数,因为它已被弃用