TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 04-01-2008, 03:38 PM   #1 (permalink)
The Prestige
Upcoming Programmer Inquisitive 
 
Tanax's Avatar
 
Join Date: Sep 2007
Location: Sweden, Stockholm
Posts: 1,080
Thanks: 115
Tanax is on a distinguished road
Default Including.. :S

Ye, something's.. wrong >.<

My config.php lies in:
website/includes/config.php

My admin files lies in:
website/admin/*.php

It works fine to include the config from there with this:
PHP Code:
include('../includes/config.php'); 
However, I have one admin file in a subfolder to the admin.

website/admin/edit/tab.php

And I can't seem to include the config file from there :S
I tried with:
PHP Code:
include('.../includes/config.php'); 
But it didn't work :S
__________________
Tanax is offline  
Reply With Quote
Old 04-01-2008, 04:05 PM   #2 (permalink)
The Contributor
 
abiko's Avatar
 
Join Date: Feb 2008
Location: Croatia
Posts: 90
Thanks: 4
abiko is on a distinguished road
Default

Try a path to the included file
- include '/your/path/to/folder/includes/config.php';
it'll be better if you have one file that includes all other files - your admin.php just includes other needed files - the tab.php file. And then in your admin file you make all the configuration needed to run your site.

Or if you have you config file in an array, or if it is an object you can use the global var scope.

Here are 2 examples:
- array styled config file
- object styled config file

PHP Code:
<?php
// The config file

// Array style
$config = array( 'database' => array( 'username' => 'mysql_user',
                                                                            
'pass'        => 'mysql_pass',
                                                                            
'database' => 'db'
                                                                            
),
                                 
'site_name' => 'The site',
                                 
'caching'   => '0'
                                 
);

// Object style
class siteConfig {
    protected 
$database = array( 'username' => 'mysql_user',
                                                                            
'pass'        => 'mysql_pass',
                                                                            
'database' => 'db'
                                                                            
);
    protected 
$site_name 'The site';
    protected 
$caching   '0';
}

?>
Now you structure your admin.php
PHP Code:
<?php
include '../includes/config.php';

/*
 * If you are using the array styled config file
 * your configuration is avaible trought the $config var
 * or any other you defined ($conf, $c ...)
 */
 
 // If using the object style config file
 
$config = new siteConfig();
 
 
 
// Using the object styled 
 // Example:
 
mysql_connect'localhost'$config->database['username'], $config->database['pass']);


// And here you include your files that depend on, let's say, 
// page var
$page $_GET['page'];

switch( 
$page ) {
    case 
'news':
     include 
'somefile.php';
    break;
    
    default:
        include 
'mainFile.php';
    break;
}
?>
Now when you include the file - no need for config.php to be included again and again - this way you built a platform for your included fileds.
And if you are using functions and you need to access the $config - you use the global variable scope

PHP Code:
<?php

function test() {
 global 
$config;
  
// The stuff the function does
}
?>
__________________
Back from sysadmins to the programmers.
Send a message via ICQ to abiko Send a message via MSN to abiko
abiko is offline  
Reply With Quote
The Following User Says Thank You to abiko For This Useful Post:
Tanax (04-01-2008)
Old 04-01-2008, 04:48 PM   #3 (permalink)
The Acquainted
 
wGEric's Avatar
 
Join Date: Nov 2007
Posts: 166
Thanks: 0
wGEric is on a distinguished road
Default

.. goes back one directory so you need to go back two, ../../

PHP Code:
include('../../includes/config.php'); 
. is the same directory. .. is the parent directory. You can put as many of those in there as you need. Separate folders with a / .
__________________
Eric
wGEric is offline  
Reply With Quote
The Following User Says Thank You to wGEric For This Useful Post:
Tanax (04-01-2008)
Old 04-01-2008, 06:07 PM   #4 (permalink)
The Prestige
Upcoming Programmer Inquisitive 
 
Tanax's Avatar
 
Join Date: Sep 2007
Location: Sweden, Stockholm
Posts: 1,080
Thanks: 115
Tanax is on a distinguished road
Default

Thanks both of you
I found that wGEric's method worked best for me
__________________
Tanax is offline  
Reply With Quote
Old 04-03-2008, 11:37 PM   #5 (permalink)
The Wanderer
 
Join Date: Apr 2008
Posts: 8
Thanks: 2
johnN is on a distinguished road
Default

To stop this issue cropping up at all, use $_SERVER['DOCUMENT_ROOT']. This will automatically go to your root dir:)

you'd just put include($_SERVER['DOCUMENT_ROOT']."/website/includes/config.php");

works in any directory
johnN is offline  
Reply With Quote
The Following User Says Thank You to johnN For This Useful Post:
Tanax (04-04-2008)
Old 04-04-2008, 08:16 AM   #6 (permalink)
The Prestige
Upcoming Programmer Inquisitive 
 
Tanax's Avatar
 
Join Date: Sep 2007
Location: Sweden, Stockholm
Posts: 1,080
Thanks: 115
Tanax is on a distinguished road
Default

Quote:
Originally Posted by johnN View Post
To stop this issue cropping up at all, use $_SERVER['DOCUMENT_ROOT']. This will automatically go to your root dir:)

you'd just put include($_SERVER['DOCUMENT_ROOT']."/website/includes/config.php");

works in any directory
Oo didn't know that.. I thought that I could use like

include('/website/includes/config.php');


Still, I didn't want to use this, since I might change the name of the folder of the website, so I needed a path FROM the current folder
__________________
Tanax is offline  
Reply With Quote
Old 04-04-2008, 11:50 AM   #7 (permalink)
Moderateur
RegEx Guru PHP Guru Top Contributor Advanced Programmer 
 
Salathe's Avatar
 
Join Date: Apr 2007
Posts: 1,393
Thanks: 5
Salathe is on a distinguished road
Default

To always reference the path to the file with the include call in it, you can use the dirname function with an argument using the constant __FILE__, the latter always giving the path to the file it is used in. So used from admin/edit/tab.php: include dirname(__FILE__).'/../../includes/config.php';

It might, or might not, prove useful to you.
Salathe is offline  
Reply With Quote
The Following User Says Thank You to Salathe For This Useful Post:
Tanax (04-04-2008)
Old 04-04-2008, 03:23 PM   #8 (permalink)
The Prestige
Upcoming Programmer Inquisitive 
 
Tanax's Avatar
 
Join Date: Sep 2007
Location: Sweden, Stockholm
Posts: 1,080
Thanks: 115
Tanax is on a distinguished road
Default

Thanks Can be useful, not in this case though, but it's always good to know
__________________
Tanax is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 12:13 PM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design