0
<!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>

I've been stuck with this login script for the last 15 minutes, I cant figure out why it is not logging me in :/ Would really appreciate if someone could point me out the possible problems.

I've followed all the steps that I normally follow when creating a login/register script, really clueless right now.

4

1 回答 1

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 } ?>
于 2013-08-18T08:14:39.583 回答