Tag Archives: PHP

Installing PEAR for PHP on OSx

Along your PHP travels you will hear noises relating to installing something from PEAR to help you with some PHP snizz.

PEAR is simply a code repository, in fact PEAR stands for PHP Extension and Application Repository.

Installing it is a breeze assuming you already have php installed (you can verify this by typing php -v into your command line).

The first thing to check is that it isn’t already installed — a quick command will verify; pear version. If this brings back version information then you are good to go, if it reports Command not found then follow these simple steps.

Download the phar file at this location: http://pear.php.net/go-pear.phar

Run PHP on the file, so if you downloaded it to your Downloads folder then run sudo php go-pear.phar.

You will be asked some installation questions, I quite like the defaults but feel free to change whatever you want.

Next if you have accepted the defaults you will probably want to add PEAR to your path so that you can call it easily from the command line. I like to do this in vi but there are several ways to achieve it;

I run sudo vi ~/.profile and add in my PATH to the bottom of the document; export PATH=/Users/YOUR_USER_NAME/pear/bin:$PATH, I then reload my profile with . ~/.profile and that is me, now if I run pear version I get version information.

Share this on

Finding out what version of Apache and PHP a webserver is running when you have little access

There are better ways to do this, but let us assume you have no access via SSH or even FTP to be able to add a file.

Linux / OSx

Open up a terminal window and type the following;

curl --head http://thesite.com

This will return something similar to:

HTTP/1.1 200 OK
Date: Wed, 15 Aug 2012 10:41:32 GMT
Server: Apache/2.2.9 (Debian) PHP/5.3.8-1~dotdeb.1 with Suhosin-Patch
X-Powered-By: PHP/5.3.8-1~dotdeb.1
Set-Cookie: PHPSESSID=47d6a5e22c54307f211f67294f17495f; path=/
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Pragma: no-cache
Keep-Alive: timeout=15, max=100
Content-Type: text/html
Accept-Ranges: none
Connection: Keep-Alive

And as you can see in this information you can find the Apache and PHP information.

Share this on

Copying a CakePHP Project from Linux to Windows

Recently I had to make some changes to a website built on top of the CakePHP framework.

Most of the time I would develop within a Linux environment, making the edits locally and running them on a development server before pushing them onto a production server – the development server being pretty much a clone of the production server.

This time however I opted to do the changes locally (there were a lot of small changes that make pushing to a development server a bit of a pain).

My setup is Windows 7 with XAMPP installed on it, but the stages I had to follow should be similar for most setups.

I already had the website code downloaded onto my machine as that is where I make the edits – once I set up my hosts file in Windows and the virtual host directives in Apache I figured I would be 90% of the way there. I wish!

The first issue I encountered was this:

Warning: include(cake\bootstrap.php) [function.include]: failed to open stream: No such file or directory in C:\path\to\my\site\webroot\index.php on line 78

Warning: include() [function.include]: Failed opening 'cake\bootstrap.php' for inclusion (include_path='/path/to/cake') in C:\path\to\my\site\webroot\index.php on line 78

Fatal error: CakePHP core could not be found. Check the value of CAKE_CORE_INCLUDE_PATH in APP/webroot/index.php. It should point to the directory containing your \cake core directory and your \vendors root directory. in C:\path\to\my\site\webroot\index.php on line 79

Obviously you go where the errors take you, sure enough if I open webroot/index.php I can see that it is linking to a folder structure that I don’t have locally.

I jump onto the development machine and grab the files that it was linking to, pull them into a folder and put the correct path in, like so:

if (!defined('CAKE_CORE_INCLUDE_PATH')) {
		//define('CAKE_CORE_INCLUDE_PATH', "/old/linux/path/cake" );
		define('CAKE_CORE_INCLUDE_PATH', "C:\new\windows\path\cake" );
	}

Now when I go to my site I get this error:

An Internal Error Has Occurred

Useful! But before that displays I do get some of my layout displaying on screen, so there is a bit of progress.

In order to squeeze some more information out of cake I go into config/core.php and change the debug level.

//Configure::write('debug', 0);
Configure::write('debug', 2);

Now when I refresh the page I get plenty of useful information! In my case it was because I was an idiot and forgot to hook up my database – I won’t go through the steps to do that but basically export the development database, import it into your local and make sure you set up the same users with the same privileges.

I tend to try and keep my local setup the same as development so I am not messing about with config files too much.

My next issue that is immediately apparent after a refresh is that CSS is not getting loaded (and I am going to guess that JavaScript isn’t getting loaded either).

I have found that the best way to begin to track down this sort of issue is to view the source of the page, first of all is the code in place to try and load in CSS? In my case it was.

Next question, does the path to the CSS file look right? Again in my case it does.

Final question, when you click on the link to the CSS file, does it load? In my case it does not.

This immediately feels like an issue with either Apache or .htaccess redirects.

First let me check Apache to make sure that mod_rewrite is turned on, I check my httpd.conf file and sure enough it is enabled. Which means there is probably something wrong with how my site is locally trying to rewrite things. Onto .htaccess!

Annoyingly in my case htaccess was fine – in my case it turns out that I stupidly didn’t have my path in my virtual host set to the webroot, once I added this into my virtual host directive and restarted Apache I started to get my CSS again.

That is almost us finished – everything seems to be working but there was one edit made earlier that needs to be tidied up – the location of the Cake folder is something you want to worry about long term, having to remember to change that whenever you move code about is just a pain.

You don’t want to exclude webroot/index.php from any deployment solution because at some stage you might change something that you want to deploy, so I suggest doing something like this:

	if (!defined('CAKE_CORE_INCLUDE_PATH')) {
	    $currenturi = $_SERVER['DOCUMENT_ROOT'];
	    $mypos = strpos($currenturi, 'C:');
	    if ($mypos !== false) {
	    	define('CAKE_CORE_INCLUDE_PATH', "C:\my\windows\path\to\cake" );
	    } else {
	    	define('CAKE_CORE_INCLUDE_PATH', "/linux/path/to/cake" );
	    }		
	}

This will check for “C:” in the document_root variable, if it is there then we must be on a Windows machine, so lets display our local path. If not the Linux path will be used. (Note, it isn’t just because this is Windows vs Linux, any two setups can do this – with Linux to Linux you might have your files in your home directory, in which case you would look up your name as the differentiator)

Share this on

PHP – Turn UK Date into US Date

This is something I find myself needing to do quite frequently. You deal with UK dates as input and output of a system but somewhere in the middle they need to convert to US dates.

A quick way to shove a UK Date into a US format is to to an explode on the UK date by whichever separator it is using (normally ‘/’, sometimes ‘-’) then glue the bits back together with the second element swapped with the first (moving month and day around).

I have compiled this into a quick method:

/**
* A very simple function to change the date from the UK
* format to the American format.
*
* @param string $uk_date Assumed to be in the format day month year
* @param string $separator_input What divides the date up goung in.
* @param string $sepatator_output What divides the date up going out.
*
* @return string The date formatted to suit the US formatting.
*
* @author Toby Osbourn 
*/

function makeUSDate($uk_date, $separator_input = '/', $sepatator_output = '/')
{
    list($day, $month, $year) = explode($separator_input, $uk_date);
    return $month.$sepatator_output.$day.$sepatator_output.$year;
}

This is also part of my Useful PHP Functions file on Github.

Share this on

Useful PHP Links

Another post that will be a dumping ground for useful PHP links I have found over time.

Latest Edited: 18/06/12

9 Useful PHP functions – Including string compression and functions with an arbitrary number of arguments.

PHP Security Guide – A really nice security guide for sites built in PHP.

50 Extremely Useful PHP tools - From Smashing Magazine

Share this on