PHP: include function is blocked - php

My PHP files are not able to call the include function anymore. I suppose my hosting changed php settings. Is there any workaround to this problem? Can I enable the include function in php.ini/.htaccess files? How? My host is using PHP version 4.4.9.
EDIT - All the files that I am trying to include are local files and not files on another server. The include functions were working fine until a few days back. They have suddenly stopped working.

i think is permission problem,
do you see error in error log file ,
or see error in the screen ?

Maybe your host changed the path to your www.
Are you using relative or absolute paths in your include()?

This looks and feels wrong, but could this work?
function includeFile($path) {
if (!file_exists($path)) {
return false;
}
$contents = file_get_contents($path);
eval($contents); // ewww
return true;
}

Related

Create variable that contains working path for including files on both localhost and live server (PHP)

I am working on a project with a lot of includes and requires. I don't want to change this everytime I deploy from localhost to my live server.
So I thought to use $_SERVER['DOCUMENT_ROOT'], which worked fine until I checked my git which pushes directly to a backup folder on my live server. It is not in the root, but one level lower.
All the files in this backup folder that use include or require get their files from my server root (public_html/) not the current folder the project is in (public_html/backup/).
So this wouldn't work. After that I tried getcwd();, this fixed above situation but now I can't include files on my localhost.
So now I thought to make a check to see if the file is currently on my local pc or on my live server like this:
$checkpath = getcwd();
$localcheck = 'C:\\';
// Test if path contains C:\
if(strpos($checkpath, $localcheck) !== false){
$absolute_path = $_SERVER['DOCUMENT_ROOT'];
} else{
$absolute_path = getcwd();
}
My only problem is how can I include this for all files when including is my issue?
Maybe there is a better way to get a path that always works no matter if it is live or local?
You can use set_include_path($absolute_path); to change the directory include/require looks from. You would then technically only need to call this once in every PHP request. Beware that you need to provide the actual path to set_include_path. For example, you would need to provide "/home/username/directory" instead of "~/directory".
set_include_path documentation.

xampp include doesn't work

Good morning at all! I have an issue that I don't understand.I've XAMPP
localhost files where include function doesn't work in one file, while
in other files works without problems.I'm running Chrome browser. I've
already tried to clear browser cache, but issue still exists. Why?
Include function works in this pages:
"index.php",
"blog.php",
"history.php"
Include function doesn't work only on this page:
contacts.php
The code is here:
<?php include('include/menu.php'); ?>
You need your absolute path to the include file correct by prefixing with $_SERVER['DOCUMENT_ROOT'];
In all your includes() do :
include($_SERVER['DOCUMENT_ROOT'].'/include/menu.php');
it would help to know two things: 1) where are the files located in? 2) what is the error message?
maybe the solution is simple since there is only the lack of the correct path:
include(__DIR__."/blog.php");
etc.
__DIR__ contains the path of the current script and is very useful to go up/down the directories based on it.
I've solved issue. I copied another existent page such as index.php with menu and footer correctly included, and I changed the body content and now it works.

Unable to get correct path in PHP due to server settings

I am not an expert on servers and one of my friends wanted to use some of my PHP scripts I wrote, but has a problem with his server and I was unable to find how to fix this.
Basically the problem is in generating paths to files.
In my script, I use the following variables to get the path:
$path = "/template/"; // path to the template folder
$baseURL = $_SERVER['DOCUMENT_ROOT'].$path;
Then I include my pages for example like this:
include($baseURL."scripts/functions.php");
On my server, this works fine and when I tried to echo the baseURL parameter I get this:
/data/web/virtuals/104571/virtual/www/template/
However, on my friendĀ“s server, it just throws "PHP Fatal error: Call to undefined function" - because I checked and the functions.php is not being loaded,
When I tried echoing baseURL on my friendĀ“s server, I got nothing.
So I tried viewing phpinfo() and interestingly, I did't find the DOCUMENT_ROOT there at all and the paths are just weird, the PHP error shows:
PHP Fatal error: Call to undefined function convertT() in H:\webspace\hostings\kocher.es\hosting\www\meteotemplate\index.php on line 14
As I dont really understand servers so I dont know what this means, why it has that H:/ drive there and more importantly, how to fix this....
Here is the phpinfo() displayed:
http://kocher.es/meteotemplate/
If anyone knew how to fix this I would very much appreciate it, thanks.
try __DIR__
example:
$basePath = __DIR__;
$templatePath = $basePath."/template/"; // path to the template folder
include($templatePath."/scripts/functions.php");
__DIR__ will return file base directory and its different in different files which mean you must be careful from when will call it
You can try with dirname(), pathinfo() and realpath() to determine the base path instead using $_SERVER['DOCUMENT_ROOT']
Please add document root path in httpd.conf file of apache
Or try _SERVER["CONTEXT_DOCUMENT_ROOT"]

WAMP 403 forbidden error when using 'glob()'

I'm trying to make PHP grab the paths to all images in a folder (and then select one at random), but I keep getting a 403 - forbidden warning.
I tried using both glob() and scandir(), as well as trying the path directly in the browser.
Here is my code (using glob()) -
$directory = plugins_url('images/backgrounds', __FILE__);
if(is_dir($directory)) :
$backgrounds = glob($directory . '*.{jpg,jpeg,png,gif}', GLOB_BRACE);
endif;
I'm using WAMP and developing locally, so I can't see any reason why Apache wouldn't have access to the folder in question.
The $directory is correct, so it's not that it doesn't exist that is causing a problem (a 404 error is returned if I deliberatly alter the path to one I know doesn't exist).
I am preventing indexing via .htaccess but I have since removed this line and it made no difference.
Can anyone please suggest what I might check to try and fix this issue? Thanks.
Edit
I should add that my PHP and Apache logs seem to not be logging this event.
In your code, you have used - plugins_url which looks like a Wordpress function which will return a URL.
But you have to pass a file path to the glob function.
So you can use plugin_dir_path(), a wordpress function to get absolute path of plugin directory which can be used in glob function.

Why would getcwd() return a different directory than a local pwd?

I'm doing some PHP stuff on an Ubuntu server.
The path I'm working in is /mnt/dev-windows-data/Staging/mbiek/test_list but the PHP call getcwd() is returning /mnt/dev-windows/Staging/mbiek/test_list (notice how it's dev-windows instead of dev-windows-data).
There aren't any symbolic links anywhere.
Are there any other causes for getcwd() returning a different path from a local pwd call?
Edit
I figured it out. The DOCUMENT_ROOT in PHP is set to /mnt/dev-windows which throws everything off.
Which file are you calling the getcwd() in and is that file is included into the one you are running (e.g. running index.php, including startup.php which contains gwtcwd()).
Is the file you are running in /dev-windows/ or /dev-windows-data/? It works on the file you are actually running.
Here's an example of my current project:
index.php
<?php
require_once('./includes/construct.php');
//snip
?>
includes/construct.php
<?php
//snip
(!defined('DIR')) ? define('DIR', getcwd()) : NULL;
require_once(DIR . '/includes/functions.php');
//snip
?>
#Ross
I figured it out and updated the OP with the solution.
#Ross
I thought that getcwd() was returning a filesystem path rather than a relative url path.
Either way, the fact remains that the path /mnt/dev-windows doesn't exist while /mnt/dev-windows-data does.
#Mark
Well that's just plain weird! What's your include_path - that could be messing thigns around. I've personally ditched it in favour of contants as it's just so temperamental (or I've never learned how to do it justice).

Categories