PHP神秘问题

时间:2012-11-21 02:44:41

标签: php mysql

  

可能重复:
  PHP login Code always say wrong username or password

使用PHP进行登录检查时遇到问题我感到很惭愧,我一直试图调试我的代码几天没有成功。它总是说我的用户名或密码错误。即使数据库中有正确的详细信息,我的值为$ count也为0。谁能发现我的错误?我已经尝试回应查询,直接在PHPmyadmin上运行,我尝试在变量之间添加点,并且还回显错误类型,删除所有数据并再次输入,我更改了我的服务器等。有没有人知道任何其他解决方案?谢谢你们。

<?php

session_start();

ob_start();


$host=""; // Host name 
$username=""; // Mysql username 
$password=""; // Mysql password 
$db_name=""; // Database name 
$tbl_name="users"; // Table name 

// Connect to server and select databse.
mysql_connect($host, $username, $password)
or die('Error connecting to MySQL Server!');
mysql_select_db($db_name)
or die("Database not found");

// Define $myusername and $mypassword 
$myusername=$_POST['myusername']; 
$mypassword=$_POST['mypassword']; 

// To protect MySQL injection (more detail about MySQL injection)
$myusername = stripslashes($myusername);
$mypassword = stripslashes($mypassword);
$myusername = mysql_real_escape_string($myusername);
$mypassword = mysql_real_escape_string($mypassword);

$sql="SELECT * FROM $tbl_name WHERE username='$myusername' and password='$mypassword'";

$result=mysql_query($sql);

// Mysql_num_row is counting table row
$count=mysql_num_rows($result);


// If result matched $myusername and $mypassword, table row must be 1 row
if($count==1){

// Register $myusername, $mypassword and redirect to file "login_success.php"
$_SESSION["myusername"] = $myusername;
$_SESSION["mypassword"] = $mypassword;
header("location:login_success.php");
}
else {
echo "Wrong Username or Password";
}
ob_end_flush()
?>

3 个答案:

答案 0 :(得分:2)

$tbl_name未定义:

$sql="SELECT * FROM $tbl_name WHERE username='$myusername' and password='$mypassword'";

在我的本地计算机上,此类用法不会导致生成错误,因此您有"SELECT * FROM WHERE ..."

答案 1 :(得分:1)

好的,这里有几个问题:

1)首先,不推荐使用session_register,因此我们使用$_SESSION superglobal
1.1)自PHP 4.2.0起,PHP指令register_globals的默认值为off。并且session_register需要启用才能工作 2)由于您的代码不完整或者您只是显示不完整的部分,我不知道您是否已经开始会话,因此我们需要使用session_start();启动它

如果这仍然不适合您,您需要发布您的表方案,以便我们可以重现相同的测试。

所以工作代码是:

session_start();

ob_start();

// We pretend that this is post data
$posted_user = 'test';
$posted_pass = 'test';

// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB");

// Define $myusername and $mypassword 
$myusername = $posted_user; 
$mypassword = $posted_pass; 

// To protect MySQL injection (more detail about MySQL injection)
$myusername = stripslashes($myusername);
$mypassword = stripslashes($mypassword);
$myusername = mysql_real_escape_string($myusername);
$mypassword = mysql_real_escape_string($mypassword);

$sql="SELECT * FROM $tbl_name WHERE username='$myusername' and password='$mypassword' LIMIT 1";

$result=mysql_query($sql);

// Mysql_num_row is counting table row
$count=mysql_num_rows($result);

// If result matched $myusername and $mypassword, table row must be 1 row
if(!empty($count)){

    // Register $myusername, $mypassword and redirect to file "login_success.php"
    $_SESSION["myusername"] = $myusername;
    $_SESSION["mypassword"] = $mypassword;
    echo 'All good.';
    //header("location:login_success.php");
}
else {
    echo "Wrong Username or Password";
}
ob_end_flush();

答案 2 :(得分:0)

始终检查MySQL查询返回的错误状态。 mysql_query()函数在出错时返回false。这有助于您了解何时出现错误以及错误类型。

$result=mysql_query($sql);
if ($result === false) { 
  die(mysql_error());
}

如果您收到上述报告的错误消息,您将获得更多信息,以了解出现了什么问题。

同样,如果$ result无效,mysql_num_rows()可能会返回false,表示错误。 False == 1将失败,如果你的密码错误,那么它会出现,这真的意味着你的SQL失败了。

$count=mysql_num_rows($result);
if ($count === false) {
   die(mysql_error());
}

我同意其他海报,他们唠叨你使用PDO并使用查询参数更安全。它实际上更容易比完全剥离斜线然后转义。