<?php
ob_start();
echo "<body><p>Hello "
if ($condition) {
header( "Location: http://www.google.com/" );
exit;
}
echo " World!</p></body>";
ob_end_flush();
?>
当$condition
是真的,我得到这样的:
<body>Hello
我要的是当$condition
将是真,那么去谷歌!
我不知道发生了什么,你能解释一下或者给我一个解决方案!?
谢谢。
只需添加ob_end_clean();
前标题呼叫。
一切都没有问题,只是把一个;
后回声“ <body><p>Hello
”,你会被罚款..
如果我是你,我就已经开始什么可能出错第然后做处理。
一个例子
$exit_condition_1 = some_value1;
$exit_condition_2 = some_value2;
if($exit_condition_1 == false){
//Redirect
//Exit
}
if(!$exit_condition_2){
//Redirect
//Exit
}
//start the buffer ob_start()
//show some HTML
//flash the buffer ob_end_clean()
there is no point of starting the buffer then if something goes wrong close it and redirect. Just do value testing at the begining then process the request.
An example: lets say that you want to view a product's info and you have a function that will do that
function view_product($product_id){
if(!$product = getProductById($product_id)){
//product does not exist, redirect
}
if(the user does not have enough access rights){
//show a message maybe
//redirect
}
//everything is alright then show the product info
}