1

我有一个 php 登录脚本,可以通过一个简单的表单访问它:

<?php
session_start();
try{
    $user = 'root';
    $pass = null;
    $pdo = new PDO('mysql:host=localhost; dbname=divebay;', $user, $pass);

    if(isset($_SESSION['loggedin'])){

        echo "1"; //already logged in
    }


    else{
        $username = $_POST['username'];
        $password = sha1($_POST['password']);

        $ucheck = $pdo->prepare('SELECT * FROM user WHERE username = ?');
        $ucheck->bindValue(1, $username);
        $ucheck->execute();

        if($ucheck->fetch(PDO::FETCH_OBJ)){
            $stmt = $pdo->prepare('SELECT * FROM user WHERE username = ? AND password = ?');
            $stmt->bindValue(1, $username);
            $stmt->bindValue(2, $password);

            if($stmt->fetch(PDO::FETCH_OBJ)){
                $row = $stmt->fetch(PDO::FETCH_ASSOC);

                $_SESSION['username'] = $row['username'];
                $_SESSION['loggedin'] = 'YES';
                $_SESSION['location'] = $row['location'];
                echo "2"; //logged in

            }
            else{
                echo "3"; //password incorrect
            }
        }
        else{
            echo "4"; //user does not exist
        }

    }
}catch(PDOException $e){
    echo $e->getMessage();
}

?>

但是当我尝试使用我刚刚创建并已确认存在于数据库中的帐户运行它时,我没有从该脚本中得到任何响应。鉴于登录信息是正确的,我希望它回显 2,但我什么也没得到

谁能建议我在这里做错了什么?

4

2 回答 2

1

您似乎忘记了execute()以下声明:

    if($ucheck->fetch(PDO::FETCH_OBJ)){
        $stmt = $pdo->prepare('SELECT * FROM user WHERE username = ? AND password = ?');
        $stmt->bindValue(1, $username);
        $stmt->bindValue(2, $password);

        // Execute it!!!
        if ($stmt->execute()) {
          $row = $stmt->fetch(PDO::FETCH_OBJ);

          if ($row) {
            // And don't call fetch() again, since you would already have advanced
            // the record pointer in the first fetch() above. If one record was returned,
            // this one would always be FALSE.
            //$row = $stmt->fetch(PDO::FETCH_ASSOC);

            $_SESSION['username'] = $row['username'];
            $_SESSION['loggedin'] = 'YES';
            $_SESSION['location'] = $row['location'];
            echo "2"; //logged in
          }
          // else execute failed...
        }
于 2012-09-05T01:57:35.360 回答
0

你确定session.use_cookies = 1在 php.ini 中吗?

请确保名称是 PHPSESSION cookie。

于 2012-09-05T02:17:51.987 回答