php 无法修改标头信息 - 标头已由... WordPress 问题发送

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7381661/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 02:35:03  来源:igfitidea点击:

Cannot modify header information - headers already sent by... WordPress Issue

phpwordpress

提问by Ben Daggers

I'm encountering this error. and I have no idea dealing with this.

我遇到了这个错误。我不知道处理这个。

Cannot modify header information - headers already sent by (output started at /home/ben213/public_html/wp-content/themes/Bendaggers/functions.php:9) in /home/ben213/public_html/wp-includes/pluggable.php on line 934

无法修改标题信息 - 标题已由 /home/ben213/public_html/wp-includes/pluggable.php 中的(输出开始于 /home/ben213/public_html/wp-content/themes/Bendaggers/functions.php:9)发送934线

my Functions.php file line # 9 is:

我的 Functions.php 文件第 9 行是:

<?php if(function_exists('register_sidebar'))register_sidebar();?>

while my pluggable.php # 934 is

而我的 pluggable.php # 934 是

function wp_redirect($location, $status = 302) {
    global $is_IIS;

    $location = apply_filters('wp_redirect', $location, $status);
    $status = apply_filters('wp_redirect_status', $status, $location);

    if ( !$location ) // allows the wp_redirect filter to cancel a redirect
        return false;

    $location = wp_sanitize_redirect($location);

    if ( !$is_IIS && php_sapi_name() != 'cgi-fcgi' )
        status_header($status); // This causes problems on IIS and some FastCGI setups

    header("Location: $location", true, $status);}
endif;

I'm having a hard time figuring this out since im not a programmer. what seems to be wrong? kindly help me please...

由于我不是程序员,我很难弄清楚这一点。似乎有什么问题?请帮助我...

回答by hardy101

Your theme is printing output (text) to the browser, but then for some reason WordPress is redirecting the user (with wp_redirect) away from that page before the whole page is rendered. You can't start printing output and then redirect, or you'll get the error you see. That's what Paul Grime was getting at in his comment.

您的主题正在将输出(文本)打印到浏览器,但是由于某种原因,WordPress 在呈现整个页面之前将用户(使用 wp_redirect)从该页面重定向。你不能开始打印输出然后重定向,否则你会看到你看到的错误。这就是 Paul Grime 在他的评论中的意思。

Ken White commented with a reference to a post with a similar problem. I've fixed this in my own experience by buffering the output of the script.

肯·怀特 (Ken White) 评论了一篇有类似问题的帖子。我已经根据自己的经验通过缓冲脚本的输出解决了这个问题。

In your theme's functions.phpfile (which gets included every time your theme's pages load), put the following:

在您的主题functions.php文件(每次主题页面加载时都会包含)中,输入以下内容:

//allow redirection, even if my theme starts to send output to the browser
add_action('init', 'do_output_buffer');
function do_output_buffer() {
        ob_start();
}

Now, even if part of your theme starts to send input to the browser, PHP won't send that text until the page is fully loaded, which allows WordPress to redirect users, if necessary, as part of its own logic.

现在,即使您的主题的一部分开始向浏览器发送输入,PHP 也不会在页面完全加载之前发送该文本,这允许 WordPress 在必要时重定向用户,作为其自身逻辑的一部分。

回答by Pir Fahim Shah

If you are trying to redirect to another page from your current page, where you have impose a condition or without condition, then use this code. E.gs you have two pages A.php, & B.php and currenty you are in A.php where you want to go to other page B.php on clicking the BUTTON.

如果您尝试从当前页面重定向到另一个页面,在那里您有条件或没有条件,请使用此代码。例如,您有两个页面 A.php 和 B.php,并且当前您在 A.php 中,您想在单击按钮时转到其他页面 B.php。

   if(isset($_POST['save_btn']))
    {
        //write some of your code here, if necessary
        echo'<script> window.location="B.php"; </script> ';
     }