简体   繁体   中英

Error in Login PHP Code

Currently I'm working on a site and I've ran into a problem on the login script.

<?php
    include 'includes/connect.php';
    include 'includes/core.php'; 
?>
<?php
    $user = sanitizeString($_POST['username']);
    $pass = sanitizeString($_POST['password']);

    if($user == "" || $pass = ""){
        $error = "Not all fields were entered<br />";
    } else {
        $query = "SELECT username,password FROM admins WHERE username='$user' AND password='$pass'";
        if(mysql_num_rows(queryMysql($query)) == 0){
            print "Wrong Username/Password <br />";
            print "$user";
            print "$pass";
        } else {
            $_SESSION['user'] = $user;
            $_SESSION['pass'] = $pass;
            print "Successfully Logged In. Redirecting...";
        }
    }
?>
<html>
    <head>
        <title>
            Login
        </title>
        <link rel="stylesheet" type="text/css" href="css/stylesheet.css">
    </head>

    <body>
        <div id="mainDiv">
            <h2>&nbsp;&nbsp;&nbsp;Login</h2>
            <form action="login.php" method="post">
                Username: <input type="text" name="username"><br>
                Password: <input type="password" name="password"><br><br>
                <input type="submit" name="login" value="Login">
            </form>
        </div>
    </body>
</html>

I know most of this is incomplete but that's not the problem. The problem is that I'm getting "Incorrect Username/Password" when ever I try to login. Upon further investigation I found that $pass isn't being set.

在此处输入图片说明

Why is $pass not being set and displayed?

Change

if($user == "" || $pass = ""){

To

if($user == "" || $pass == ""){

You were setting $pass = "" instead of checking if it was "".

I can't help but notice, unless the PHP and the HTML are two separate files, you're posting back to yourself. leave .

Secondly, it's not wise to have the two together in the same document.

Thirdly, you need to start the session before you can add values to it.

Forth, you don't need to wrap a var in " " if there's no string being attached with it.

Just some thoughts... and don't feel bad, single = has gotten me more times than I'd like to admit.

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