在函数内调用时,标题不起作用 [英] header is not working when called within a function

查看:128
本文介绍了在函数内调用时,标题不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个名为 public.php 的php文件,并增加了一些函数来减少代码冗余。其中一个功能如下。

  / ** 
*检查是否存在有效的会话并导航到登录页面如果为false。
* @return布尔如果会话有效,则为true。
* /
函数checkSession_auto(){
if(checkSession()){
return true;
} else {
// echo'pass';出口;
header(location:accounts / login.php);




$ b我已经证实 checkSession()使用 echo 语句返回 false 。问题出在这里。


页面不会导航到 login.php 。为什么?


感谢您的建议!

解决方案


通过在 header($ string)后添加 exit $ c>,
阅读此评论


看来退出是必需的。



标题只是一行要求浏览器重定向的数据。页面的其余部分仍然由PHP提供,并且可以通过简单地阻止执行命令来查看客户端。



资源;


I have created a php file named public.php and have added few functions to it to reduce code redundancy. One of the functions is as follows.

/**
 * Checks if there is a valid session and navigates to the login page if false.
 * @return boolean true if session valid.
 */
function checkSession_auto() {
    if (checkSession()) {
        return true;
    } else {
        //echo 'pass'; exit;
        header("location: accounts/login.php");
    }
}

I have verified that the checkSession() returns false, using an echo statement. Here's the problem.

The page does not navigates to login.php. Why?

Thanks in advise!

解决方案

Problem solved by adding exit statement after header($string), read this comment.

It seems that the exiting is mandatory after sending a location header unless you do not have any code (PHP or HTML) under header.

The header is only a line of data asking the browser to redirect. The rest of the page will still be served by PHP and can be looked at by the client by simply preventing the header command from executing.

Resources;

这篇关于在函数内调用时,标题不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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