3

I have this functions file. By doing... <?php include('functions.php'); ?> ... am I running all of the functions inside it, or am I simply allowing them to be called (making them exist in the current scope).

Sorry if it's a basic question, I'm a PHP nooby!

<?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;
          }
        }
      }
 ?>
1
  • 2
    You're just allowing them to be accessed. Once you've included functions.php then you can use any of the functions defined in the file. If any code isn't wrapped in a function (or class) then it WILL be run when the file is included.
    – Joe
    May 22, 2013 at 16:27

4 Answers 4

3

You are just defining the functions, not calling them. If there was a function call in that file it would execute. Typically you want to separate executing code and function/class definitions.

If you have a file that just has function or class definitions it's better to use include_once (or require_once). Because if you include the file twice you will get a function already exists error. Using include_once you avoid this.

A more advanced way of handling dependencies is autoloading using spl_autoload_register. Popular frameworks such as Symfony 2 have a distinct naming scheme that allows you to map classnames to filenames so any class can be automatically loaded just-in-time.

1
  • Thanks! I will use include_once. May 22, 2013 at 17:04
0

You are defining them. Any code that exists outside of a function will be ran.

0

You are actually running what is in the functions.php, thus declaring the functions and allowing them to be accessed, if you have any echo (for example) outside the functions, it will be executed.

0

You're only allowing them to be accessed. Although depending on include or require the script might affect the file in which it is included

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.