登录表格不断回来

时间:2015-02-26 22:08:59

标签: php forms login logout back

这是我的登录页面:

“这是表格,表格动作=”“设置为无(可能是问题?)

< form action =“”method =“POST”>
用户名:< input type =“text”name =“user”>
密码:< input type =“password”name =“pass”>
< input type =“submit”value =“Login”name =“submit”/>
< /形式>“中

$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 {

每当我点击“登录”按钮时,表单会返回而不是重定向我的页面..请帮助,

提前致谢

3 个答案:

答案 0 :(得分:0)

为什么不试试这样:

    <?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次

答案 1 :(得分:0)

我实际上找到了另一种方式:

基本上:

echo "<script>
top.location='admin.php'
</script>"

答案 2 :(得分:-1)

我认为header()引发了很多错误。所以我使用的是:

<meta http-equiv='refresh' content="0; url=yoururlhere"

保持原样,它会将您重定向到新页面:)与标题相同。这应该可以解决你的问题。

修改

好的,这里有一些更新有助于提高安全性,它应该可以解决您的问题! :) 首先让我们从数据库连接文件开始:

<?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。接下来你应该改变的事情。首先,如果有人进入您的数据库,您的密码不安全,因为它们没有“哈希”。现在,您可以在将这些密码提交到数据库之前执行此操作来散列这些密码:

password_hash($pass, PASSWORD_DEFAULT);

所以基本上在用户即将注册之前它会自动password_hash,然后将其输入到数据库中。现在必须完成,否则上面的代码将无效。然而,如果您选择不使用password_hash(糟糕的主意),那么您可以使用此代码:http://pastebin.com/SJisBwnB。这应该可以解决您的问题,如果仍有任何错误,或者您有任何疑问,请再发表评论! :)这对我来说很完美。