views:

71

answers:

4

hey everyone i'm trying to apply some module system on my web, using get and include, here's some of my code on my index.php

    $section = 'user';
    if(isset($_GET) && !empty($_GET) && $_GET !== ''){
        $module = $_GET['module'].".php";
        load_module($section, $module);
    }

load_module function

function load_module($section="", $module=""){
    include(SITE_ROOT.DS.$section.DS.'modules'.DS.$module);
}

*i have already define DS as DIRECTORY_SEPARATOR

and i stored few files inside modules folder, the file loads perfectly, my problem is that all the variable i declared on my included page fails to load, here's my code on one of the included file

if($session->is_logged_in()){
    $user = User::find_by_id($session->user_id);
    $profile = $user->profile();
    $company = $user->compro();
    $logo = $user->logo();
}else{redirect_to('index.php');}

on my index.php i got this error

Notice: Undefined variable: session in C:\www\starpro\user\modules\edit_company.php on line 3 Fatal error: Call to a member function is_logged_in() on a non-object in C:\www\starpro\user\modules\edit_company.php on line 3 

and if i move those variables inside my index.php, i get this message

Notice: Undefined variable: company in C:\www\starpro\user\modules\edit_company.php on line 181  Notice: Trying to get property of non-object in C:\www\starpro\user\modules\edit_company.php on line 181

please some one help me, thank you in advance

Regards

======================================================================

i am using deceze's answer

and modify my user's class by adding a static function like this

public static function load_module($section="", $module="", $user_id=""){
$user = self::find_by_id($user_id);
    $profile = $user->profile();
    $company = $user->compro();
    $logo = $user->logo();
include(SITE_ROOT.DS.$section.DS.'modules'.DS.$module);
}

and then on my index i use this

if(isset($_GET) && !empty($_GET) && $_GET !== ''){
    $module = $_GET['module'].".php";
    User::load_module($section, $module, $user->id);
}else{

i got it working, but is this a bad practice ?? need advise

thanks much

A: 

Including a file is like putting the contents of the file exactly where the include command is. So, this:

function load_module($section="", $module=""){
    include(SITE_ROOT.DS.$section.DS.'modules'.DS.$module);
}

is equivalent to this:

function load_module($section="", $module=""){
    if($session->is_logged_in()){
        $user = User::find_by_id($session->user_id);
        $profile = $user->profile();
        $company = $user->compro();
        $logo = $user->logo();
    }else{redirect_to('index.php');}
}

All your variables are confined to the scope of the function. As soon as the function returns, the variables go out of scope. Also, variables that are not in scope inside the function are not available to the included code.

You'll need to do the include directly without the function.

deceze
i am gonna go ahead and try thisthank you very much
littlechad
i got it working by making a slight change on the function, instead of placing the load_module function, i move it inside the user classposted the change on my question, advise pleasethanks
littlechad
+2  A: 

As has been stated, you are trying to include the code into the middle of the function, making the scope of the included page limited to that function.

One solution would be to have a global array of files to include, then include them at the end of the script. Just add each file to the array, and at the end, loop through it and include them all.

$includeFiles = array();

...

function load_module($section="", $module=""){
    // include(SITE_ROOT.DS.$section.DS.'modules'.DS.$module);
    global $includeFiles;
    $location = SITE_ROOT.DS.$section.DS.'modules'.DS.$module;
    array_push($includeFiles, $location);
}

...

foreach( $inludeFiles as $location )
{
    include_once($location);
    // using include_once so that if the file is added multiple times in the 
    // document, it only gets included once
}

It is also a massive security risk to include a file based on a parameter in the GET request. You should sanitize that input by either stripping or encoding all symbols which could be used to traverse to another directory and include code you don't want included (so remove any slashes, etc.), or make a whitelist of includable files. If you had an array of sections and modules and their locations you could take an approach which would solve both problems:

$modules = array(
    'section1'  => array(
                    'module1' => '/php/modules/module1.php',
                    'module2' => '/php/frameworks/foo/bar.php'
                    ),
    'section2'  => array(
                    'module1' => '/php/modules/baz.php',
                    'module2' => '/php/modules/quot.php'
                    )
    )
}

$modulesIncluded = array();

...

function load_module($section="", $module="")
    global $modulesIncluded;
    array_push($modulesIncluded, $section => $module);
}

...

foreach( $modulesIncludes as $section => $module )
{
    include_once($modules[$section][$module]);
}

Note: I have not tested any of this code, this is purely theoretical. I would not advise copying this, but using it as a jumping-off place.

nhinkle
i will look into this as a good reference, thanks for the valuable information
littlechad
A: 

The include's scope is the same as if the code were in that function.

If you want a variable in this case to be global, assign it to $GLOBALS['varName']

Andrew67
A: 

Aside from using globals, you can also use static class methods/properties, e.g.:

/* session.php */
class session {
    public static $user_id;
    public static $logged_in;
    public static function user_id() {
        return self::$user_id;
    }
    public static is_logged_in() {
        return self::$logged_in;
    }
}

/* foo.php */
class foo {
    public static $user;
    public static $profile;
    public static $company;
    public static $logo;
    public static function init() {
        self::$user = User::find_by_id(Session::user_id());
        self::$profile = self::$user->profile();         
        self::$company = self::$user->compro();
        self::$logo = self::$user->logo();
    }
}

if (Session::is_logged_in()) {
    foo:init();
}
Lèse majesté