PHP页面重定向

PHP是否可以在执行函数后进行重定向调用?我正在创建一个函数,希望将其重定向到位于同一根文件夹中的文件。能做到吗


 if {

      //i am using echo here

 }


 else if ($_SESSION['qnum'] > 10) { 

            session_destroy();

            echo "Some error occured.";

            //redirect to user.php

        }


翻阅古今
浏览 646回答 3
3回答

慕桂英4014372

使用javascript作为故障保护将确保用户被重定向(即使标头已经发送)。干得好:// $url should be an absolute urlfunction redirect($url){&nbsp; &nbsp; if (headers_sent()){&nbsp; &nbsp; &nbsp; die('<script type="text/javascript">window.location=\''.$url.'\';</script‌>');&nbsp; &nbsp; }else{&nbsp; &nbsp; &nbsp; header('Location: ' . $url);&nbsp; &nbsp; &nbsp; die();&nbsp; &nbsp; }&nbsp; &nbsp;&nbsp;}如果您需要正确处理相对路径,那么我已经为此编写了一个函数(但这不在问题范围内)。

明月笑刀无情

&nbsp;$url='the/url/you/want/to/go';&nbsp; &nbsp;echo '<META HTTP-EQUIV=REFRESH CONTENT="1; '.$url.'">';这对我来说很好。
打开App,查看更多内容
随时随地看视频慕课网APP