0

I'm a beginner in PHP. And i'm working on project with this directories hierarchy : model, control, view and helper folders are in my project folder

Now i'm trying to write a file init.php and require_once it in each of control and model files, here's my init.php

<?php

    $current_dir = basename(getcwd());
    $model_dir = "model";
    $helper_dir = "helper";

    function require_helper(){
        $handle = opendir("../{$helper_dir}");
        while($file = readdir($handle)){
        if($file != "." && $file != ".."){
                require_once "../{$helper_dir}/{$file}";
            }
        }
    }

    if($current_dir == "control"){
        $handle = opendir("../{$model_dir}");
        while($file = readdir($handle)){
            if($file != "." && $file != ".."){
                require_once "../{$model_dir}/{$file}";
            }
        }

        require_helper();

    } elseif( $current_dir == "model") {
        $handle = opendir($current_dir);
        while($file = readdir($handle)){
            if($file != "." && $file != ".."){
                require_once "{$file}";
            }
        }

        require_helper();
    } 
?>

But when i test my project i get this error :

Notice: Undefined variable: session in C:\wamp\www\harmony\control\login.php on line 11

Here's my login.php file :

<?php
    require_once "../helper/init.php";
?>

<?php

    if(isset($_GET["logout"]) && $_GET["logout"] == "true" ){
        $session->logout();
    }

    if($session->is_logged_in()){
        redirect_to("../view/index.php"); 
    }

    if(isset($_POST["submit"])){
        $username = $db->escape_value($_POST["username"]);
        $password = $db->escape_value($_POST["password"]);
        $password = hash('sha1' , $password);
        $arr = User::auth($username , $password);
        if($arr){
            $usr = $db->instantiate($arr);            
            $session->login($usr);
        } else {
            Session::notify("Invalid login information.");
        }
    }    

?>

So could you help me please ? what's wrong is going on ?

Rafael Adel
  • 7,673
  • 25
  • 77
  • 118
  • try adding if(isset($session) && $session->is_logged_in()){ to that condition. That should get rid of the error. – sissonb Jul 30 '11 at 17:37
  • Reuiring `init.php` in all files? You're doing it wrong! Look up `front controller` design. Basically you should have this one file that handles all requests and loads necessary files. This reduces amount of repeated boilerplate code dramatically. – Mchl Jul 30 '11 at 17:46
  • @Mchl Can you provide me with a good php tutorial for front controller design ? thanks :) – Rafael Adel Jul 30 '11 at 23:40
  • Not really. I've yet to write one ;P THe idea however is to have your `index.php` decide what files/classes to require and execute basing on URL parameters (which go into `$_GET` array. All PHP MVC frameworks I looked into use a variation of this scheme. – Mchl Jul 31 '11 at 08:27
  • Oh, so i can modify `init.php` so i can make use of $_GET variable ? or just have to start over. – Rafael Adel Jul 31 '11 at 12:45

1 Answers1

1

You're trying to access $current_dir, $model_dir and $helper_dir inside of functions. You can't access variables which were declared outside of a function unless they are declared global, or else actually passed into the function.

so for example:

function require_helper(){
    global $helper_dir;//this is key
    $handle = opendir("../{$helper_dir}");
    while($file = readdir($handle)){
    if($file != "." && $file != ".."){
            require_once "../{$helper_dir}/{$file}";
        }
    }
}
Abel Mohler
  • 785
  • 5
  • 12