登录突然停止工作

时间:2014-01-03 20:03:49

标签: php login mysqli

我正在研究我的学校项目,我需要一个简单的登录功能。它在20分钟前工作,但后来我可能犯了一些错误。它不显示任何错误消息。数据库似乎没问题。

'jmeno'=姓名,'heslo'=密码

<?php $mysqli = new mysqli("localhost","admin","admin","uzivatele");

    if(isset( $_POST['heslo']) && isset($_POST['jmeno'])){
        $username = $_POST['heslo'];
        $password = $_POST['jmeno'];
        /* defends SQL injection */
       // $username = stripslashes($username);
        //$password = stripslashes($password);
        //$password = mysqli_real_escape_string($mysqli, ($_POST['heslo']));
        //$username = mysqli_real_escape_string($mysqli, $_POST['jmeno']);

         $sqllogin = "SELECT * FROM prihlaseni WHERE jmeno = '".$username."'  AND heslo = '".$password."' LIMIT 1";

        $result = mysqli_query($mysqli, $sqllogin);
        if (!$result) {
        die(mysqli_error($mysqli));
        }
        $count = mysqli_num_rows($result);       

        if ($count == 1) {
        session_start();
        $_SESSION['loggedin'] = true;
        header('Location: home.php');

        }else {

        echo "<script language='javascript'>alert('Wrong password!');</script>";
    }
    }
?>

2 个答案:

答案 0 :(得分:3)

我认为你混合了后期价值观。试试:

$username = $_POST['jmeno'];
$password = $_POST['heslo'];

答案 1 :(得分:0)

我建议调试如下:

<?php $mysqli = new mysqli("localhost","admin","admin","uzivatele");

    if(isset( $_POST['heslo']) && isset($_POST['jmeno'])){
        $username = $_POST['heslo'];
        $password = $_POST['jmeno'];
        /* defends SQL injection */
       // $username = stripslashes($username);
        //$password = stripslashes($password);
        //$password = mysqli_real_escape_string($mysqli, ($_POST['heslo']));
        //$username = mysqli_real_escape_string($mysqli, $_POST['jmeno']);

         $sqllogin = "SELECT * FROM prihlaseni WHERE jmeno = '".$username."'  AND heslo = '".$password."' LIMIT 1";


        echo $sqllogin; //check the sql query string
        $result = mysqli_query($mysqli, $sqllogin);
        print_r($result);
        if (!$result) {
        die(mysqli_error($mysqli));
        }
        $count = mysqli_num_rows($result);       

        if ($count == 1) {
        session_start();
        $_SESSION['loggedin'] = true;
        header('Location: home.php');

        }else {

        echo "<script language='javascript'>alert('Wrong password!');</script>";
    }
    }
?>

如果sql字符串看起来正确,请尝试直接查询数据库并检查输出。 可能它没有获得$ _POST变量,也没有返回有效的$ result。 另外我建议你不要处理和保存这样的密码,而是使用像md5(string)这样的哈希函数。