4WebHelp: Configuring PHP in a shared hosting environment

Shared web hosting always has been and always will be a low cost way of hosting your average website. After all, most people cannot afford to rent/buy a dedicated server for their website. However, shared hosting presents a major inconvenience: customisability. Since you're "sharing" this server with other people, you can't just do what you want. Your host will decide on all server wide settings, in the interests of all. These settings will include PHP settings.

But let's say you need to change these settings: how are you going to do it? One way would be to get down on your hands and knees and plead with your host. But that's not exactly an easy or sure way of getting the PHP configuration changed. No, there's a much easier solution: set them yourself! "But they're server wide settings" I hear you say. Wrong! Most of the PHP settings which developers use can actually be changed by the user, surprisingly enough.

Now, let's get down to the technical aspect of this. Let's say I want to turn off magic quotes (automatic escape of special characters, usually in preparation for use in an SQL query). The relevant setting for this would be magic_quotes_gpc. You now have two ways of setting it to off:

  1. In your PHP script:

    <?php
      ini_set ('magic_quotes_gpc', 0);
    ?>

    This would turn off magic quotes in that particular script. Or you could include it in your template/header file, and have it take effect in all your PHP scripts.

  2. In a particular directory/your whole site:
    Let's say you're in a hurry, or you don't want to edit a script. Apache lets you control its settings in your .htaccess file. However, you can also control PHP settings from this file, using the php_value command. Simply place the following code in your .htaccess file:

    php_value magic_quotes_gpc 0

    You can place this file in different places:

Simple, eh? Now didn't that save you (and your server administrator!) a lot of trouble? Wink For a complete list of settings which you can or cannot set by yourself, please see PHP.net: http://www.php.net/manual/en/function.ini-set.php.

© 4WebHelp and Daniel

Latest comments on this tutorial
shedi912
how do i set php session save part with this tutor
smwaruka
Hey, so Daniel I have a ? for you:

If I am hosting 2 domains on one hosting plan and I would like the emails to go to different places, how would I configure that?  The issue I am having is that the emails will work for one domain or the other, not both. I thought you might know.
dave
My current host blocks the use of php_value in .htaccess files. That's fasthosts.co.uk
Copot
.htaccess can be used on both - Windows and Linux
Ian
Using:

<?php
ini_set ('magic_quotes_gpc', 0);
?>

will not do anything. Magic quotes are added before the script starts running, so by the time you call ini_set it's too late.

A better example might be:

<?php
ini_set('error_reporting', E_ALL);
?>

which enables all error/warning/debug/info messages to be displayed.
Crist
How can I set mod_rewrite on using htaccess or using a PHP script. I'm on shared hosting.
Dave (again
actually .htaccess is available to windows i now no this, however your article still gives wrong information. The value of magic quotes cannot be changed in a users script. It can only be changed in php.ini, httpd.conf or .htaccess.
Dave
Yes very useful indeed. However isn't .htaccess only available on UNIX? I might be advisable to recommend the embedded option as this will always work on both platforms -WINDOWS AND UNIX.
meisterflexer
Excellent tips, very useful!

Add a new comment


Page URL: http://www.4webhelp.net/tutorials/php/config_override.php
Back to the "pretty" page!

© 2024, 4WebHelp Team.