3

我有这个函数文件。通过这样做... <?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;
          }
        }
      }
 ?>
4

4 回答 4

3

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

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

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

于 2013-05-22T16:28:07.267 回答
0

你正在定义它们。将运行存在于函数之外的任何代码。

于 2013-05-22T16:27:58.417 回答
0

您实际上正在运行functions.php 中的内容,因此声明了这些函数并允许它们被访问,如果您在函数之外有任何回声(例如),它将被执行。

于 2013-05-22T16:27:59.027 回答
0

您只允许访问它们。尽管取决于 include 或 require 脚本可能会影响包含它的文件

于 2013-05-22T16:30:00.830 回答