MySQLi登录脚本不起作用

时间:2014-03-04 11:54:57

标签: php mysqli

我已经坚持了一段时间,并尝试了不同的代码变体,已经读取了多个堆栈溢出内容,但没有一个工作,我真的需要得到一些帮助,我做错了什么。我的注册页面效果很好但是当我想用注册的详细信息登录时,它会一直说“登录失败”。任何帮助将不胜感激。谢谢。

我的connection.php代码:

<?php

 /****Connetion to database details****/ 
 $dbh = new mysqli("localhost","root","","mydatabase");

// Check connection
if ($dbh->connect_errno)
  {
  echo "Connection to MySQL unsuccessfull!: (" . $dbh->connect_errno . ") " . $dbh->connect_error;
  }

?>

我的login.php代码:

<?php
 session_start();
 include "include_files/connect.php"; 
 include "include_files/header.php";

    if(isset($_POST['submit'])) 
    { 
    $username = validate($_POST['username']); 
    $password = validate($_POST['password']); 
    $hash = password_hash($password, PASSWORD_DEFAULT);

    if(!empty($username) && !empty($password)) 
    { 
        $query = $dbh->prepare("SELECT username, password FROM users WHERE username = ? AND password = ? "); 
        $query->bind_param('ss', $username, $hash); 
        $query->execute(); 
        $query->store_result(); 
        $query->bind_result($username, $hash); 


        if($query->num_rows > 0) 
        { 
            $query->fetch(); 
            $_SESSION['username'] = $username; 
            header('location: index.php'); 
            exit(); 
        } 
        else 
        { 
            echo "Login Failed"; 
        } 
    } 
    else 
    { 
        echo "Your details do not exist!"; 
    } 
}  


/**With thanks to W3Schools**/
function validate($input)
{
     $input = trim($input);
     $input = stripslashes($input);
     $input = htmlspecialchars($input);
     return $input;
}
?>
<div id="login_form">
    <form action="login.php" id="loginForm" method="POST" autocomplete="off">&nbsp; &nbsp;
        <label id="labels">Username:</label>&nbsp<input name="username" type="text" id="tbox" placeholder="Username" />

        <br />
        <br />

        <label id="labels">Password:</label>&nbsp<input name="password" type="password" id="tbox" placeholder="******" /><br /><br />

        <input type="submit" id="login" name="submit" value="Submit" />
    </form>
    <br />
</div>

registration.php代码:

$nameError = $snameError = $passwordError = $emailError = "";
$pwordError = $mailError = "";

if ($_SERVER["REQUEST_METHOD"] == "POST") {

    $success = "Registration complete! Please activate your account using link sent to your email.";

    $username = validate($_POST['username']);
    $validChar = array('-', '_');
    $email = validate($_POST['email']);
    $cEmail = validate($_POST['cEmail']);
    $password = validate($_POST['password']);
    $cPassword = validate($_POST['cPassword']);
    $hash = password_hash($password, PASSWORD_DEFAULT);
    $joinDate = date("Y-m-d");
    $activateCode   = password_hash(mt_rand(0,999), PASSWORD_DEFAULT);

    $emailChecker = $dbh->query("SELECT email FROM users WHERE email = '$email' ");
    //$sql = $conn->prepare($emailChecker);
    //$sql->execute();


    if(empty($username) || empty($password) || empty($cPassword) || empty($email) || empty($cEmail)) {
        $errors[] = 'All fields must be completed!';
    }else {
        /*if(!preg_match("#^[A-Za-z' ]*$#",$firstname)) {
            $nameError = 'Please enter a valid firstname!.';
        }*/

        if(!preg_match("#^[A-Za-z' ]*$#",$username)) {
            $snameError = 'Please enter a valid surname!.';
        }

        if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/",$email)) {
            $emailError = "Please enter a valid email format!";
        }

        if ($password==$cPassword) {

        } else {
            $pwordError = 'Passwords do not match!';
        }

        if ($email==$cEmail) {
        } else {
            $mailError = 'The Email\'s provided do not match!';
        }

        if($emailChecker->num_rows == 1) {
            die("Email address already registered!");
        }   

    if (strlen($password) < 5)  {
        $errors[] = 'Password must be at least 5 characters.';  
        }

        $password = password_hash($password, PASSWORD_DEFAULT);
        $cPassword = password_hash($cPassword, PASSWORD_DEFAULT);
    }

    if (!empty($errors)) 
             foreach ($errors as $error){
                echo '<div id= "errmsg">' .$error. '</div>';
        } else {    

            echo '<div id= "success">'  .$success. '</div>';

                $insert = "INSERT INTO users (username, password, email, activateCode, joinTimeDate) VALUES ('$username',
                    '$hash', '$email', '$activateCode', '$joinDate') "; 

                //$result = $dbh->prepare($insert);
                //$result->bind_param('sssssd', $username, $hash, $email, $accessLevel, $activateCode, $joinDate);

                $query = $dbh->query($insert);              


                $to = $email;   //users email to send to
                $subject = 'Account verification';  //subject of email

                //message and link//


                mail($to, $subject, 
                ' Hello! 
                Thank you for joining our website.      
                We have created your account, you may log in using your email:
                ' .$email. ' and password after activating your account with the below link.
                Click link here to activate your account: 
                http://localhost/public_html/verification.php?email='.$email.'&activateCode='.$activateCode.'');

        }


}

/**With thanks to W3Schools**/
function validate($input)
{
     $input = trim($input);
     $input = stripslashes($input);
     $input = htmlspecialchars($input);
     return $input;
}

?>



    <div id="reg_div">
        <a href="login.php"><img src="images/login_img.jpg" alt="Login" id="loginimg" /></a>
    <form id="reg_form" method="POST" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">

        <label id="alabel">Username:</label>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
            <input type="text" name="username" id="firstname" placeholder="Username" required/> 
            <span id="error">* <?php echo $nameError; ?></span>

        <br />
        <br />

        <label id="alabel">Password:</label>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;
            <input type="password" name="password" id="password" placeholder="********" onblur="checkPass(); return false;" required/>
            <span id="error">* <?php echo $passwordError; ?></span>

        <br />
        <br />

        <label id="alabel">Confirm password:</label>&nbsp; &nbsp;  &nbsp; &nbsp; &nbsp;
            <input type="password" name="cPassword" id="cPassword" placeholder="********" onkeyup="confirmPass(); return false;" required/>
            <span id="error">*</span>

        <br />
        <br />

        <label id="alabel">Email:</label>&nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
            <input type="email" name="email" id="email" placeholder="example@example.com" required/>
            <span id="error">* <?php echo $emailError; ?></span>

        <br />
        <br />

        <label id="alabel">Confirm email:</label>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
            <input type="email" name="cEmail" id="cEmail" placeholder="example@example.com" required/>
            <span id="error">*</span>

        <br />
        <br />

        <input type="reset" width="55" height="55" id="reset" name="reset" value="Reset" onclick="resetFunction()" /> <input type="submit" width="55" height="55" id="submit" name="submit" value="Submit" />


    </form>
    </div>

    <div id="horizontal_line"></div>

0 个答案:

没有答案
相关问题