Forums

PHP

This topic is locked

How to include a file

Posted 25 Aug 2011 09:38:04
1
has voted
25 Aug 2011 09:38:04 shadoiqw kolo posted:
What is the best way to include my database.php file to any other .php files that require a database connection. I know that some ways are more secure then others. I have my database.php in a private folder and just need to know what code would be the best to include it in other .php files. Thanks in advance.

Replies

Replied 13 Sep 2011 04:04:56
13 Sep 2011 04:04:56 Michel Tiffin replied:
Hi,
Thanks for sharing this information. I think you’re very interested about What is the best way to include my database.php file.

Best ways to create include path for PHP

Use php.ini to create include path:

If PHP is compiled as a CGI binary, you can use php.ini to create include path. If you have no direct access to php.ini on the web server (e.g. you are using shared web hosting), you can create a custom php.ini on your hosting account's web root directory. Note that, you need to place your custom php.ini in every folder where include path is used.
Don't want to place php.ini in every subfolder?

GeeksEngine is hosted by LunarPages where PHP is compiled as CGI. In addition, LunarPages also uses suPHP to parse php scripts. An account can have multiple php.ini files in different folders so you can customize the php processing in different folders should your script require it. A php.ini file will not inherit down into subfolders. However, you can create a .htaccess file in the same folder as the php.ini file and place the following code into it:
suPHP_ConfigPath /home/username/public_html/

where "username" is your cPanel username. This will cause the php.ini file to affect all subfolders, unless a php.ini file is in a subfolder, at which point the php.ini in the subfolder takes precedence.

To create include path in your php.ini file, download your current php.ini from your website by using a FTP client, or create a new one if you don't have it yet. Open php.ini file in your favorite text editor and add include path in either of the following two ways:

1.Include folder is at your hosting account's home directory.

It's a good idea to create include folder at the home directory level (which is one level above the public_html folder) so that sensitive files are placed outside the publicly accessible web folder (where public_html is) - just an extra precaution.

Add the following line in your custom php.ini file:

include_path = ".:/home/your_cpanel_username/your_include_folder_name"

Make sure to replace your_cpanel_username with your actual cPanel username and replace your_include_folder_name with the actual folder name you have created in your hosting account's home directory.

Setting up include path this way is recommended as no one can access folders in your home directory which is one level higher than your web root (public_html) directory.

2.Include folder is at your hosting account's web root directory.

Web root directory is the folder you place all your web pages. When you connect to your hosting account via a FTP client, you should normally see a folder called public_html. This is the web root directory. Note that it might be named as something else by your hosting company. Please refer to your hosting account setup email or contact your hosting company if you are not sure.

Add the following line in your custom php.ini file:

include_path = ".:/home/your_cpanel_username/webroot/your_include_folder_name"

Make sure to replace your_cpanel_username with your actual cPanel username. Replace webroot with the actual folder name of the web root directory (e.g. public_html), and replace your_include_folder_name with the actual folder name you have created in your hosting account web root directory.

Note that there is a potential security issue related to the include folder if it's placed in web root directory. Click here to jump to the bottom section of this page to see how to fix it.


3.On Windows, the include folder can be any folder on your hard disk.

For example, two include paths defined in php.ini for my local site:

include_path = ".;C:\test;C:\php\PEAR"

php.ini is at C:\WINNT on Windows 2000 or Windows XP.

Note: Separate each include path by : (colon) on a Unix/Linux system. On Windows, the separator is ; (semi-colon).

Here is the example given in php.ini file in C:\WINNT folder:

UNIX: "/path1:/path2"

Windows: "\path1;\path2"
Thanks agains,
Michel
Replied 15 Dec 2011 12:50:43
15 Dec 2011 12:50:43 adum paul replied:
You can insert the content of one PHP file into another PHP file before the server executes it, with the include() function.If an error occurs, the include() function generates a warning, but the script will continue execution.
Replied 04 Feb 2012 08:49:26
04 Feb 2012 08:49:26 maria halons replied:
This can load files on the fly, and ignore the XML reference, they do not have to be XML!
Replied 13 Feb 2012 04:25:26
13 Feb 2012 04:25:26 anijikio Simond replied:
Thanks for nice reply
Replied 10 Jun 2012 05:49:42
10 Jun 2012 05:49:42 Jim Elliott replied:
It is always better to use require_once() than include()!
Replied 25 Jun 2012 08:50:36
25 Jun 2012 08:50:36 Inars Greinv replied:
Finally, I need to be able to pass about ten string values (text) back to the calling PHP file.
Replied 25 Jun 2012 09:43:54
25 Jun 2012 09:43:54 Jim Elliott replied:
In that case the included file must contain a function to set the variables or whatever. If you give more information it would help.
This reply was removed on 7/19/2012 4:12:33 PM.
See the changelog

Reply to this topic