PostGreSQL 和 PHP 的登录脚本不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2505149/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Login Script for PostGreSQL and PHP not working
提问by MrEnder
I tried this so far:
到目前为止我试过这个:
<?php
$error = "";
$conn = pg_connect("host=localhost dbname=brittains_db user=brittains password=XXXX" );
$sql = "SELECT * FROM logins";
$result = pg_query($conn, $sql);
if($_SERVER["REQUEST_METHOD"] == "GET") {
$userName="";
$password="";
}
else if($_SERVER["REQUEST_METHOD"] == "POST") {
$userName=trim($_POST["userNameLogin"]);
$password=trim($_POST["passwordLogin"]);
if(pg_fetch_result($results, $userName, "userName")==true
&& pg_fetch_result($results, $password, "userName")==true) {
setcookie("userIDforDV", $userName, time()+43200);
}
else {
$error = "Your username and or password is incorrect";
}
}
$userName = $_COOKIE['userIDforDV'];
if(isset($userName) && $userName!="") {
echo "Welcome " . $userName;
}
echo $error;
?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<table>
<tr>
<td class="signupTd">
User Name:
</td>
<td>
<input type="text" name="userNameLogin" value="" size="20" />
</td>
</tr>
<tr>
<td class="signupTd">
Password:
</td>
<td>
<input type="password" name="passwordLogin" value="" size="20" />
</td>
</tr>
<tr>
<td class="signupTd" colspan="2">
<input type="submit" name="submit" value="Submit"/>
</td>
</tr>
</table>
</form>
That was the idea I came up with... but its probably a really bad idea and it doesn't work... how might I go about this properly? I need really detailed descriptions please.
那是我想出的主意……但它可能是一个非常糟糕的主意,而且行不通……我该如何正确地解决这个问题?我需要非常详细的描述。
By the way, my SQL is:
顺便说一下,我的 SQL 是:
CREATE TABLE logins(
userName VARCHAR(25) NOT NULL PRIMARY KEY,
password VARCHAR(25) NOT NULL,
firstName VARCHAR NOT NULL,
lastName VARCHAR NOT NULL,
ageDay INTEGER NOT NULL,
ageMonth INTEGER NOT NULL,
ageYear INTEGER NOT NULL,
email VARCHAR(255) NOT NULL,
createDate DATE
);
and my registration form has already been made and is working and I do have users in my database, they just can't login.
我的注册表已经制作完成并且正在运行,我的数据库中确实有用户,但他们无法登录。
回答by OneOfOne
check http://php.net/manual/en/function.pg-fetch-result.php's comments. edit : password should be varchar(32) and use md5 passwords (or even better 64 and use sha1). also I'm not familiar with pgSQL in general, however IMO the correct way would be to use an sql query to check instead of using pg_fetch_result.
检查http://php.net/manual/en/function.pg-fetch-result.php的评论。编辑:密码应该是 varchar(32) 并使用 md5 密码(甚至更好的 64 并使用 sha1)。我一般也不熟悉 pgSQL,但是 IMO 正确的方法是使用 sql 查询来检查而不是使用 pg_fetch_result。
$query = "SELECT * FROM logins WHERE userName = '$userName' AND password = md5('$password');";
$result = pg_query($conn, $query);
if(pg_num_rows($result) != 1) {
// do error stuff
} else {
// user logged in
}
回答by Kamikaze478
If you would like to see a phenominal login script in action (and thereby learn from it) Check out jpmaster77's core: http://evolt.org/node/60384It has a strong class based infrastructure and takes full advantage of your object-oriented background. It works out of the box & is free.
如果您想看到一个现象级的登录脚本在运行(并从中学习),请查看 jpmaster77 的核心:http://evolt.org/node/60384它具有强大的基于类的基础结构并充分利用您的对象-定向背景。它开箱即用且免费。
glhf
格鲁夫