PHP

Posts about the server side scripting language PHP

Starting And Stopping Apache Using Windows .bat Files

Following on from the post about starting and stopping MySQL using .bat files I decided to add commands to these files that also controlled Apache in the same way. This turned out to be a lot easier than trying to start and stop MySQL as the command line commands for httpd executable worked very well in .bat files.

So, to start Apache use the following line.

"C:\Program Files (x86)\Apache Software Foundation\Apache2.2\bin\httpd" -k start

And to stop the server use the following line.

"C:\Program Files (x86)\Apache Software Foundation\Apache2.2\bin\httpd" -k stop

Pixelate Images With PHP

The other day I needed to hide an image but keep the original image intact so rather than do complicated things with light and dark filters I decided the best way would be to pixelate the image. All that is required to pixelate an image is to shrink it down and then blow it up to the original size, thus reducing the quality of the image.

The following code will do this process for a jpeg image. The pixelate amount variable is used to reduce the width and height of the first image using division and then multiply them back again to the original values for the output image. Remember to change the ImageCreateFromJPEG() to the format you want.

Excel Document Scanning With Zend_Search_Lucene

Zend_Search_Lucene offers some powerful document scanning capabilities, and there are a few different formats that are useful for the search engine to index.

To allow the indexing and searching of Excel documents using Zend_Search_Lucene you need to use the Zend_Search_Lucene_Document_Xlsx class. However, to use this class you must have the Zip module installed with PHP. For Windows users this means editing your php.ini file and uncommenting the following line:

extension=php_zip.dll

For Linux users you will need to recompile PHP with the --enable-zip configure option.

Create and/or open the index in the normal way and you can index Excel documents using the following code.

Multi Page Forms In PHP

Multi pages forms are just as they sound, a single form spread across multiple pages. These are useful in terms of usability as it can break up an otherwise dauntingly big form into smaller chunks. It can also be useful if you want to process some of the results in order to determine what forms the user sees on later steps.

There are two ways in which it is possible to do this using PHP.

The first (and simplest) is just to cycle through the items submitted on a previous form and print them out as hidden fields. Our first page source code will look like this:

Validate EAN13 Barcodes

EAN13 barcodes are commonly used to label products in Europe. If you want to know more about how they work then please view the Wikipedia entry on European Article Numbers.

EAN13 barcodes are actually 12 digits long and are validated by using a check digit, which is placed at the end, making the code 13 digits long. The check digit is worked out by the following process:

Hide And Unhide Code With PHP

If you are selling a system the last thing you want is for people to copy the system and pass it on for free. There are numerous ways to implement parts of the system that will stop this from happening.

By far the easiest is to create a section of code that is hidden, the removal of which will cause the application to fall over. It could even be as simple as a link back to your site so that even if you give you application away for free, you will always have that link present.

This method involves the use of a function called eval(), which takes PHP code as a string and interprets it to produce output. Here is an example that prints a link to #! code.

Print Array Without Trailing Commas In PHP

I have previously talked about Removing commas from the end of strings, but it is also possible to use the implode() function to do the same sort of thing.

implode() takes two parameters, the separator and the array, and returns a string with each array item separated with the separator. The following example shows how this function works.

$array = array(1,2,3,4,5,6);
$list = implode(',', $array);

The $list variable will now contain the string "1,2,3,4,5,6". However, things tend to become messy again when you have an array with empty items in it.

PHP Array Of Australian States

Use the following array to print out a list of Australian states.

$australian_states = array(
    "NSW" => "New South Wales",
    "VIC" => "Victoria",
    "QLD" => "Queensland",
    "TAS" => "Tasmania",
    "SA" => "South Australia",
    "WA" => "Western Australia",
    "NT" => "Northern Territory",
    "ACT" => "Australian Capital Territory"
);

 

PHP Array Of Canadian States

Use the following array to print out a list of Canadian states, also know as provinces.

$canadian_states = array( 
    "BC" => "British Columbia", 
    "ON" => "Ontario", 
    "NL" => "Newfoundland and Labrador", 
    "NS" => "Nova Scotia", 
    "PE" => "Prince Edward Island", 
    "NB" => "New Brunswick", 
    "QC" => "Quebec", 
    "MB" => "Manitoba", 
    "SK" => "Saskatchewan", 
    "AB" => "Alberta", 
    "NT" => "Northwest Territories", 
    "NU" => "Nunavut",
    "YT" => "Yukon Territory"
);

Here is the same array, but with the French versions of the states.

PHP Array Of USA States

Use the following array if you want to print out a list of USA states either as a list, or as a select box.