簡體   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