登录系统获胜';t将新用户重定向到右侧页面


Login System won't redirect new users to right page

在游戏中工作&我添加了一个功能,当用户注册时,它会向数据库中插入一组随机坐标,以及他们的所有其他信息,然后登录用户。

登录系统会将用户重定向到正确的页面,除非他们是一个新的寄存器,在这种情况下,它只会将他们引导到0,0。我不知道为什么会发生这种事。

    function login($username, $password)
    {
    $password = $this->passwordEncryption($_POST['password']);
    $sql = "SELECT count(uid), uid, homeX, homeY FROM users WHERE username = :username AND password = :password";
    $que = $this->db->prepare($sql);
    $que->bindParam('username', $_POST['username']);
    $que->bindParam('password', $password );
    try{ 
        $que->execute();
        while($row = $que->fetch(PDO::FETCH_BOTH))
        {
            if($row[0] == 0)
            {
                $error = 'who do you think you are?';
                echo $error;
            }
            else
            {
                $_SESSION['uid'] = $row[1];
                $x = $row[2];
                $y = $row[3];
                $index = "index.php?X={$x}&Y={$y}";
                return $index;
                //* Start the Session Timer
                $_SESSION['SS'] = time();
            }
        }
    }catch(PDOException $e) { echo $e->getMessage();}   
}
    function registerUser($password, $username)
{
    if($this->checkUsername($username) == 'chr')
    {
        header('location:index.php?error=nameC');
    }
    if($this->checkUsername($username) == 'short')
    {
        header('location:index.php?error=nameL');   
    }
    if(!$this->checkUsername($username))
    {
        header('location:index.php?error=taken');   
    }
    else
    {
    if(strlen($password) == 0)
    {
        header('location:index.php?error=pass');
    }
    else
    {
        $password = $this->passwordEncryption($password);
        $x = rand(-16, 16);
        $y = rand(-16, 16);
        $sql = "INSERT INTO users(username, password, homeX, homeY) VALUES (:username, :password, :X, :Y);";
        $sql .= "INSERT INTO bank_accounts(balance, fuel_cell, energy_cell) VALUES (10000,575, 575);";
        $sql .= "INSERT INTO user_upgrades(science, technology, economy, religion, military) VALUES(1,1,1,1,1)";
        $que = $this->db->prepare($sql);
        $que->bindParam('username', $username);
        $que->bindParam('password', $password);
        $que->bindParam('Y', $y);
        $que->bindParam('X', $x);
        try{
             $que->execute(); 
             $que->nextRowset();
             $que->nextRowset();
             $this->login($username, $password);
             }
             catch(PDOException $e){}
        }
    }
}

我想你在问为什么新寄存器总是重定向到0,0的cornates。这很可能是因为该表对于该新用户没有任何值,并且对于数据库中的homeX和homeY列,该表将是0。很难准确判断,因为我们无法知道用户注册时您是否正在设置这些值,因为您的代码只显示登录信息。

我还想指出,返回后的任何代码都不会被执行。在验证用户已登录的else语句内部,您在会话之前返回。

return $index;
//* Start the Session Timer
$_SESSION['SS'] = time();

会话将永远不会设置。

编辑更新。您没有正确绑定值。

    $que->bindParam(':username', $username);
    $que->bindParam(':password', $password);
    $que->bindParam(':Y', $y);
    $que->bindParam(':X', $x);