繁体   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