![](/img/trans.png)
[英]PHP - Self form submission: $_SERVER['PHP_SELF'] OR action=“”?
[英]Form submission with action=“<?=$_SERVER['PHP_SELF']?>” causing logout
請記住此處的初學者模式。 我正在建立一個從數據庫搜索產品的電子商務站點。 我使用的登錄名基於數據庫中的0和1。 當以管理員身份登錄並在admin.php頁面上使用搜索功能時,我注銷或重定向到frontPage。 相同的搜索功能可以在FrontPage上正常工作。
這是我在admin.php上提交表單的部分。
<div id="mainArea">
<form id="searchBox" action="<?=$_SERVER['PHP_SELF']?>" method="post"><input id="search" name="title" type="text" placeholder="Search Products" /><input name="submit" type="submit" value="Search" style="width:75px;height:36px"/></form>
</div>
<div id="show">
<?php
include("searchAdmin.php");
?>
</div>
admin.php頁面的頂部如下所示:
<?php
session_start();
$name = $_SESSION['myusername'];
?>
搜索是在adminSearch.php中:
<?php
if ($_POST['submit']){
include ("connection.php");
$title = (trim($_POST['title'])=="")?
die ("You did not enter any search criteria"):
mysql_real_escape_string($_POST['title']);
$query = "select * from beer where upper(beer_name) like '%".strtoupper($title) ."%' OR upper(beer_descr) like '%". strtoupper($title) ."%' OR upper(beer_categ) like '%". strtoupper($title) ."%'";
if(! ($result = mysql_query($query)))
die ("No matches found. Please search again.</body></html>");
echo "<table border=1>";
while ($rows = mysql_fetch_array($result)) {
echo "<tr><td rowspan=3>" . '<img src="images/'. $rows['beer_image'] . '.jpg"/>'."</td><td>".$rows['beer_name']."</td><td><a href='updateBeer.php?id=". $rows['beer_id'] ."'>Update</a></td></tr>";
echo "<tr><td>".$rows['beer_categ']."</td><td><a href='deleteBeer.php?id=". $rows['beer_id'] ."'>Delete</a></td></tr>";
echo "<tr><td>".$rows['beer_descr']."</td>
<td>".$rows['beer_cost']."</td></tr>";
}
echo "</table>";
}
else include("showAdmin.php");
?>
編輯:忘記顯示我的登錄php的結尾看起來像這樣。 它正確地將我發送到管理頁面,但是當我在管理頁面上進行搜索時,它使我注銷或將我發送到frontPage。
if($count==1){
if($auth==1){
session_start();
$_SESSION['myusername'] = $myusername;
$_SESSION['auth']=$auth;
header("location: /admin.php");
}
elseif($auth==0){
session_start();
$_SESSION['myusername'] = $myusername;
$_SESSION['auth']=$auth;
header("location: /user.php");
}
}
else {
echo "Wrong Username or Password";
}
我的注銷在同一管理頁面上看起來像這樣:
<div id="login1">
<font color="white">Logged in with Administrator privilages as: <?php echo $name ?>.</font><br /><form id="logout" method="post" name="logout" action="logout.php"><input name="logout" type="submit" value="Logout" style="width:75px;height:22px"><br />
</div>
和logout.php讀取:
<?php
if (!empty($_POST['logout'])){
session_start();
session_destroy();
header("location: /frontPage.php");
}
?>
乍一看,您使用的是變量$ result,這可能是問題的一部分。
從我所看到的,您沒有聲明或為$ result賦值,因此if語句中的比較可能未達到您的預期。
我的建議是通過刪除if語句來簡化代碼,以達到調試目的,並查看是否遇到相同的問題。
還添加一些警報框來調試代碼,以便您可以確定代碼的哪一部分將您重定向到注銷代碼。
代替
<?=$_SERVER['PHP_SELF']?>
采用
<?php echo $_SERVER['PHP_SELF']?>
我認為這個簡單的錯誤導致了錯誤。 希望這次運氣能和你在一起。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.