[英]How to call functions from another file in php?
我有許多功能的php文件“ user.class.php”。 其中有一部分:
/**
*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");
}
我在index.php中包含了user.class.php和一些代碼,以查看用戶是否已登錄:
<?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");
}} ?>
那么,如何在我的index.php上調用此函數? 我嘗試了函數調用的所有變體,但總是收到錯誤消息。 函數運行良好,問題是我需要編寫登錄表單,注銷按鈕等內容,但是我無法調用函數。
調用未定義的函數logout()??? 為什么??
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
?>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.