I am a reseller on cPanel with Apache and PHP 5.5, and my server owner has just installed the latest PHPMailer on the shared folder, so it can be accessed from:
require "PHPMailerAutoload.php";
but, all of my current sites have PHPmailer in my class folder, with an extended class to customise the mailer details to each site.
So, my PHP class has:
require_once "class.phpmailer.php";
class ChoiceMailerSMTP extends PHPMailer {
/**
* Mailer
*
**/
My issue is that since my server guy has put on the shared folder, the new PHPMailer, that now all of my require calls are searching the Shared folder BEFORE searching the local folder, and therefore loading the wrong phpmailer.class.php.
This is really frustrating, although in this instance it's (only) a hassle - if he puts other files in the shared folder for himself or for others, how am I meant to ensure that my PHP includes and requires run the local rather than shared files?
Is there a line in php.ini I can use? Or perhaps a cPanel setting?
require "./file" doesn't work.
EDIT:
I've been doing some research on my system and found that set include path is already set to:
include_path = ".:/usr/lib/php:/usr/local/lib/php"
So any help why the local directory is only searched after the include path directories?
The include order of relative paths depends on the set include path, see set_include_path. You can either modify that include path to prioritise your local folder, or you can use an explicit absolute path for includes to begin with:
require_once __DIR__ . '/class.phpmailer.php';
What's wrong with the shared version? Is it older than yours?
You can adjust the order that directories are searched by changing the include_path directive in php.ini.
You should be loading the autoloader anyway, as it always looks for class files relative to itself, so, similar to #deceze's suggestion, you should do:
require_once __DIR__ . '/PHPMailerAutoload.php';
Related
I've spent the past few months building a website on localhost. Throughout the site, instead of using relative paths, I used $_SERVER["DOCUMENT_ROOT"] to access the root of my server so I could reference files from there. However, I just decided to upload my site to 000webhost. As I soon found out, and for those of you who use them, you are probably aware, that their server root is different than your actual directory in which you upload your files. I believe it's virtual hosting... anyway, my $_SERVER["DOCUMENT_ROOT"] now throws errors along the lines of this on the site:
Warning: include_once() [function.include-once]: open_basedir restriction in effect. File(/usr/local/apache/htdocs/mypath) is not within the allowed path(s)
Every other site I looked at said that you should just replace $_SERVER["DOCUMENT_ROOT"] with the home directory provided to you by 000webhost. However, if I want to change hosting services in the future, I'm screwed. Is there any way to use $_SERVER to access a set virtual directory or use htaccess or something to make my code work? I tried using DocumentRoot in a htaccess file in my root directory, but it threw a 404 error when trying to access the page.
I would never recommend using DOCUMENT_ROOT. For example, it is useless if you ever want to run scripts via the command line or cron.
I would instead, set the include_path via one of your scripts. Something that is included in every page (like some sort of config or bootstrap script).
For example, say you have config.php in your app's root directory (could be doc root but it's not important) that is included on every page
set_include_path(implode(PATH_SEPARATOR, array(
__DIR__, // this is the directory of this file, config.php
get_include_path()
)));
Then, you can safely use relative paths from this location in your include / require statements
include 'foo.php'; // APP_ROOT/foo.php
include 'somedir/bar.php'; // APP_ROOT/somedir/bar.php
When setting the include path, it doesn't really matter where the script resides, just construct the appropriate path. Say config.php lived in APP_ROOT/configs/config.php, the include path would be
realpath(__DIR__ . '/..') // realpath just resolves the path traversal
Decided to go with the following method:
http://www.000webhost.com/forum/customer-assistance/4857-document_root-environment-variable.html
Works!
I've got a few Framework folders and plugins in the xampp/library folder.
I've included it in my php.ini include_path.
But when I use:
$mail = new Zend_Mail();
It can't find it.
So i added:
require_once 'Zend/Mail.php';
and it worked...
Should I specify each folder seperately in php.ini or is including the entire Library folder fine...
You need to require Zend.php so that autoloading starts working. Setting the include path means when you provide file names in your require or include statements php will search on those folders to see if a matching file exist. Include path does not mean auto load.
I am using XAMPP for PHP development, new to this, was previously familiar with WampServer. I have a require_once statement like this
require_once('config.php');
I assumed it would include the file in the current directory, but it is fetching a file from PEAR directory because the path to PEAR is also set in the include_path directive in php.ini.
However if I change the include_path to just '.' which is the current directory, it seems to work fine.
This had worked fine for me before in WampServer, no clue as to what causes this (it has always looked in the current directory before fetching form include paths). Is this a problem with PHP or something to do with XAMPP? And any solutions for this?
Well, you already found the problem: the include path directive is different.
Every include is looked up relative to the include path, the first matching file is used. If you want to explicitly use a file in a specific directory, use an absolute path:
require_once __DIR__ . '/config.php';
I am using xampp to develop my php application. Few days back I installed pear ti use DB abstraction. After that, I couldn't use include files from parent directory, however I can include from sub-driectories.
Here is what I see when I check my include path
.;E:\xampp\php\PEAR
I tried changed include path using set_include_path to the location where my files are stored, then the application failed to load Pear files.
Any help appreciated.
Easiest way to prepend to the include path stack is...
set_include_path(implode(PATH_SEPARATOR, array(
'path/to/app/includes',
'path/to/any/other/includes',
get_include_path()
)));
If you really want to use set_include_path, you can do it like this:
set_include_path(get_include_path().PATH_SEPARATOR.'path_to_parent');
Use the predefined constant DIRECTORY_SEPARATOR in case your code moves to a server that uses a different directory separator.
Personally if I needed to set the path specially for a particular site, I would try to set the path in the .htaccess file in the site's web root. It provides a more obvious place to look for site-wide configurations like the include_path. Here is the line you would put in the .htaccess file:
php_value include_path ".;E:\xampp\php\PEAR;path_to_parent"
or on a Linux server:
php_value include_path ".:some_path/PEAR:path_to_parent"
I have a php script that has the following requirement command: require_once 'HTTP/OAuth.php'; the file HTTP/OAuth.php is in php's include_path that is .:/usr/lib/php.
Nevertheless in Eclipse the require_once line is marked with the following warning: Include filename: 'HTTP/OAuth.php' doesn't exist in project:
How can I make my project see the include_path so it can find the require_once file?
I honestly think you can't access any folder outside the root of your web application, basically you can't go any further than /. Imagine you use a shared web hosting service and someone access your files at will, it's just not safe at all.
You can, however, copy the file and put in inside your application scope. E.g: require('/includes/OAuth.php');.