简体   繁体   English

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

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

code: sql.php代码: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();
?>

code: login.html代码:登录.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>

All the files are in the same root directory, submitting the userid and password through login.html to sql.php should redirect to the file codepage.php (I havent added the login check yet, so should succeed for any values of username as password)所有文件都在同一个根目录下,通过login.html提交userid和password到sql.php应该重定向到文件codepage.php(我还没有添加登录检查,所以用户名作为密码的任何值都应该成功)

But when I try to do so, All I get is a blank page.但是当我尝试这样做时,我得到的只是一个空白页。

If I add an output statement after the header call, I see the output of that statement如果我在 header 调用之后添加 output 语句,我会看到该语句的 output

Essentially, header("Location: codepage.php") had no effect本质上, header("Location: codepage.php")没有效果

The header function does work in some other pages on the same site header function 在同一站点的其他一些页面中确实有效

The header('Location: address') should contain the ENTIRE url, not just the file. header('Location: address')应该包含整个 url,而不仅仅是文件。

This means the protocol, domain and alternatively folders and files.这意味着协议、域以及文件夹和文件。

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

Docs:文档:

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

Here are the w3 specifications on the Location header at point 14.30 (search for '14.30 Location')这是位置 header 点 14.30 上的 w3 规范(搜索“14.30 位置”)

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

To qoute the w3:引用 w3:

The field value consists of a single absolute URI.字段值由单个绝对 URI 组成。

As noted in the comments, there may not be any whitespace before or after the the colon (:).如评论中所述,冒号 (:) 前后可能没有任何空格。

Use curly brackets. 使用大括号。 Your code is broken the way it is. 您的代码已按原样损坏。 Also, it's header('Location: codepage.php') , not header('Location: codepage.php') .此外,它是header('Location: codepage.php') ,而不是header('Location: codepage.php') Notice the extra space you've added.请注意您添加的额外空间。

REMOVED移除

Your codes are vulnerable to Sql Injection .您的代码容易受到Sql Injection的攻击。 Use mysql_real_escape_string() to filter.使用mysql_real_escape_string()进行过滤。 Like:像:

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

And make sure that you have corrent address to codepage.php.并确保您有 codepage.php 的正确地址。 Try尝试

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

please used header("Location: codepage.php");请使用 header("Location: codepage.php");

the problem is the space between Location and: it's working for me, hope it will work for u问题是 Location 和之间的空间:它对我有用,希望它对你有用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM