我在这里做错了什么?它说数据库计数失败(在Process.php的$ count行上)没有说明理由。当我带着我们的'#34;或者死去"该行的一部分,它始终显示"用户帐户创建"无论我按下哪个按钮,它实际上都没有创建帐户...
form.php的
<html>
<head>
<title>Forms</title>
<link rel="stylesheet" href="http://code.jquery.com/mobile/1.1.0-rc.1/jquery.mobile-1.1.0-rc.1.min.css" />
<script src="http://code.jquery.com/jquery-1.7.1.min.js"></script>
<script src="http://code.jquery.com/mobile/1.1.0-rc.1/jquery.mobile-1.1.0-rc.1.min.js"></script>
</head>
<body>
<form action="process.php" method="post">
<label for="username">Email: </label>
<input type="email" name="username" value="" id="username"/>
<br/>
<label for="password">Password: </label>
<input type="password" name="password" value="" id="password"/>
<br/>
<input type="submit" name="submit" value="Sign in"/>
<input type="submit" name="submit" value="Sign up"/>
</form>
</body>
</html>
Process.php
<?php
//1. Create a database connection
$connection = mysql_connect("localhost","web","1234") or die("Database connection failed: " . mysql_error());
//2. Select a database to use
$db_select = mysql_select_db("tongue", $connection) or die("Database selection failed: " .mysql_error());
?>
<html>
<head>
<title>Form processing</title>
<link rel="stylesheet" href="http://code.jquery.com/mobile/1.1.0-rc.1/jquery.mobile-1.1.0-rc.1.min.css" />
<script src="http://code.jquery.com/jquery-1.7.1.min.js"></script>
<script src="http://code.jquery.com/mobile/1.1.0-rc.1/jquery.mobile-1.1.0-rc.1.min.js"></script>
</head>
<body>
<?php
$username = $_POST['username'];
$password = trim($_POST['password']);
$hashed_password = sha1($password);
$action = $_POST['submit'];
//3. Select email, password from database
$query = "
SELECT email, password FROM user
WHERE email='$username' AND password='$hashed_password'";
$user = mysql_query($query, $connection)
or die ("Database query failed: ".mysql_error());
$count = mysql_num_rows($user) or die ("Database count failed: ".mysql_error());
//4. Authenticate user
if ($count == 1) {
if($action="Sign up"){
echo "User already exists";
} else if ($action="Sign in"){
echo "User signed in";
};
} else if ($count == 0){
if($action="Sign up"){
$query = "
INSERT INTO users (email, password)
VALUES ('$username', '$hashed_password')";
$signup = mysql_query($query, $connection);
echo "User account created";
} else if ($action ="Sign in"){
echo "Username and/or password incorrect";
};
};
?>
</body>
</html>
答案 0 :(得分:0)
这一行
$count = mysql_num_rows($user) or die ("Database count failed: ".mysql_error());
当$ count == 0时会死,因为0会被评估为false。当您处理没有返回任何行的情况时,不必中止$ count == 0。另外,您已经检查过查询执行成功,所以这实际上不太可能失败。
如果您想检查失败,请检查结果是否为假,而非'错误'
$count = mysql_num_rows($user);
if ($count === false)
{
die ("Database count failed: ".mysql_error());
}
请注意使用===
而非==
,这可确保我们检查类型和值。
答案 1 :(得分:0)
我建议您先使用简单的SELECT * FROM user
测试连接并打印出元素。如果连接正常,则SQL查询中可能存在错误。 $user
变量应该包含资源,否则mysql_num_rows()
将失败。使用is_resource()
验证mysql_query()
返回的对象实际上是否有效。
答案 2 :(得分:0)
尝试运行此示例:
$a = 0 OR die('foobar');
var_dump($a);
您会看到OR die()
会在价值为considered false的任何时候执行。
那说..你的代码非常糟糕。
mysql_*
API已超过10年,不应在任何新编写的代码中使用。相反,您应该学会使用PDO或MySQLi。这就像我会这样做:
$connection = new PDO( 'mysql:host=hostname;dbname=tongue', 'web', '1234');
$connection->setAttribute( PDO::ATTR_EMULATE_PREPARES, false );
$statement = $connection->prepare('
SELECT
email,
hash, -- instead of password, because thats what it is
salt
FROM user
WHERE email = :username
');
$statement->bindParam( ':username', $_POST['username'], PDO::PARAM_STR, 127);
if ( $statement->execute() )
{
$data = $statement->fetch( PDO::FETCH_OBJ );
}
if ( $data )
{
$hash = crypt( $_POST['password'], '$2a$07$' . $data->salt . '$' );
if ( $hash === $data->hash )
{
// user OK
}
else
{
// login failed
}
}
else
{
//login failed
}
这显然是一个简化版本,因为,如果您阅读'crypt()`的手动输入,您会注意到生成的哈希已包含原始盐。你可以从中提取。但目的是要提出一个观点,而不是添加太多细节。
此外,登录的if
条件可能已被包装成单个if,因为当您执行if( $condition_1 && $condition_2 && $condition_3 )
时,PHP将在第一个失败的条件下停止检查。
答案 3 :(得分:0)
这是最终的代码(非常感谢@_tereko):
<?php
try{
$username_signup = "Tst@gmail.com";
$password_signup = "est";
$hash = crypt($password_signup, '$3a$08$2'); // salt
$connection = new PDO ('mysql:host=localhost;dbname=tongue', 'web', '1234');
$connection -> setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$connection -> setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
function login ($query, $connect, $user) {
$statement = $connect->prepare($query);
$statement -> bindParam(':username', $user, PDO::PARAM_STR, 127);
$statement -> execute();
$data = $statement->fetch (PDO::FETCH_OBJ); // fetches the columns defined as $property
return $data;
}
function create ($query, $connect, $user, $pass) {
$statement = $connect->prepare($query);
$statement -> bindParam(':username', $user, PDO::PARAM_STR, 127);
$statement -> bindParam(':password', $pass, PDO::PARAM_STR, 127);
$statement -> execute();
}
$sql = 'SELECT email, hash FROM user WHERE email=:username'; // must be defined before calling
if ($row = login ($sql, $connection, $username_signup)) {
echo "Account already exists!";
}
else {
$sql = 'INSERT INTO user(email, hash) VALUES (:username, :password)';
create($sql, $connection, $username_signup, $password_signup);
echo "account created";
};
$connection = null;
} catch(PDOException $e) {
echo $e->getMessage();
}
?>