March 30, 2006

PHP - Using Libraries without requiring install on hosting server

You will find other articles relevant to this document in these sections:
Cameron Manderson @ 10:33 am

I have been asked before about using local PEAR libraries (or the like) that you have downloaded and having them run locally, not needing them installed on your hosting server.

To do this we need to add to the include path to point to your library paths.

When PHP looks for a file it checks the path from the current working directory, and then interogates the details it has received from the directive ‘include_path’ and checks there. Typically the include_path contains a directory path to the location that your hosting company has installed the components (PEAR and the like) to.

So, we just need to be able to change classpaths to point to our library folder.

Classpaths (or include paths) are seperated by a delimeter. The delimeter is different depending on what O/S you are operating on. So first we need to determine how to treat the classpath delimeter:

/**
* Returns with the delimeter to use
* @return String Path Delimeter for this O/S
* @access private
*/
function getClassPathDelimeter() {
if(defined(’PATH_SEPARATOR’)) {
return PATH_SEPARATOR;
} else {
// Determine the O/S
$osType = (substr(PHP_OS, 0, 3) == ‘WIN’) ? “WINDOWS” : “UNIX”;
if($osType == ‘WINDOWS’) {
return ‘;’; // Windows
} else {
return ‘:’; // Mac, nix, etc
}
}
}

Say we had our files like this:

myfile.php
lib/PEAR/*

We can make calls in myfile.php to any pear library (without have to fix paths etc) by calling:

$currentClassPath = get_include_path();

$delimiter = getClassPathDelimeter();

$newPath = getcwd() . ‘/lib/PEAR/’; // Don’t have to use getcwd(), could be dirname(__FILE__);, always use ‘/’ regardless of OS

$newClassPath = $currentClassPath . $delimiter . $newPath;

ini_set(’include_path’, $newClassPath);

Ofcourse this can be placed in a shorter version, and reused by making a class called ClassPath and adding the functions for static calls, eg.

ClassPaths::addClassPaths(’/lib/PEAR’, getcwd());

Therefore your setup is reusable and more concise.

Share and Enjoy:These icons link to social bookmarking sites where readers can share and discover new web pages.
  • del.icio.us
  • digg
  • Furl
  • Reddit
  • YahooMyWeb

4 Comments »

  1. […] If your hosting service does not want to help you install PEAR into their include_paths of PHP, follow this guide to using libraries without requiring your hosting service to install. Remember, if you are using $_SESSION in your application, be careful of some of the security holes that are opened. Have a look at my discussion of Sessional Handling with PHP and read the resources I have linked to. […]

    Pingback by melbourne chapter » PHP and Authentication Security — April 4, 2006 @ 10:18 am

  2. […] Overall the package is quite a neat way of handling your basic mail needs. Its inheritant support of alternate SMTP and handling of HTML means that it suits a majority of our requirements for basic packages. If we require more complex functionality we can simply extend and overwrite the required methods by extending the class PHPMailer. To learn more about PHPMailer visit their website here. If you wish to learn about setting up use of packages without requiring installation on your hosting service read here. You may wish to use this allong with encrypting the mail contents using gnuPG. […]

    Pingback by melbourne chapter » Mail with Phpmailer — April 11, 2006 @ 5:20 pm

  3. Also a note to make: Your hosting service may be operating an older version of the libraries that you require. If you need to ensure that your application does not load the classes from their location, try a:

    ini_set(’include_path’, getcwd());
    //print get_include_path();

    To overwrite their include_path first and ensure that it only finds your libraries.

    Comment by Cameron Manderson — April 15, 2006 @ 8:45 pm

  4. A continution of my previous post, if you are getting weird PEAR errors, such as fatal errors about call on non-objects, it may be because it is loading the wrong packages or creating an object of an older version. It may be necessary to use the above ini_set to check if this is the case.

    Comment by Cameron Manderson — April 15, 2006 @ 9:00 pm

RSS feed for comments on this post. TrackBack URI

Leave a comment