PHP页面重定向 [英] PHP page redirect

查看:144
本文介绍了PHP页面重定向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

  if {
//我在这里使用echo
}

else if($ _SESSION ['qnum']> 10){
session_destroy();
回声发生了一些错误。;
//重定向到user.php
}


解决方案 div>

是的,您可以使用标题功能。

  header(Location:http://www.yourwebsite.com/user.php); / *重定向浏览器* / 
exit();

调用 exit()

另外,从文档中可以看出:

请记住,必须在发送任何实际输出之前调用header(),或者使用普通的HTML标记,文件中的空行或PHP。使用include()或require()函数或其他文件访问函数读取代码,并在调用header()之前输出空格或空行是非常常见的错误。使用单个PHP / HTML文件时存在同样的问题。


这意味着您不应该在 header()函数,因为这样做可能会引发错误。此外,您还需要验证此代码是否在任何其他输出之前运行。


Can PHP make a redirect call after executing a function? I am creating a function on the completion of which I want it to redirect to a file located in the same root folder. Can it be done?

 if {
      //i am using echo here
 }

 else if ($_SESSION['qnum'] > 10) { 
            session_destroy();
            echo "Some error occured.";
            //redirect to user.php
        }

解决方案

Yes, you would use the header function.

header("Location: http://www.yourwebsite.com/user.php"); /* Redirect browser */
exit();

It is a good practice to call exit() right afterwords so that code below it does not get executed.

Also, from the documentation:

Remember that header() must be called before any actual output is sent, either by normal HTML tags, blank lines in a file, or from PHP. It is a very common error to read code with include(), or require(), functions, or another file access function, and have spaces or empty lines that are output before header() is called. The same problem exists when using a single PHP/HTML file.

This means you should not echo anything right before the header() function, as doing so will more than likely throw an error. Also, you will need to verify that this code gets run before any other output as well.

这篇关于PHP页面重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆