PHP登录脚本/不工作,没有错误

时间:2013-08-17 23:16:57

标签: php mysql debugging

<!DOCTYPE html>
<html lang="en">
  <head>

    <!-- Font Awesome -->
    <link href="//netdna.bootstrapcdn.com/twitter-bootstrap/2.3.2/css/bootstrap-combined.no-icons.min.css" rel="stylesheet">
    <link href="//netdna.bootstrapcdn.com/font-awesome/3.2.1/css/font-awesome.css" rel="stylesheet">
    <!-- End Font Awesome -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="description" content="">
    <meta name="author" content="">
    <link rel="shortcut icon" href="../../assets/ico/favicon.png">

    <title>xAuth - Simple Login/Register Script</title>

    <!-- Bootstrap core CSS -->
    <link href="css/bootstrap.css" rel="stylesheet">
  </head>

  <body>

    <div class="container">
    <div class='well'>
      <form class="form-signin" action="" method="POST">
        <h2 class="form-signin-heading">Login</h2>
    <?php


    /*
    * This is the login part of the script. Below is the PHP code
    */

    session_start();
    if(isset($_SESSION['Username'])) {
    $LoggedIn = $_SESSION['Username'];
    }
    include('config.php');

    if(isset($_POST['login'])) {

    if(empty($_POST['username'])) {
    $error = "Username should not be left empty";
    $displayError = true;
    } elseif(empty($_POST['password'])) {
    $error = "Password should not be left empty";
    $displayError = true;
    if($displayError==true) {
    echo "<div class='alert alert-danger' style='text-align:center; padding:10px; height:40px;margin: 0 auto;'><b>".$error."</b></div><br />";
    } else {
        $displayError = false;
        $username = $_POST['username'];
        $password = $_POST['password'];
        $userinfo = $con->query("SELECT * FROM users WHERE Username='$username'");
        $userinfo = $userinfo->fetch_object();
        if(hash('sha512', $password) == $userinfo->Password) {
            $_SESSION['Username'] = $username;
            $_SESSION['Password'] = $password;
        }
    }
    }
    }
    ?>
        <input type="text" name="username" class="form-control" placeholder="Your Username" autofocus> <br /><br />
        <input type="password" name="password" class="form-control" placeholder="Your Password"> 
        <input type="submit" class="btn btn-large btn-success"name="login" value="Login">
        <a href="register.php" class="btn btn-large btn-danger"> Register </a>
      </form>


  </body>
</html>

过去15分钟我一直坚持使用这个登录脚本,我无法弄清楚为什么它没有登录我:/如果有人能指出我可能遇到的问题,我真的很感激。

我已经按照创建登录/注册脚本时通常遵循的所有步骤进行操作,现在真的很无能为力。

1 个答案:

答案 0 :(得分:0)

这是您修改后的代码,但是您需要大大改进代码以防止sql注入等等。希望它有效

<?php
    session_start();
    if(isset($_SESSION['Username'])) {
    $LoggedIn = $_SESSION['Username'];
    }
    include('config.php');

    if(isset($_POST['login'])) {

    if(empty($_POST['username'])) {
    $error = "Username should not be left empty";
    $displayError = true;
    } elseif(empty($_POST['password'])) {
    $error .= "<br />Password should not be left empty"; //Add new string to errors
    $displayError = true;
    }//Forgot this curly bracket too
    if($displayError==true) {
    echo "<div class='alert alert-danger' style='text-align:center; padding:10px; height:40px;margin: 0 auto;'><b>".$error."</b></div><br />";
    } else {
       // $displayError = false; No need
        $username = $_POST['username'];
        $password = $_POST['password'];
        $userinfo = $con->query("SELECT * FROM users WHERE Username='$username'");
        $userinfo = $userinfo->fetch_object();
        if(hash('sha512', $password) == $userinfo->Password) {
            $_SESSION['Username'] = $username;
            //$_SESSION['Password'] = $password; Dont store passwords in the session 
            echo'Welcome '.$username;
        }
    }
    }
    else{

    ?>
<!DOCTYPE html>
<html lang="en">
  <head>

    <!-- Font Awesome -->
    <link href="//netdna.bootstrapcdn.com/twitter-bootstrap/2.3.2/css/bootstrap-combined.no-icons.min.css" rel="stylesheet">
    <link href="//netdna.bootstrapcdn.com/font-awesome/3.2.1/css/font-awesome.css" rel="stylesheet">
    <!-- End Font Awesome -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="description" content="">
    <meta name="author" content="">
    <link rel="shortcut icon" href="../../assets/ico/favicon.png">

    <title>xAuth - Simple Login/Register Script</title>

    <!-- Bootstrap core CSS -->
    <link href="css/bootstrap.css" rel="stylesheet">
  </head>

  <body>

    <div class="container">
    <div class='well'>
      <form class="form-signin" action="" method="POST">
        <h2 class="form-signin-heading">Login</h2>

        <input type="text" name="username" class="form-control" placeholder="Your Username" autofocus> <br /><br />
        <input type="password" name="password" class="form-control" placeholder="Your Password"> 
        <input type="submit" class="btn btn-large btn-success"name="login" value="Login">
        <a href="register.php" class="btn btn-large btn-danger"> Register </a>
      </form>


  </body>
</html>

<?php } ?>