簡體   English   中英

PHP:使用“header ()”重定向無效

[英]PHP: redirect using "header ()" not working

代碼:sql.php

<?php
$uid = trim($_POST['uid']);
$pass= trim($_POST['pass']);
$type= trim($_POST['type']);
$link = mysql_connect("localhost","root","akash");
if (!$link) 
                 die('Could not connect: '.mysql_error());
if(!mysql_select_db("fsproj",$link))        
die("Can't Select database");
if ($type == 0 ) 
$query = "select uid from admin where uid = \"$uid\" AND password = \"$pass\"";
else
$query = "select username from user where uid = $uid AND pass = $pass";
$result = mysql_query("$query",$link);                        
$num_r=mysql_num_rows($result); 
        header("Location : codepage.php");
exit();
?>

代碼:登錄.html

<html>
<form action="sql.php" method="post" name="form1" id="form1" target=result>
Enter UID

<textarea name="uid" id="uid" rows="1" cols="40"> 

</textarea><br>
Password

<textarea name="pass" id="pass" rows="1" cols="40"> 

</textarea><br>
Type

<textarea name="type" id="type" rows="1" cols="40"> 

</textarea>
<br>
<input type="submit" value="Submit" />
</form>
</html>

所有文件都在同一個根目錄下,通過login.html提交userid和password到sql.php應該重定向到文件codepage.php(我還沒有添加登錄檢查,所以用戶名作為密碼的任何值都應該成功)

但是當我嘗試這樣做時,我得到的只是一個空白頁。

如果我在 header 調用之后添加 output 語句,我會看到該語句的 output

本質上, header("Location: codepage.php")沒有效果

header function 在同一站點的其他一些頁面中確實有效

header('Location: address')應該包含整個 url,而不僅僅是文件。

這意味着協議、域以及文件夾和文件。

header('Location: http://wwww.google.com'); 

文檔:

http://php.net/manual/en/function.header.php

這是位置 header 點 14.30 上的 w3 規范(搜索“14.30 位置”)

http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html

引用 w3:

字段值由單個絕對 URI 組成。

如評論中所述,冒號 (:) 前后可能沒有任何空格。

使用大括號。 您的代碼已按原樣損壞。 此外,它是header('Location: codepage.php') ,而不是header('Location: codepage.php') 請注意您添加的額外空間。

移除

您的代碼容易受到Sql Injection的攻擊。 使用mysql_real_escape_string()進行過濾。 像:

 $pass= mysql_real_escape_string(trim($_POST['pass']));

並確保您有 codepage.php 的正確地址。 嘗試

header('Location: /codepage.php'); //OR
header('Location: http://site.com/to/codepage.php');

請使用 header("Location: codepage.php");

問題是 Location 和之間的空間:它對我有用,希望它對你有用

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM