JpGraph Error Can’t access PHP_SELF, PHP global variable

One of the sites I work on uses JpGraph to generate graphs for their reports. A few months ago we moved the site from an old server with PHP 4 to a new server with PHP 5 and started getting the error “JpGraph Error: Can’t access PHP_SELF, PHP global variable”. This post looks at the error and a couple of possible solutions to it.

The error message

Instead of generating the requested graph, JpGraph instead displays an image as follows, containing the error message “JpGraph Error: Can’t access PHP_SELF, PHP global variable. You can’t run PHP from command line if you want to use the ‘auto’ naming of cache or image files.”

JpGraph Error Can't access PHP_SELF, PHP global variable

We were running this from a web browser and not from the command line so the error message seemed a little odd. However it was clear that the functions are trying to access the PHP_SELF variable in some way that isn’t defined.

Reason for the error

A quick dig through the source and I found that it’s an old version of JpGraph designed for PHP 4. In several places in the jpgraph.php file were references to PHP_SELF using the old HTTP_SERVER_VARS array like so:

 global $HTTP_SERVER_VARS;

 if( $aCacheName=='auto' )
     $aCacheName=basename($HTTP_SERVER_VARS['PHP_SELF']);

How to resolve the error

There are two versions of JpGraph currently maintained, one for PHP 4 and one for PHP 5. You can download either of these from the JpGraph download page.

I figured grabbing the PHP 5 version might end up taking a while to test and make sure everything was working correctly so instead made a minor change to the jpgraph.php file by adding the following to the top of the script:

$HTTP_SERVER_VARS = array(
  'PHP_SELF' => $_SERVER['PHP_SELF']
 );

Each function call does “global $HTTP_SERVER_VARS” so even though this array won’t have gobal scope in PHP 5 it will still be available to all the functions that use it.

The alternative hack to the file would be to replace all the instances of $HTTP_SERVER_VARS with $_SERVER. The following example shows the earlier example changed to be like this.

 if( $aCacheName=='auto' )
     $aCacheName=basename($_SERVER['PHP_SELF']);

Conclusion

The “JpGraph Error: Can’t access PHP_SELF, PHP global variable” error is caused by the use of $HTTP_SERVER_VARS when running the scripts under PHP 5, a global array which has been deprecated and is no longer available in PHP 5 unless you enable the long arrays in the PHP config. The solution is to get the PHP 5 version of the library or apply one of the “hacks” I have supplied in the above examples.

Categories PHP

Command line arguments for the PHP CLI

The PHP command line interface or CLI allows you to run PHP scripts from the command line. It also has a number of flags that can be passed to it which allow you to see which modules are available, do a syntax check on a file, view PHP information etc. This post gives a basic …

Read more

Categories PHP

Command line arguments with a PHP CLI script

PHP has the getopt() function for getting options or command line arguments from a CLI PHP script. This provides a simple way to get values from the command line like e.g. “-a foo”. This post looks at how to do this. More details can be found in the PHP getopt() manual page. $_SERVER[‘argv’] The command …

Read more

Categories PHP

Fix the “call to undefined function ImageCreateFromPNG” error in PHP on Debian

If you call the ImageCreateFromPNG function (and similar image processing functions) in PHP and do not have the GD module installed you will get the error message "Fatal error: Call to undefined function ImageCreateFromPNG()" error message. This post shows how to install the PHP GD module on Debian Linux and the instructions should also work on all Debian derived distros such as the Ubuntu family.