2013-10-06 68 views
0

好的,這是我的問題。我爲幾頁創建了一個簡單的登錄系統。這一切都有效。我剛剛創建了一個PHP表單提交輸入到MySQL數據庫的另一頁。這一切都工作以及...在我的網站上的其他網頁上。
出於某種原因,當我提出這種形式,它一直重定向到我的登錄頁,我不知道爲什麼。下面是形式/代碼,我試圖提交:PHP表單錯誤,保持重定向登錄頁面

<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 

我的其他網頁都很好,但由於某些原因,一個表格/搜索頁面不斷重定向到我的登錄頁面。任何幫助是極大的讚賞。我希望這是有道理的大聲笑。

+0

什麼是你的admin_logincheck?似乎登錄不起作用 – wernersbacher

+0

我會編輯我的帖子並添加它。 admin_logincheck的作品,只要我添加此頁面。 – dkeeper09

+0

所以你是腳本重定向你admin_index.php登錄=失敗和原因= WrongPassword? – wernersbacher

回答

0

替換此:

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

與此:

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

請注意,如果(!empty($ some_var))...就夠了。您不需要首先使用isset($ some_var),因爲empty()已經這樣做了(並且它將未設置的變量視爲空)。 –

+0

嘿老闆,那沒用。它不會讓我登錄,只需要我到admin_index.php – dkeeper09