在使用ob_start时,如何在php中使用头位置重定向?

在使用ob_start时,如何在php中使用头位置重定向?,php,header,location,ob-start,Php,Header,Location,Ob Start,我想要的是当$condition为真时,然后去谷歌 我不知道发生了什么,你能解释一下或者给我一个解决办法吗 谢谢。只需添加ob_end_clean()在标题调用之前。如果我是你,我会先开始可能出错的操作,然后再进行处理 一个例子 <body>Hello 一切都应该正常,只要放一个在echo“您好”之后,您将可以解决类似的情况,即函数使用ob_start()并且存在标题(“位置:http://www.example.com");之后,但出现错误“已发送…”,我将标题(…调用替换为 $

我想要的是当
$condition
为真时,然后去谷歌

我不知道发生了什么,你能解释一下或者给我一个解决办法吗


谢谢。

只需添加
ob_end_clean()在标题调用之前。

如果我是你,我会先开始可能出错的操作,然后再进行处理

一个例子

<body>Hello

一切都应该正常,只要放一个
在echo“
您好
”之后,您将可以解决类似的情况,即函数使用ob_start()并且存在
标题(“位置:http://www.example.com");之后,但出现错误“已发送…”,我将
标题(…
调用替换为

$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

}
echo“window.location.href=”https://www.example.com' "
它在这种特殊情况下起了作用(所需要的只是一个页面重定向)。

IMO,ob_start()可以删除,我不能删除ob_start(),因为我需要在写入客户端之前处理输出。
$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

}
echo "<script> window.location.href = 'https://www.example.com' </script>"