user1951228
user1951228

Reputation:

How to call functions from another file in php?

I have php file "user.class.php" with lot of functions. There is part of this:

/**
*Biztositja, hogy a jelszo visszafejthetetlen legyen
 */
    function encode_password($password){
        return md5("dF6u2#j@jd".$password."5YVM7&fdsga");
    }

/**
*Ellenorzi, hogy letezo status kod lett-e megadva
 */
    function validate_status($status){      
        if($status == 1 || $status == 4) return $status;
        else die('Status Error');
    }


/**
*Bejelentkezes
 */
    function login($email,$password){

        $encoded_password = $this->encode_password($password);
        $res = $this->db->select("SELECT * FROM `users` WHERE email = '".$email."' AND password = '".$encoded_password."' AND status < 8 LIMIT 1");
        $r = mysql_fetch_array($res);
        if($r['user_id'] > 0)
        {
            $this->clear_session_variables("user_status");
            $_SESSION['username'] = $r['username'];

            return $r['user_id'];
            redirect("../../index.php&msg=del_success");
        }else{
            return false;
        }

    }

/**
*Kijelentkezes
 */
    function logout(){

        $this->clear_session_variables();

    }


/**
*torli a megadott session valtozokat vagy az egesz sessiont
 */
    function clear_session_variables($variables=''){

        if($variables==''){

            // Unset all of the session variables.
            $_SESSION = array();
            // Finally, destroy the session.
            session_destroy();

        }else{

            $var = split(",",$variables);
            foreach($var as $v){
                $_SESSION[$v] = '';
            }

        }

    }


/**
*Ellenorzi, hogy be van-e jelentkezve
 */
    function is_logged_in(){

        if (!isset($_SESSION['uid']) || $_SESSION['uid']<=0){ return false;}
        else { return true; }

    }

/**
*Visszaadja a felhasznalo statuszat
 */
    function get_status($user_id){

        return $this->db->select_one("SELECT status FROM `users` WHERE user_id = '".$user_id."' LIMIT 1");

    }

I have included user.class.php in my index.php and some code for see if user is logged in:

<?php

    print_r($_SESSION);

    if(isset($_SESSION['uid']) ){

    if ($_SESSION['uid']=='1'){
    include("./modules/frontend/user_menu.php");
}   elseif ($_SESSION['uid']=='5'){
    include("./modules/frontend/admin_menu.php");
}   else {
    include("./modules/frontend/login_form.php");
    }} ?>

So, How can I call this functions on my index.php? I try all variations of function calling but i get always error mesage. The functions is working fine, the problem is i need to write forms for login, button for logout and other things, but i cant call functions for something.

Call to undefined function logout() ??? why??

Upvotes: 0

Views: 3861

Answers (1)

MD SHAHIDUL ISLAM
MD SHAHIDUL ISLAM

Reputation: 14523

user.class.php

<php

   class user {

       function getUser() {

           return "User Name";

       }

   }
?>

index.php

<?php

include("user.class.php");

$userObj = new user(); //creating object of your class which is containing methods

echo $userObj->getUser(); //calling function

?>

Upvotes: 2

Related Questions