Linux论坛's Archiver

《开源》旗舰电子杂志2009年第1期火热下载!

bg24254 发表于 2008-9-6 01:48

mysterious php header relocation

Recently I have encountered non-working header redirect issue
<?php
//some process code here
//........

header("Location: [url]http://www.mysite.com[/url]");
exit;

this code above is not working and there is no error been caught. It took me whole alot time for debugging the root of problem,
and still no luck in finding.

After went through the php.net, I found out this very useful function for debugging such issue
headers_sent();

The usage is follow,

if(headers_sent($file,$line)){
echo "header is already sent in {$file} at line {$line}";
}

this will spit out the cause of error as it occur. It saves me alot time when the such error is not so
obvious.

I hope my mistake and findings can save other people's some times.

grassofhust 发表于 2008-9-6 10:18

header() must be called before any actual output is sent

页: [1]

Powered by Discuz! Archiver 7.0.0  © 2001-2009 Comsenz Inc.