Recent Posts

Showing posts with label .htaccess. Show all posts
Showing posts with label .htaccess. Show all posts

How to fix: open_basedir restriction in effect.

Modify the open_basedir settings in your PHP configuration (See Runtime Configuration).

The open_basedir setting is primarily used to prevent PHP scripts for a particular user from accessing files in another user's account. So usually, any files in your own account should be readable by your own scripts.

Example settings via .htaccess if PHP runs as Apache module on a Linux system:

<DirectoryMatch "/home/sites/site81/">
    php_admin_value open_basedir "/home/sites/site81/:/tmp/:/"
</DirectoryMatch>

How to: URL Rewrite with PHP


Covert this URL:

url.com/picture.php?id=51
to this another

picture.php/Some-text-goes-here/51
You can essentially do this 2 ways:

The .htaccess route with mod_rewrite

Add a file called .htaccess in your root folder, and add something like this:

RewriteEngine on
RewriteRule ^/Some-text-goes-here/([0-9]+)$ /picture.php?id=$1
This will tell Apache to enable mod_rewrite for this folder, and if it gets asked a URL matching the regular expression it rewrites it internally to what you want, without the end user seeing it. Easy, but inflexible, so if you need more power:

The PHP route

Put the following in your .htaccess instead:

FallbackResource index.php
This will tell it to run your index.php for all files it cannot normally find in your site. In there you can then for example:

$path = ltrim($_SERVER['REQUEST_URI'], '/');    // Trim leading slash(es)
$elements = explode('/', $path);                // Split path on slashes
if(count($elements) == 0)                       // No path elements means home
    ShowHomepage();
else switch(array_shift($elements))             // Pop off first item and switch
{
    case 'Some-text-goes-here':
        ShowPicture($elements); // passes rest of parameters to internal function
        break;
    case 'more':
        ...
    default:
        header('HTTP/1.1 404 Not Found');
        Show404Error();
}
This is how big sites and CMS-systems do it, because it allows far more flexibility in parsing URLs, config and database dependent URLs etc. For sporadic usage the hardcoded rewrite rules in .htaccess will do fine though.


How to solve: Warning: open_basedir restriction in effect

This warning can appear with various functions that are related to file and directory access. It warns about a configuration issue.
When it appears, it means that access has been forbidden to some files.
The warning itself does not break anything, but most often a script does not properly work if file-access is prevented.
The fix is normally to change the PHP configuration, the related setting is called open_basedir.
Sometimes the wrong file or directory names are used, the fix is then to use the right ones.
The open_basedir setting is primarily used to prevent PHP scripts for a particular user from accessing files in another user's account. So usually, any files in your own account should be readable by your own scripts.
Example settings via .htaccess if PHP runs as Apache module on a Linux system:
<DirectoryMatch "/home/sites/site81/">
    php_admin_value open_basedir "/home/sites/site81/:/tmp/:/"
</DirectoryMatch>