{RESOLVED} include inside include variable disappearing

  • righteous_trespasser
  • Scuffle
  • Genius
  • User avatar
  • Posts: 6230
  • Loc: South-Africa

Post 3+ Months Ago

So here's what's going on, I have a file (index.php) which is just a page that I use for including the template for my custom CMS. This index.php file builds all the variables for the template file ($title,$content,$me(logged in user),etc). At the end of this file I include my template (/templates/theme/index.php) and this works great and pulls in all the variables set in index.php. Inside the template file I include the login module (/modules/user/theme/login.php).

Some snippets of code:

index.php
PHP Code: [ Select ]
...
  //if the user is logged in, create the user object
  if(isset($_SESSION["login"]) && $_SESSION["login"] != ""){
    $me = new User($_SESSION["login"]);
  }
  else{
    $me = false;
  }
...
  $template_file = "templates/" . WEBSITE_THEME . "/" . (($mobile_browser == true) ? "mobile.php" : "index.php");
  if(file_exists($template_file)){
    require_once($template_file);
  }
  1. ...
  2.   //if the user is logged in, create the user object
  3.   if(isset($_SESSION["login"]) && $_SESSION["login"] != ""){
  4.     $me = new User($_SESSION["login"]);
  5.   }
  6.   else{
  7.     $me = false;
  8.   }
  9. ...
  10.   $template_file = "templates/" . WEBSITE_THEME . "/" . (($mobile_browser == true) ? "mobile.php" : "index.php");
  11.   if(file_exists($template_file)){
  12.     require_once($template_file);
  13.   }


/templates/theme/index.php
PHP Code: [ Select ]
<div class='login'>
 <?php Module_Helper::load_module("user","login"); ?>
</div>
  1. <div class='login'>
  2.  <?php Module_Helper::load_module("user","login"); ?>
  3. </div>


/modules/user/theme/login.php
PHP Code: [ Select ]
<?php if($me == false): ?>
 ...
<?php else: ?>
 ...
<?php endif; ?>
  1. <?php if($me == false): ?>
  2.  ...
  3. <?php else: ?>
  4.  ...
  5. <?php endif; ?>


now the problem is, $me is ALWAYS false on the /modules/user/theme/login.php page, even though it is an object on the /templates/theme/index.php page, which just includes the login.php page. Can anyone tell me what's happening here ... ?
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • joebert
  • Fart Bubbles
  • Genius
  • User avatar
  • Posts: 13502
  • Loc: Florida

Post 3+ Months Ago

I'm taking a shot in the dark here, but my guess is that you're using eval or something within your Module_Helper::load_module method and your $me variable isn't available in that context. Perhaps $me isn't available in load_module's context.

What happens if you use "global $me" inside load_module ?
  • righteous_trespasser
  • Scuffle
  • Genius
  • User avatar
  • Posts: 6230
  • Loc: South-Africa

Post 3+ Months Ago

Here's the function, nothing happens to the $me variable inside this.
PHP Code: [ Select ]
public static function load_module($module,$action,$module_url = null){
  if(file_exists("modules/{$module}/" . WEBSITE_THEME . "/{$action}.php")){
    include("modules/{$module}/" . WEBSITE_THEME . "/{$action}.php");
  }
  else{
    include("modules/{$module}/default/{$action}.php");
  }
}
  1. public static function load_module($module,$action,$module_url = null){
  2.   if(file_exists("modules/{$module}/" . WEBSITE_THEME . "/{$action}.php")){
  3.     include("modules/{$module}/" . WEBSITE_THEME . "/{$action}.php");
  4.   }
  5.   else{
  6.     include("modules/{$module}/default/{$action}.php");
  7.   }
  8. }
  • joebert
  • Fart Bubbles
  • Genius
  • User avatar
  • Posts: 13502
  • Loc: Florida

Post 3+ Months Ago

What happens if you do this ?

PHP Code: [ Select ]
public static function load_module($module,$action,$module_url = null){
  global $me;
  if(file_exists("modules/{$module}/" . WEBSITE_THEME . "/{$action}.php")){
    include("modules/{$module}/" . WEBSITE_THEME . "/{$action}.php");
  }
  else{
    include("modules/{$module}/default/{$action}.php");
  }
}
  1. public static function load_module($module,$action,$module_url = null){
  2.   global $me;
  3.   if(file_exists("modules/{$module}/" . WEBSITE_THEME . "/{$action}.php")){
  4.     include("modules/{$module}/" . WEBSITE_THEME . "/{$action}.php");
  5.   }
  6.   else{
  7.     include("modules/{$module}/default/{$action}.php");
  8.   }
  9. }
  • righteous_trespasser
  • Scuffle
  • Genius
  • User avatar
  • Posts: 6230
  • Loc: South-Africa

Post 3+ Months Ago

works like a charm ... Why does this happen though? if I include a file shouldn't it also read the variables from the file that is including it ... ?
  • joebert
  • Fart Bubbles
  • Genius
  • User avatar
  • Posts: 13502
  • Loc: Florida

Post 3+ Months Ago

You're still within the context of that function when you do the include though. include doesn't inherently use the global context, it uses the current execution context. :)
  • righteous_trespasser
  • Scuffle
  • Genius
  • User avatar
  • Posts: 6230
  • Loc: South-Africa

Post 3+ Months Ago

oh okay, noted, cool stuff, thanks.

Post Information

  • Total Posts in this topic: 7 posts
  • Users browsing this forum: No registered users and 58 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
cron
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.