PHP表单出错,保持重定向到登录页面

时间:2013-10-06 18:45:09

标签: php mysql forms login

好的,这是我的问题。我为几页创建了一个简单的登录系统。这一切都有效。我刚刚用php表单创建了另一个页面,将输入提交给mysql数据库。这一切都有效...在我网站的其他页面上。
出于某种原因,当我提交此表单时,它会一直重定向到我的登录页面,我不明白为什么。这是我要提交的表单/代码:

<form method="post" action="admin_home.php?page=search.php">
<p>Search by date range: </p>
From: <input type="text" id="from" name="from" />To: <input type="text" id="to" name="to" />
<BR>
<p>Search by:</p>
Name: <input type="text" id="name" name="name" />
Phone Number: <input type="text" id="phone" name="phone" />
<input name="export" type="submit" value="Search" />
</form>

该表单位于search.php文件中,搜索结果也显示在表单下方。 这是我在admin_home.php页面上的登录代码:

<?php
session_start();
require("admin_logincheck.php");
if (!(isset($_SESSION['name']) && $_SESSION['name'] != '')) {

header ("Location: admin_index.php");

}
?>

....然后显示任何内容。

这是我的admin_logincheck.php页面:

<?php
session_start();
session_name("MemberLogin");


$hostname = "";
$username = "";
$dbname = "";
$password = "";

if($_GET['action'] == "login") {

mysql_connect($hostname, $username, $password) OR DIE ("Unable to connect to database! Please try again later.");
mysql_select_db($dbname);


$name = $_POST['user'];
$q_user = mysql_query("SELECT * FROM users WHERE username='$name'");

if(mysql_num_rows($q_user) == 1) {

$query = mysql_query("SELECT * FROM users WHERE username='$name'");
$data = mysql_fetch_array($query);
if($_POST['pass'] == $data['password']) {
$_SESSION['name'] = $name;
header("Location: admin_home.php"); // This is the page that you want to open if the user successfully logs in to your website.
exit;
}
            else {
                header("Location: admin_index.php?login=failed&cause=".urlencode('Wrong Password'));
                exit;
            }

} 
else {
    header("Location: admin_index.php?login=failed&cause=".urlencode('Invalid User'));
exit;
}
}

// if the session is not registered
//if(session_is_registered("name") == false) {
if ($_SESSION['name'] == false) {   
header("Location: admin_index.php");
}
?>
All

我的其他页面很好,但由于某种原因,一个表单/搜索页面会重定向到我的登录页面。任何帮助是极大的赞赏。我希望它有意义lol。

1 个答案:

答案 0 :(得分:0)

替换它:

if (!(isset($_SESSION['name']) && $_SESSION['name'] != '')) {
header ("Location: admin_index.php");
}

用这个:

if (isset($_SESSION['name']) && !empty($_SESSION['name'])) {
header ("Location: admin_index.php");
}