gpt4 book ai didi

php - 通过在页面上包含此 PHP 文件,我是在运行其中的所有功能,还是只是允许访问它们?

转载 作者:搜寻专家 更新时间:2023-10-31 20:43:29 25 4
gpt4 key购买 nike

我有这个函数文件。通过做... <?php include('functions.php'); ?> ...我是在运行其中的所有函数,还是只是允许调用它们(使它们存在于当前范围内)。

抱歉,如果这是一个基本问题,我是一个 PHP 菜鸟!

<?php

/*
* -------------------------------------------------------------------------------------
* ---------------------------- GLOBALLY AVAILABLE FUNCTIONS ---------------------------
* -------------------------------------------------------------------------------------
*/

function user_loggedin_check() {
if(isset($_COOKIE['logged_in']) && ($_COOKIE['logged_in'] == true)){
$logged_in = true;
setcookie("logged_in", $logged_in, time()+14400);
return true;
} else {
header('Location: index.php?loginerr=6');
exit();
}
}

function permission_level_2_check() {
if(isset($_COOKIE['permission_level']) && ($_COOKIE['permission_level'] == 2)){
$logged_in = true;
setcookie("logged_in", $logged_in, time()+14400);
return true;
} else {
setcookie("logged_in", false, time()-1);
setcookie("permission_level", 1, time()-1);
session_unset();
session_destroy();
header('Location: index.php?loginerr=7');
exit();
}
}

function permission_level_3_check() {
if(isset($_COOKIE['permission_level']) && ($_COOKIE['permission_level'] == 3)){
$logged_in = true;
setcookie("logged_in", $logged_in, time()+14400);
return true;
} else {
setcookie("logged_in", false, time()-1);
setcookie("permission_level", 1, time()-1);
session_unset();
session_destroy();
header('Location: index.php?loginerr=7');
exit();
}
}

function display_login_errors() {
/*
* -------------------------------------------------------------------------------------
* ---------------------------------- ERROR LIST ---------------------------------------
* -------------------------------------------------------------------------------------
*/
/*
* loginerr=0 -> passwords don't match
* loginerr=1 -> username already exists in DB
* loginerr=2 -> registration is currently disabled
* loginerr=3 -> password is too long and/or too short
* loginerr=4 -> email isn't in proper format
* loginerr=5 -> email is too long and/or too short
* loginerr=6 -> user isn't logged in, generic redirect to login page error
* loginerr=7 -> user doesn't have the required access to view that page
*/

if(isset($_GET["loginerr"])){
$loginerr = $_GET["loginerr"];
switch ($loginerr) {
case 0:
echo "<div class='alert alert-error pagination-centered frontpage-alert'>";
echo "<strong>Your passwords didn't match. Try again.</strong>";
echo "</div>";
break;
case 1:
echo "<div class='alert alert-error pagination-centered frontpage-alert'>";
echo "<strong>That email address is already registered.</strong>";
echo "</div>";
break;
case 2:
echo "<div class='alert alert-error pagination-centered frontpage-alert'>";
echo "<strong>Registration is currently disabled. Try again later.</strong>";
echo "</div>";
break;
case 3:
echo "<div class='alert alert-error pagination-centered frontpage-alert'>";
echo "<strong>Your password must be between 4 and 32 characters in length.</strong>";
echo "</div>";
break;
case 4:
echo "<div class='alert alert-error pagination-centered frontpage-alert'>";
echo "<strong>Your email address didn't match proper formatting (email@domain.com).</strong>";
echo "</div>";
break;
case 5:
echo "<div class='alert alert-error pagination-centered frontpage-alert'>";
echo "<strong>Your email address must be between 6 and 32 characters in length.</strong>";
echo "</div>";
break;
case 6:
echo "<div class='alert alert-error pagination-centered'>";
echo "<strong>You need to be logged in to access that page. Please log in.</strong>";
echo "</div>";
break;
case 7:
echo "<div class='alert alert-error pagination-centered'>";
echo "<strong>You don't have access to view that page. You have been logged out due to suspicious activity.</strong>";
echo "</div>";
break;
}
}
}
?>

最佳答案

您只是在定义函数,而不是调用它们。如果该文件中有一个函数调用,它就会执行。通常,您希望将执行代码和函数/类定义分开。

如果您的文件只有函数或类定义,最好使用 include_once(或 require_once)。因为如果您包含该文件两次,您将得到一个函数已存在 错误。使用 include_once 可以避免这种情况。

处理依赖关系的更高级方法是使用 spl_autoload_register 自动加载. Symfony 2 等流行的框架有一个独特的命名方案,允许您将类名映射到文件名,这样任何类都可以及时自动加载。

关于php - 通过在页面上包含此 PHP 文件,我是在运行其中的所有功能,还是只是允许访问它们?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16696981/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com