简体   繁体   中英

Login system wont login

So, I have the following code which is supposed to work as a login system. When I enter the details that are supposed to be entered, it still comes up with "Wrong details. Please try again". This is probably a stupid basic bug but I am not that fluent with PHP yet.

<?php

$dbc = mysqli_connect("hostaddress","user","pass") or 
      die("Could not connect to server". mysqli_connect_error());

mysqli_select_db($dbc, "dbname") or die("could not connect to the database");

//Check if the login form has been submitted;

if(isset($_POST["go"])) {
      $addr = mysqli_real_escape_string($dbc, htmlentities($_POST["e_address"]));
      $psw = SHA1 ($_POST["u_pass"]); //Using sha1() to encrypt passwords

      //query to check if the email address and password match;
      $q = "SELECT * FROM users WHERE address='$addr' AND pass='$psw'";

      //run the query and store result;
      $res = mysqli_query($dbc, $q);

      //Make sure we have a positive result;
      if($res = mysqli_query($dbc, $q)) {
        //Start a session;
        session_start();

        //Creating a log session variable that will persist through pages;
        $_SESSION["log"] = "in";

        //Redirecting to restricted page;
        header("location: restricted.php");

      } else {
        //Create an error message;
        $error = "Wrong details. Please try again";
      }

} //End isset go

?>

<form method="post" action="#">

    <p><label for="e_address">Email Address:</label></p>
    <p><input type="text" name="e_address" value="" placeholder="Email Address" maxlength="30"></p>
    <p><label for="u_pass">Password:</label></p>
    <p><input type="password" name="u_pass" value="" placeholder="Password"   maxlength="12"></p>
<p><button type="submit" name="go">Log me in</button></p>
</form>

<!-- Error Displayer -->
<p><strong><?php if(isset($error)) { echo $error; } ?></strong></p> 

try to start with session_start(); at first line

and try add after the query or die(__LINE__." ".mysqli_error($dbc))

and U can use the query without the password and check the password then with php

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM