登录表单不断返回
Login Form keeps coming back
这是我的登录页面:
" 这是表单,表单 action="" 设置为 none(可能是问题所在?)
$query=mysql_query("SELECT * FROM login WHERE username='".$user."' AND password='".$pass."'");
$numrows=mysql_num_rows($query);
if($numrows!=0)
{
while($row=mysql_fetch_assoc($query))
{
$dbusername=$row['username'];
$dbpassword=$row['password'];
}
if($user == $dbusername && $pass == $dbpassword)
{
session_start();
$_SESSION['sess_user']=$user;
/* Redirect browser */
header("Location: member.php");
}
} else {
echo "Invalid username or password!";
}
第二页
<?php
session_start();
if(!isset($_SESSION["sess_user"])){
header("location:login.php");
} else {
每当我单击 'log in' 按钮时,表单会返回而不是页面重定向我.. 请帮助,
提前致谢
我认为 header() 引发了很多错误。所以我使用的是:
<meta http-equiv='refresh' content="0; url=yoururlhere"
保持原样,它会将您重定向到一个新页面 :) 与 header 相同。这应该可以解决您的问题。
编辑
好的,这里有一些更新可以帮助提高安全性,应该可以解决您的问题! :)
首先让我们从数据库连接文件开始:
<?php
$db_username = ""; //Input your database username here
$db_password = ""; //Input your database password here
$db_host = ""; //Input your database host here
$db_name = ""; //Input your database name here
$options = array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8');
try {
$connection = new PDO("mysql:host={$db_host};dbname={$db_name};charset=utf8", $db_username, $db_password, $options);
$connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch(PDOException $ex) {
echo "Can not connect to database.";
}
header('Content-Type: text/html; charset=utf-8');
?>
以上将为您连接到您的数据库! :)
好的,接下来让我们使用 PHP(请在您输入之前将其放在您的顶部):
<?php
//Grabs the database connection
require("path to database connection file");
$user = $_POST["user"];
$pass = $_POST["pass"];
$query="SELECT * FROM login WHERE username=:username AND password=:password";
$params->execute(array(':username' => $user,
':password' => $pass));
try{
$stmt = $connection->prepare($query);
$result = $stmt->execute($params);
}
catch(PDOException $ex){
echo ("Failed to run query: " . $ex->getMessage());
}
$fetch = $stmt->fetch();
if($fetch) {
while($row=mysql_fetch_assoc($query)){
$usernamefetch=$fetch['username'];
$passwordfetch=$fetch['password'];
}
if($user == $usernamefetch && $pass == $passwordfetch)
session_start();
$_SESSION['sess_user']=$user;
?>
<meta http-equiv='refresh' content="0; url=page_to_redirect_to"
<?php
{
else {
echo "Invalid username or password!";
}
}
else {
echo "Invalid username or password!";
}
?>
这就是代码,它应该可以完美运行! :) 您应该确保更改的唯一一件事是重定向到何处的 mets 标记。然后当然是你的简单 HTML:
<form action="" method="POST">
Username: <input type="text" name="user">
Password: <input type="password" name="pass">
<input type="submit" value="Login" name="submit" />
</form>
请测试这段代码,我认为它现在会工作得更好! :) 更不用说它会更安全。
编辑
终于,我 100% 成功了!!! :) 好的,现在使用这个:
<?php
$db_username = "peoplein"; //Input your database username here
$db_password = "xxxxxxx"; //Input your database password here
$db_host = "localhost"; //Input your database host here
$db_name = "xxxxxxxx"; //Input your database name here
$options = array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8');
try {
$connection = new PDO("mysql:host={$db_host};dbname={$db_name};charset=utf8", $db_username, $db_password, $options);
$connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch(PDOException $ex) {
echo "Cannot connect to database.";
}
header('Content-Type: text/html; charset=utf-8');
?>
<!doctype html>
<html>
<head>
<title>Login</title>
</head>
<body>
<?php
if (isset($_POST["user"])) {
$user = $_POST["user"];
}
else {
$user = "";
}
if (isset($_POST["pass"])) {
$pass = $_POST["pass"];
}
else {
$pass = "";
}
$query="SELECT * FROM login WHERE username=:username";
$params=(array(':username' => $user));
try{
$stmt = $connection->prepare($query);
$result = $stmt->execute($params);
}
catch(PDOException $ex){
echo ("Failed to run query: " . $ex->getMessage());
}
$fetch = $stmt->fetch();
$passwordfetch=$fetch['password'];
if (password_verify($pass, $passwordfetch)) {
session_start();
$_SESSION['sess_user']=$user;
?>
<meta http-equiv='refresh' content="0; url=http://peopleinvestment.ro/filip/admin.php">
<?php
}
else {
echo "Invalid Information";
}
?>
<center>
<h3>Login</h3>
<form action="" method="POST">
Username: <input type="text" name="user">
Password: <input type="password" name="pass">
<input type="submit" value="Login" name="submit" />
</form>
</body>
</html>
除了 db_password 和 db_name 之外,不要更改任何内容。现在你应该改变的下一件事。首先,如果有人进入了您的数据库,您的密码就不安全,因为它们不是 "hashed"。现在您可以在将这些密码提交到数据库之前通过执行此操作来散列这些密码:
password_hash($pass, PASSWORD_DEFAULT);
所以基本上在用户要注册之前会自动password_hash,然后输入到数据库中。现在必须完成,否则上面的代码将不起作用。然而,如果您选择不 password_hash(坏主意),那么您可以只使用此代码:http://pastebin.com/SJisBwnB。那应该可以解决您的问题,如果仍然出现任何错误或者您有任何疑问,请再发表评论! :) 这对我来说非常有效。
为什么不这样试试:
<?php
session_start();
$query = mysql_query("SELECT * FROM login WHERE username='" . $user . "' AND password='" . $pass . "'");
$numrows = mysql_num_rows($query);
if ($numrows > 0) {
while ($row = mysql_fetch_assoc($query)) {
$dbusername = $row['username'];
$dbpassword = $row['password'];
}
$_SESSION['sess_user'] = $user;
header("Location: http://peopleinvestment.ro/filip/member.php");
} else {
echo "Invalid username or password!";
}
它也更智能、更短,因为您要检查 2 次用户名和密码是否匹配
我实际上找到了另一种解决方法:
基本上:
echo "<script>
top.location='admin.php'
</script>"
这是我的登录页面:
" 这是表单,表单 action="" 设置为 none(可能是问题所在?)
$query=mysql_query("SELECT * FROM login WHERE username='".$user."' AND password='".$pass."'");
$numrows=mysql_num_rows($query);
if($numrows!=0)
{
while($row=mysql_fetch_assoc($query))
{
$dbusername=$row['username'];
$dbpassword=$row['password'];
}
if($user == $dbusername && $pass == $dbpassword)
{
session_start();
$_SESSION['sess_user']=$user;
/* Redirect browser */
header("Location: member.php");
}
} else {
echo "Invalid username or password!";
}
第二页
<?php
session_start();
if(!isset($_SESSION["sess_user"])){
header("location:login.php");
} else {
每当我单击 'log in' 按钮时,表单会返回而不是页面重定向我.. 请帮助,
提前致谢
我认为 header() 引发了很多错误。所以我使用的是:
<meta http-equiv='refresh' content="0; url=yoururlhere"
保持原样,它会将您重定向到一个新页面 :) 与 header 相同。这应该可以解决您的问题。
编辑
好的,这里有一些更新可以帮助提高安全性,应该可以解决您的问题! :) 首先让我们从数据库连接文件开始:
<?php
$db_username = ""; //Input your database username here
$db_password = ""; //Input your database password here
$db_host = ""; //Input your database host here
$db_name = ""; //Input your database name here
$options = array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8');
try {
$connection = new PDO("mysql:host={$db_host};dbname={$db_name};charset=utf8", $db_username, $db_password, $options);
$connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch(PDOException $ex) {
echo "Can not connect to database.";
}
header('Content-Type: text/html; charset=utf-8');
?>
以上将为您连接到您的数据库! :) 好的,接下来让我们使用 PHP(请在您输入之前将其放在您的顶部):
<?php
//Grabs the database connection
require("path to database connection file");
$user = $_POST["user"];
$pass = $_POST["pass"];
$query="SELECT * FROM login WHERE username=:username AND password=:password";
$params->execute(array(':username' => $user,
':password' => $pass));
try{
$stmt = $connection->prepare($query);
$result = $stmt->execute($params);
}
catch(PDOException $ex){
echo ("Failed to run query: " . $ex->getMessage());
}
$fetch = $stmt->fetch();
if($fetch) {
while($row=mysql_fetch_assoc($query)){
$usernamefetch=$fetch['username'];
$passwordfetch=$fetch['password'];
}
if($user == $usernamefetch && $pass == $passwordfetch)
session_start();
$_SESSION['sess_user']=$user;
?>
<meta http-equiv='refresh' content="0; url=page_to_redirect_to"
<?php
{
else {
echo "Invalid username or password!";
}
}
else {
echo "Invalid username or password!";
}
?>
这就是代码,它应该可以完美运行! :) 您应该确保更改的唯一一件事是重定向到何处的 mets 标记。然后当然是你的简单 HTML:
<form action="" method="POST">
Username: <input type="text" name="user">
Password: <input type="password" name="pass">
<input type="submit" value="Login" name="submit" />
</form>
请测试这段代码,我认为它现在会工作得更好! :) 更不用说它会更安全。
编辑
终于,我 100% 成功了!!! :) 好的,现在使用这个:
<?php
$db_username = "peoplein"; //Input your database username here
$db_password = "xxxxxxx"; //Input your database password here
$db_host = "localhost"; //Input your database host here
$db_name = "xxxxxxxx"; //Input your database name here
$options = array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8');
try {
$connection = new PDO("mysql:host={$db_host};dbname={$db_name};charset=utf8", $db_username, $db_password, $options);
$connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch(PDOException $ex) {
echo "Cannot connect to database.";
}
header('Content-Type: text/html; charset=utf-8');
?>
<!doctype html>
<html>
<head>
<title>Login</title>
</head>
<body>
<?php
if (isset($_POST["user"])) {
$user = $_POST["user"];
}
else {
$user = "";
}
if (isset($_POST["pass"])) {
$pass = $_POST["pass"];
}
else {
$pass = "";
}
$query="SELECT * FROM login WHERE username=:username";
$params=(array(':username' => $user));
try{
$stmt = $connection->prepare($query);
$result = $stmt->execute($params);
}
catch(PDOException $ex){
echo ("Failed to run query: " . $ex->getMessage());
}
$fetch = $stmt->fetch();
$passwordfetch=$fetch['password'];
if (password_verify($pass, $passwordfetch)) {
session_start();
$_SESSION['sess_user']=$user;
?>
<meta http-equiv='refresh' content="0; url=http://peopleinvestment.ro/filip/admin.php">
<?php
}
else {
echo "Invalid Information";
}
?>
<center>
<h3>Login</h3>
<form action="" method="POST">
Username: <input type="text" name="user">
Password: <input type="password" name="pass">
<input type="submit" value="Login" name="submit" />
</form>
</body>
</html>
除了 db_password 和 db_name 之外,不要更改任何内容。现在你应该改变的下一件事。首先,如果有人进入了您的数据库,您的密码就不安全,因为它们不是 "hashed"。现在您可以在将这些密码提交到数据库之前通过执行此操作来散列这些密码:
password_hash($pass, PASSWORD_DEFAULT);
所以基本上在用户要注册之前会自动password_hash,然后输入到数据库中。现在必须完成,否则上面的代码将不起作用。然而,如果您选择不 password_hash(坏主意),那么您可以只使用此代码:http://pastebin.com/SJisBwnB。那应该可以解决您的问题,如果仍然出现任何错误或者您有任何疑问,请再发表评论! :) 这对我来说非常有效。
为什么不这样试试:
<?php
session_start();
$query = mysql_query("SELECT * FROM login WHERE username='" . $user . "' AND password='" . $pass . "'");
$numrows = mysql_num_rows($query);
if ($numrows > 0) {
while ($row = mysql_fetch_assoc($query)) {
$dbusername = $row['username'];
$dbpassword = $row['password'];
}
$_SESSION['sess_user'] = $user;
header("Location: http://peopleinvestment.ro/filip/member.php");
} else {
echo "Invalid username or password!";
}
它也更智能、更短,因为您要检查 2 次用户名和密码是否匹配
我实际上找到了另一种解决方法:
基本上:
echo "<script>
top.location='admin.php'
</script>"