How to avoid including database connection file on each page?

Suppose I have a config.php page that contains

<?php $username = "your_name"; $password = "your_password"; $hostname = "localhost"; //connection to the database $dbhandle = mysql_connect($hostname, $username, $password) or die("Unable to connect to MySQL"); echo "Connected to MySQL<br>"; ?> 

I know that I need to include this config.php file where I want to provide a database connection. But is there a way to do this globally so that I do not need to include a connection file on every required page? so I saw a lot of script where they run a request on a specific php page without including a database connection file!

+7
php mysql
source share
3 answers

My usual approach is to create an autoloader function to pull classes as needed, and the class to handle the database connection. However, this may be a bit overwhelming for some projects. Also, this configuration has yet to be included before my code, which calls the class and expects something to go and get.

Many open source PHP programs will set such values โ€‹โ€‹in a configuration file that will be included very early along with any libraries, etc.

For example (LM), NucleusCMS uses a configuration file to load global functions and other core classes, and so all that is needed is for the PHP script to make sure that config.php is enabled.

Sometimes the code you are viewing is intended to be included at runtime with other code, and therefore believes that these settings are already set. For example, I am currently working on a project that redirects all calls to PHP files to start.php, which does what is necessary, and then calls the file after that, and then finally calls the template to process the output.

If you really want to not include it at all, then there is another option, but it is not recommended. You can set the auto_prepend_file php.ini directive and include the file in EVERY script that the server runs. As I said, you probably don't want this.

However, if you want the script to handle inclusion for you, and this is important, then using the .htaccess and start.php example (actually ./engine/start.php), then this might work for you better than spoofing directives ini. However, the disadvantage is that you need to check that your .htaccess redirects work correctly by passing the file name back to start.php, and then make sure some naughty user is not asking your script to do something bad. After you have cleared and sanity checked the input, you can go ahead and require_once ($ filename) ...

My .htaccess looks something like this:

 RewriteEngine on RewriteRule ^(.*).php?$ ./path/to/start.php?page=$1 [QSA,L] 

However, as I said, if you donโ€™t have a specific need to do this, then it would be safer if you allow ANY input in the include / require line and have to spend a lot of resources on EVERY page load, make sure the request is safe.

To be honest, the most common practice is to have a file called common.php, and everything that includes scripts to get there, and just

 require_once('./path/to/common.php'); 

at the top of each page. If your project ends with a large number of classes and / or libraries in it, then you may be glad that you did it.

+1
source share

Usually I process connections to the database, this is to include my configuration file in the header.php file (which processes the navigation file, logo, etc.), and the header file should be included everywhere to maintain uniformity.

So this is one solution you can try

0
source share

You must include this code on every page. This works PHP.

If you donโ€™t want to do this manually and your project is large enough, you should probably consider using a framework in your project. A good PHP infrastructure can take care of connecting to the database and many other common tasks, such as handling requests / responses, checking, etc.

There are many PHP frameworks available and you can choose what suits you best.

0
source share

All Articles