dqwh1208 2014-06-01 10:21
浏览 119
已采纳

如何从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??

  • 写回答

1条回答 默认 最新

  • dshdb64088 2014-06-01 10:30
    关注

    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
    
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥30 python代码,帮调试,帮帮忙吧