开发者

Redirect Function Php

开发者 https://www.devze.com 2023-03-28 22:03 出处:网络
I have a function, \'redirect_to()\' written on php script that is called after a successful update to a page on my custom CMS. It works fine on the localhost, but when I try it on my actual live doma

I have a function, 'redirect_to()' written on php script that is called after a successful update to a page on my custom CMS. It works fine on the localhost, but when I try it on my actual live domain I get the following error message:

Warning: Cannot modify header information - headers already sent by (ou开发者_StackOverflow中文版tput started at /hermes/bosweb/web119/b1192/ipg.typaldosnetcom/edit_listing.php:7) in /hermes/bosweb/web119/b1192/ipg.typaldosnetcom/includes/functions.php on line 20

Here is the code for the redirect_to() function:

function redirect_to ($location = NULL) {
    if ($location != NULL) {
        header("Location: {$location}");
        exit;
    }
}

I've made sure to call the function before I output any HTML, so I'm not sure what the problem really is.

My question: Why am I receiving this error?


It's not lying. You've output something before getting to this point. Check the locations mentioned in the error messages.

Show us the first 25 lines of each of the files mentioned.


you already sent your output to the page before you set the header. first you need to set the headers and then can the output come.

It can even be a whitespace.


It means something was already outputted on the suggested line. Try going there and see what it does.

Try pasting the surrounding code on that position for a better clarification if you can't find the problem yourself.


One common cause is to have a line after a php file you're including...

Simple solution: remove the closing php tag "?>" from all files as it's not needed..


You can test if you have a character before the opening php-script tag by removing any closing php-script tag. This way you are sure there isn't any character left (it's not needed).


Use output buffering:

<?php 
ob_start();

// Test buffered output. 
echo 'hello world'; 

function redirect_to ($location = NULL) {
    if ($location != NULL) {
        header('Location: ' . $location);
        exit;
    }
}

// rest of php file here

ob_end_flush();
?>

Docs: ob_start() and ob_end_flush()

0

精彩评论

暂无评论...
验证码 换一张
取 消