PHP函数1调用函数2

时间:2017-09-03 22:37:09

标签: php function

我是一个php新手(温柔)我有两个功能,我希望能够在getUserLevel()内调用adminOnlyAccess()这是否可以做到?两个函数都在functions.php

这是getUserLevel()

function getUserLevel(){    
    $sql = 'SELECT * FROM author WHERE id = :sessionID';
    $stmt = dbConnection()->prepare($sql);
    $stmt->bindValue(':sessionID', $_SESSION['id']);
    $stmt->execute();
    $data = $stmt->fetch();

    return $data['role'];
}

这是adminOnlyAccess()

function adminOnlyAccess($access = TRUE){   
    $sql = 'SELECT * FROM author WHERE id = :sessionID';
    $stmt = dbConnection()->prepare($sql);
    $stmt->bindValue(':sessionID', $_SESSION['id']);
    $stmt->execute();
    $data = $stmt->fetch();

    if ($access == TRUE && $data['role'] != 1)
    {
        include '../accessdenied.php';
        exit();
    }
}

2 个答案:

答案 0 :(得分:1)

我不明白为什么你不能。您可以将adminOnlyAccess转换为如下所示:

function adminOnlyAccess($access = TRUE){   
    if ($access == TRUE && getUserLevel() != 1)
    {
        include '../accessdenied.php';
        exit();
    }
}

答案 1 :(得分:1)

是的......完全可能

false
false
check_for_cycle_short : 36ms
check_for_cycle_long  : 7239ms