PHP and Local Testing

So you’re developing a website and you’ve decided, or been required, to use PHP to handle some of the server side controls. Not a problem. You’ve done the reading, have your favourite help sites ready, and are down with the challenge. So you startup the website locally to do some testing. And nothing happens.

If this is your first time working with PHP, this can be a daunting experience. Many, many moons ago, when using PHP for the first time, I was frustrated and wondered why it wasn’t working. I’m not ashamed to say that the fact it is server-side, and requires PHP actually be installed, didn’t immediately dawn on me.

So, let’s ignore that part of the process and jump straight into the fix. First, you’ll need to download the version of PHP your target server is running. Create a new .php file on the server in question and put this code in it.

<?php phpinfo(); ?>

Now navigate to it on the hosted website. This will output all the details about your php install.

As an alternative, you could go onto the server in question, open a command prompt, and input the following command:

php -v

Either solution will work.

All you need to do now is get the correct version of PHP, extract it, and go into the folder and rename either php.ini-development or php.ini-production to be simply php.ini. Be aware, the development file, by default, includes development flags and could affect what gets displayed (such as when you use deprecated or obsolete methods).

So, now your PHP enabled websites will work on your PC! Nope. You need to start the PHP server to do this. Simply launch your command prompt and use this set of commands:

cd drive:\websiteFolderLocation\ php –S localhost:8000 

The drive is, of course, the drive letter of the folder, while the location is the path to the root folder of your website. As for the localhost port, you can make that anything you’d like. Now you’re ready for some testing.

All that said, opening a command prompt everytime you want to turn on your server is annoying. Besides, we’re developers. Any problem is worth over analyzing and coming up with a script to complete it for us, no matter how inefficient the time usage is!

So, here’s what we’re going to do:

  1. Write a batch script to launch the PHP server.
  2. Pin that script to our start menu.
  3. Give the script a cool command prompt icon.

So, our first step is to open notepad or something similar (I’m a fan of notepad++), and enter this script:

cd %userprofile%\websiteFolderPath\
php -S localhost:8000

The second line should look familiar, as we just talked about it earlier. The first line assumes you saved your website in your Windows user folders. Otherwise, an explicit path will be necessary. The Pause command keeps the console window from closing once things are finished running. Save the script with an appropriate name (like “LaunchWebsiteNamePHP.bat”) and copy down the path.

So, now you have to pin Command Prompt where you want the script to be accessed (either your start menu or taskbar). Why Command Prompt? Because that way we get the cool icon.

Once it’s pinned, right-click and go into properties. Under the Shortcut tab, in the Target field, add this to the end of the string

/c "%userprofile%/PhpScriptPath.bat"

(without the curly braces) with the file path you noted down earlier. Under the General tab, give the shortcut an appropriate name, like “Launch websiteName PHP”.

And bang, you have an easy way to launch your website’s php server from a pinned item.

This entry was posted in Web Development and tagged . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s