luke
luke

Reputation: 1035

include unused full function or many includes

I am in the process of creating a PHP authorization framework which aims to be simple yet fast. My problem is being simple I hope to only have one include needed to use it , however when only having one include it may load unnecessary functions. So my question is which is better, including all function resulting in more memory being used or including a file which has the function to include that function resulting in smaller unnecessary functions but will it use more CPU usage?

for example my login function looks like this:

function login ($submit ='login', $user = 'user', $pass = 'pass') {
    if ($_POST[$submit]){

        $user = $_POST[$user];
    $pass = $_POST[$pass];

    if ($user && $pass){ //if user and pass is enterered

            require("auth_vars.php"); //require MySQL conection settings
            mysql_connect($auth_mysql_server, $auth_mysql_user, $auth_mysql_pass); //connect to MySQL
        mysql_select_db($auth_mysql_db); // select MySQL database

            $pass = md5($pass); // hash password

            $query = mysql_query("SELECT * FROM $auth_mysql_table WHERE user='$user'"); // run query
            $numrows = mysql_num_rows($query);

            if ($numrows == 1){ //check if user exists
                $row = mysql_fetch_assoc ($query);
                $dbid = $row[$auth_mysql_id_row];
                    $dbuser = $row[$auth_mysql_user_row];
                    $dbpass = $row[$auth_mysql_pass_row];

                    if ($pass == $dbpass){ // if password is equal to the one in the database start session
                        //set session information
                        $_SESSION['userid'] = $dbid;
                        $_SESSION['username'] = $dbuser;


                        header("Location:$auth_path_loggedin"); // goto logged in page

                    }
                    else return (3);
            }
            else return (2);

            mysql_close(); // close MySql connection
        }
        else return (1);
}else {
    // If the user clicks the "Log Out" link.
    if(isset($_GET['status']) && $_GET['status'] == 'loggedout') {
        logout();//logout
        return 4;
        //else if the user is denied
    }else if(isset($_GET['status']) && $_GET['status'] == 'denied') {
        logout(); //to keep it clean in case of errors
        return 5;
    }
}}

now in case the login function is not needed would it be better to do this:

function login ($submit ='login', $user = 'user', $pass = 'pass') {
    include ("myloginfunction.php");
    login_function($submit, $user, $pass);
}

or would it use to much unnecessary CPU usage compare to the amount of memory needed to include whole function

Upvotes: 0

Views: 91

Answers (1)

Mchl
Mchl

Reputation: 62395

Neither approach is much better than the other. You can possibly gain more, by moving creation of mysql connection out of login function, so that all your SQL queries use one an the same connection, insteead of creating multiple ones.

Upvotes: 1

Related Questions