繁体   English   中英

用户定义函数php

[英]user defined function php

我有两个文件'inc.core.php'和'index.php'.i在'inc.core.php'中定义了一个函数为:

function isLoggedin() {
    if(isset($_SESSION['user_name']) && !empty($_SESSION['user_name'])){
        return true;
    }else{
        return false;
   }
}

我想在索引中使用该函数并尝试如下:

require "inc.core.php";

if(isLoggedin()){

  header('Location:patient.html');

}

但它总是给我错误

解析错误:语法错误,第6行的C:\\ xampp \\ htdocs \\ medfinal \\ inc.core.php中出现意外的“函数”(T_FUNCTION)

  1. 您需要将自动全局函数的值分配给变量,以便能够通过函数进行访问。
  2. 您必须在函数中使用该变量作为参数

     function isLoggedin($un) { if(isset($un) && !empty($un)){ return true; }else{ return false; } } 

然后,在调用函数时使用参数

    require("inc.core.php");

    $un = $_SESSION['user_name']; //assign session value to $un

    if(isLoggedin($un)){

    header('Location:patient.html');

    }

我按照lrd的正确编辑指出,最好在isLoggedin函数调用之后分配会话值,因为此时用户应该已经运行了验证,或者如果没有,则应该重定向到验证。 另外,您需要在验证代码之前的session_start()中检查用户凭据

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM