Jump to content

[SOLVED] Access Variables in included files


Nixeon

Recommended Posts

I'm not sure if im doing this right, but thought someone might be able to point me in the right direction. I'm making a website run by php and have put all the variable I need set in a file to be included into my code, but it's only "half" working. Sorry if my explaining confuses you:

 

I have a file with my variables called install_settings.config:

 

<?php
$content_directory = '/components/';
$theme = 'mypanel';

$install_domain = 'http://www.gredo.co.uk/';
$install_directory = 'mypanelbeta/';
$theme_directory = '/themes/';
$admin_directory = '/admin/';

global $root_directory;
$root_directory = $install_domain . $install_directory . $content_directory; 
$theme_root = $install_domain . $install_directory . $theme_directory;

?>

 

This is then included into my index.php and all the variables are picked up and working fine:

 

<?php
include('install_settings.config');
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>myPanel Beta</title>

<link rel="stylesheet" media="screen" type="text/css" href="<?php echo $theme_root . $theme;?>/css/reset.css" />
<link rel="stylesheet" media="screen" type="text/css" href="<?php echo $theme_root . $theme;?>/css/main.css" />
<!--[if lte IE 6]>
<link rel="stylesheet" media="screen" type="text/css" href="css/ie6.css" />
<![endif]-->

</head>
<body>
<?php
//Set the current page variable
if(isset($_GET['page'])) {
		   $current_directory = $_GET['page'];
		   } else {
			   $current_directory = 'dashboard';
		   };
if(isset($_GET['slevel'])) {
		   $current_page = $_GET['slevel'];
		   } else {
			   $current_page = 'front';
		   };
$current_path = $current_directory . '/' . $current_page . '.php';

//Include the header file
include ($root_directory . 'header.php');

//Load in the current page
include($root_directory . $current_path);

//Include sidebar file
include($root_directory . 'sidebar.php');

//Include footer file
include($root_directory . 'footer.php');
?>
</body>
</head>

 

Then I run into problems when trying to include the subnavigation bar on the subpage. For example on the feedback page /feedback/front.php, when I try to include the navigation file using the $root_directory, it comes up with an error saying it cant find the file, and when I echo the variable in the included file it isn't set?

 

<?php
include($root_directory . 'navigation/nav_feedback.php');

?>

<div id="content">
<h3>Leave Some Feedback</h3>
   	<p>To contact the site admin please use the form below. To help response times please select the correct feed back type from the drop down menu.</p>
  <form name="form_feedback" title="form_feedback" method="post" action="">
    <h4>Feedback Type</h4>
    <select name="dropdown_feedbacktype" class="select">
      <option value="None">Please Select</option>
      <option value="Technical Help">Technical Help</option>
      <option value="New Idea">New Idea</option>
      <option value="Complaint">Complaint</option>
      <option value="Request Functionality">Request Functionality</option>
    </select>
    <h4>Username</h4>
    <input type="text" class="textfield small" value="USERNAME" disabled />
    <h4>Message</h4>
    <textarea class="textarea large" rows="5"></textarea>
    <div style="height:15px;"></div>
    <input type="submit" class="button" value="Send Feedback" />
    <input type="reset" class="button" value="Clear Form" />
    </form>
    
    </div>

 

What I don't get is that the variables are picked up in index.php however won't work in anything included into index.php? I hope this makes sense... have i missed something like a variable setting or is this just impossible!!!?

 

Thanks In Advanced

Link to comment
Share on other sites

WELL

 

1 thing I can say is..

 

name your file

 

install_settings.config.php

 

including a txt file does the same as including a .php, however... If a user does

 

yoursite.com/install_settings.config

 

they'd get alot of information about your site.. and thats never good.. keep it secret.. keep it .php

 

ALSO

 

// config.php
$var1 = "hello";

// workit.php
echo $var1;

// index.php
include('config.php');
include('workit.php');

 

that should display "hello" if it doesn't maybe reinstall php :S idk wat else to say

Link to comment
Share on other sites

WELL

 

1 thing I can say is..

 

name your file

 

install_settings.config.php

 

including a txt file does the same as including a .php, however... If a user does

 

yoursite.com/install_settings.config

 

they'd get alot of information about your site.. and thats never good.. keep it secret.. keep it .php

 

lol cannot believe I missed that.

 

On a sidetracked note, I usually set a variable in my php files called "$included"  is that variable is not set then nothing is included and the script just dies. It helps prevent the intrusion that Russell was talking about, however that can be overkill, but I like to play things pretty safe when it comes to my site =)

Link to comment
Share on other sites

// config.php
$var1 = "hello";

// workit.php
echo $var1;

// index.php
include('config.php');
include('workit.php');

 

that should display "hello" if it doesn't maybe reinstall php :S idk wat else to say

 

http://lovinglori.com/file3.php

 

I did the exact same thing

 

file1 sets $var1

 

file 2 echoes $var1

 

file3 includes both

 

and output is exactly how it should be..

Link to comment
Share on other sites

So you're including a file inside an included file?

 

Included files are supposed to take on the scope of the file that included them.  I don't know how this works with nested includes though.  In the first included file I'd make all pertinent variables (like $root_directory) global.  This should allow the subinclude to access them.

Link to comment
Share on other sites

I've tried all of the above but still can't get it to work. Perhaps it's something else I'm doing that's wrong. Thanks for the heads up about adding .php onto the end of the file.

 

I still can't work out what I'm doing wrong because all the variables work in index.php.

 

I've got it working like this:

 

>> index.php loads in install_settings.config.php (contains the variables)

>> index.php uses these variable to include the current page in this case front.php

>> front.php then tries to use the variables to include the sub_nav.php however it can't access the variables.

 

 

Is it because they have already been used in index.php? Do you think that setting the $root_directory variable in index.php instead of install_settings.config.php may help (goes to try this).

 

 

Link to comment
Share on other sites

Trying the above didn't work. however when i change the line :

 

$install_domain = 'http://www.gredo.co.uk/';

to

$install_domain = '../';

 

in install_settings.config.php everything works fine... now im really confused.

 

Thanks for all the help

 

Easy explanation. You were using a web address to include php, the address should only be used on html side to display images style sheets etc. For the internal stuff, including php code, you have to use the actual file server path, not the virtual domain path.

Link to comment
Share on other sites

Trying the above didn't work. however when i change the line :

 

$install_domain = 'http://www.gredo.co.uk/';

to

$install_domain = '../';

 

in install_settings.config.php everything works fine... now im really confused.

 

Thanks for all the help

 

Easy explanation. You were using a web address to include php, the address should only be used on html side to display images style sheets etc. For the internal stuff, including php code, you have to use the actual file server path, not the virtual domain path.

 

 

ooh! Okay that's for the help guys will bear this in mind in future!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.