PHP标头重定向不起作用 [英] PHP Header redirect not working

查看:83
本文介绍了PHP标头重定向不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

include('header.php');

$name = $_POST['name'];
$score = $_POST['score'];
$dept = $_POST['dept'];

$MyDB->prep("INSERT INTO demo (`id`,`name`,`score`,`dept`, `date`) VALUES ('','$name','$score','$dept','$date')");
// Bind a value to our :id hook
// Produces: SELECT * FROM demo_table WHERE id = '23'
$MyDB->bind(':date', $date);
// Run the query
$MyDB->run();

header('Location:index.php');
    exit;

上面的代码不断给我重定向的问题.错误如下:

The above code keeps giving me an issue with the redirect. The error is the following:

警告:无法修改标头信息-已由(输出的标头发送) 从/Applications/MAMP/htdocs/testygubbins/OO/test/header.php:15开始 /Applications/MAMP/htdocs/testygubbins/OO/test/form.php,第16行.

Warning: Cannot modify header information - headers already sent by (output started at /Applications/MAMP/htdocs/testygubbins/OO/test/header.php:15) in /Applications/MAMP/htdocs/testygubbins/OO/test/form.php on line 16.

我对此完全感到困惑.有人知道我应该怎么做才能使它正常工作吗?

I am totally flummoxed by this. Does anyone know what I should be doing to make it work?

编辑

header.php代码:

header.php code:

<?php
include('class.user.php');
include('class.Connection.php');

$date = date('Y-m-j');

?>
<html>
<head>
    <link rel=StyleSheet href="css/style.css" type="text/css" media=screen>
    <title>Test</title>
</head>
<body>
<div id="page">

推荐答案

仔细查看您的收件箱-结帐后您是否有空白行?>?

Look carefully at your includes - perhaps you have a blank line after a closing ?> ?

这将导致一些文字空白作为输出发送,从而阻止您进行后续的标头调用.

This will cause some literal whitespace to be sent as output, preventing you from making subsequent header calls.

请注意,将close?>保留在include文件之外是合法的,这是避免此问题的有用习惯.

Note that it is legal to leave the close ?> off the include file, which is a useful idiom for avoiding this problem.

(查看您的标头,如果要输出标头或使用输出缓冲来捕获它,则需要避免进行任何HTML输出).

最后,正如用于标题的PHP手册页指出的那样,您实际上应该使用完整的URL进行重定向:

Finally, as the PHP manual page for header points out, you should really use full URLs to redirect:

注意:HTTP/1.1要求绝对 URI作为 Location:的参数 包括方案,主机名和 绝对路径,但有些客户接受 相对URI.您通常可以使用 $ _SERVER ['HTTP_HOST'], $ _SERVER ['PHP_SELF']和dirname()到 从亲戚那里制作一个绝对URI 一个人自己:

Note: HTTP/1.1 requires an absolute URI as argument to Location: including the scheme, hostname and absolute path, but some clients accept relative URIs. You can usually use $_SERVER['HTTP_HOST'], $_SERVER['PHP_SELF'] and dirname() to make an absolute URI from a relative one yourself:

这篇关于PHP标头重定向不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆