在游戏中工作&我添加了一个功能,当用户注册时,它会向数据库中插入一组随机坐标,以及他们的所有其他信息,然后让用户登录。
登录系统会将用户重定向到正确的页面,除非他们是一个新的注册表,在这种情况下,它只会将他们定向到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){}
}
}
}发布于 2014-04-18 11:45:09
我相信你在问为什么新的寄存器总是被重定向到0,0的坐标。这很可能是因为对于该新用户,该表没有任何值,并且对于数据库中的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);https://stackoverflow.com/questions/23146989
复制相似问题