PHP

Reef wanted to broaden their audience with the use of social media to expand their brand awareness. Together, we created a quirky and cheeky Facebook application for users to share their desire to go to the Reef Life.

The Reef Life campaign is a summer campaign for Facebook users to express their desires to escape to the Reef Life. The application creates random sentences for users to share on their wall, their friends see the quirky wall posts and access the application to create their own.

The application has gained a lot of popularity with a lot of repetitive users.

This script will detect if a folder exists as well as create the directories if the folder doesn't exist.

 
	mk_dir_recursive('the/directory/you/want/to/create');
	function mk_dir_recursive($folder_name) {
		if(file_exists($folder_name)) {
			return;
		}
		$folder_name = rtrim($folder_name, "/\\");
		$parent = dirname($folder_name);
		if(!file_exists($parent)) {
			mk_dir_recursive($parent);
		}
		mkdir($folder_name);
	}
 

If you want to create an easy photo gallery, this script will allow you to resize images on the fly. It has a few pros and cons that you should be aware of before you start using it.

Pros
* Simple and easy to use :) - even I can use it.
* It resizes on the fly, so you can modify the code and it will affect all images you pass through it immediately. Great if you're short on storage on your web server, and doesn't require you to re-render all the images if you decide to change the size of the thumbnails.
* You can call this script for any image on your site, the image doesn't have to sit in an actual photo gallery.

Cons
* If you have a high traffic website, this isn't the best solution. You're best off resizing all your images when they're uploaded, not when they're are viewed. If you choose to use this script on a high traffic website, then at least cache the results so your web server isn't creating the same image 1000's of times over.
* This script only works with JPEGs, I'll create a new version (one day) to handle others.
* If you think of more cons, let me know.

The GalleryViewer Script: Download (Right Click > Save Target/Link As)
* Rename the file to galleryviewer.php.

Usage
Create Thumbnail: galleryViewer.php?version=thumb&image=the/url/to/the/image.jpg
Create Resized Image: galleryViewer.php?version=full&image=the/url/to/the/image.jpg

<img src="galleryViewer.php?version=thumb
    &image=the/url/to/the/image.jpg" alt="Thumbnail" />

Full Usage

The query string 'image' needs to be relative to the script.

User Level: Beginner to Intermediate

 
< ?php
  class MySQLHandler {
     var $mysql_host;
     var $mysql_username;
     var $mysql_password;
     var $mysql_marking_database;
     var $link;
     var $select_db;
 
     function MySQLHandler($database_name) {
        $this->mysql_host = "INSERT_HOST_HERE";
        $this->mysql_username = "INSERT_USERNAME_HERE";
        $this->mysql_password = "INSERT_PASSWORD_HERE";
        $this->mysql_marking_database = $database_name;
     }
 
     function open_mysql_link() {
        $this->link = mysql_connect($this->mysql_host,
           $this->mysql_username, $this->mysql_password);
        $this->select_db = mysql_select_db(
           $this->mysql_marking_database, $this->link);
        return $this->link;
     }
 
     function close_mysql_link() {
        mysql_close($this->link);
     }
  }
?>
 

The above PHP script (mysql_handler.php) can be used to connect to your MySQL database with PHP.

 
< ?php
   include('classes/mysql_handler.php');
   $mysql = new MySQLHandler("database_name");
?>
 

The include line will allow you to make references to the class, the 2nd line will allow you to initiate the class with a database name.

 
   $mysql_link = $mysql->open_mysql_link();
 

Then when you need a link for your MySQL functions, all you need to do is specify the above script and you're connected.

You may want to further secure this file by placing your host, username, and password details elsewhere.

This is one of the most common mistakes developers (of any language) make so I thought I should let you know why you shouldn't use this method.

The Scenario:
You have a page on your site and you don't want people to simply link to this page unless they have clicked on a link from your own site. Lets say your page is called 'page2.php' and it's located at 'www.your-site.com/page2.php.'

When the user enters the 'page2.php' page you can easily determine which page they came from, in PHP you can use the $_SERVER['HTTP_REFERER'] function to determine the referring page. With this function you would check that it contains www.your-site.com in the value.

For what ever reason why you might want to check the referring address to your page, checking the referral address against your own domain name isn't bullet-proof.

Why? The referral address is set by the user's agent (their Internet browser), some browsers have the option for you to edit this value and some browsers don't even set this at all.

There are many solutions to this scenario, but I would recommend using sessions where you can set the a session variable on 'page1.php' which contains the link to 'page2.php.' When the user enters the 'page2.php' page, you can then check to see if that variable has been set, if it hasn't then the user must have come from a different location.

It's a little bit more of a setup, but it's secure.

From recent tests conducted with StreetDirectory.com shows that PHP executes a lot faster on Windows Server 2008 now with their built-in CGI support. In fact it runs 130% faster then any other server operating system out there.

The built-in support of FastCGI enables PHP applications to run efficiently on Windows Server 2008, with significant performance boosts. As can be seen in the graph from the case study:

The Windows Server 2008 installation was a core installation with no performance tweaks, making it a out-of-the-box solution for all server administrators.

As you can note with the graph above that the 4000 users point, Windows Server 2008 becomes slower then Linux. Dennis from Microsoft says many server administrators would evaluate the performance by looking at where 75% of the maximum hits is located.

If we take the same approach from Dennis here, then most server administrators would be scaling out to another machine at about 45,000 sustained hits, whereas on the Linux box it would be around 30,000 sustained hits.

So what does this mean? Less servers for the administrators to maintain, less hardware to have problems with and less network/software issues to deal with. It will also be cheaper and more effective to scale out when needed or for 'just in case' scenarios.

I haven't been brain washed by Microsoft as a Microsoft Partner but I do seriously believe that Microsoft has made dramatic changes to both Windows Server 2008 and Windows Vista. Look out Linux!

Related Articles
* PHP runs up to 130% faster on Windows Server 2008
* Minty White - PHP runs up to 130% faster on Windows Server 2008
* Windows Server 2008: PHP runs up to 130% faster
* As Windows Server 2008 RTMs, Customers and Partners Adopting with Help of New Tools, Training

I use this script quite frequently now on PHP projects for simple error reports straight to my email account. Your clients are well pleased when you have the bug/error fixed before they report it to you.

All you need to do is copy the following script and paste it into a file and include it into your project.

ie:

 
< ?php
	include('error_control.php');
?>
 

The page error_control.php contains the script below

 
< ?php
 
	$debug = true;
	define("EmailNewLine", "\r\n");
	set_error_handler('error_handler');
 
	function error_handler($errno, $errstr, $errfile, $errline) {
		global $debug;
		// echo a message to the end user
		echo '<span class="error_message">There has been a major error, now that we know about this error we will fix it!';
 
		$message = 'Date: '. date("d") .'/'. date("m") .'/'. date("Y") .' '. date("h") .':'. date("i") .':'. date("s") .' '. date("A") .'';
		$message .= 'File: '. __FILE__ .'';
		$message .= 'Line: '. __LINE__ .'';
		$message .= '';
 
		switch ($errno) {
			case E_USER_ERROR:
				if ($errstr == "(SQL)") {
					// handling an sql error
					$message .= "<b>SQL Error</b> [$errno] " . SQLMESSAGE . "";
					$message .= "Query : " . SQLQUERY . "";
					$message .= "On line " . SQLERRORLINE . " in file " . SQLERRORFILE . " ";
					$message .= ", PHP " . PHP_VERSION . " (" . PHP_OS . ")";
					$message .= "Aborting...";
				} else {
					$message .= "<b>My ERROR</b> [$errno] $errstr";
					$message .= "  Fatal error on line $errline in file $errfile";
					$message .= ", PHP " . PHP_VERSION . " (" . PHP_OS . ")";
					$message .= "Aborting...";
				}
				exit(1);
				break;
 
			case E_USER_WARNING:
				$message .= "<b>My WARNING</b> [$errno] $errstr";
				break;
 
			case E_USER_NOTICE:
				$message .= "<b>My NOTICE</b> [$errno] $errstr";
				break;
 
			default:
				$message .= "Unknown error type: [$errno] $errstr";
				break;
		}
 
		$to = 'name@domain-name.com';
		$subject = 'WEBSITE ERROR: Name of project/web-site';
		$headers =	"Date: " . date("r", time()) . EmailNewLine .
					"From: website_error@smg.com.au" . EmailNewLine .
					"Content-type: text/html; charset=iso-8859-1 ";
		mail($to, $subject, $message, $headers);
 
		// if $debug is equal to true then show the full error on the website, when you
		// make the website live make sure you set $debug equal to false
		if($debug == true) {
			echo '<span class="error_message">'. $message .'</span>';
		}
 
		return true;
	}
 
?>
 

If you want users to download files off your site but you don't want to expose the links of the files on the server, the script below in PHP is an excellent way to hide your files to your viewers.

Basically the script works by changing your browser's mime type to application/octet-stream, you can then pass the file through this page ($file in the script below), and name the file however you wish the end user to view it as ($filename in the script below).

When the user is downloading the file they see the source of the download as the page rather then the actual file ie: downloadFile.php rather then downloadFile.zip.

< ?php
   ob_start();
   $mm_type="application/octet-stream";
   $file = "downloadFile.zip";
   $filename = "download.zip";
 
   header("Cache-Control: public, must-revalidate");
   header("Pragma: no-cache");
   header("Content-Type: " . $mm_type);
   header("Content-Length: " .(string)(filesize($file)) );
   header('Content-Disposition: attachment;
    filename="'.$filename.'"');
   header("Content-Transfer-Encoding: binary\n");
 
   ob_end_clean();
   readfile($file);
?>

Click to see this script in action.

You can add security to this script to only allow authenticated users and more. If you have any queries or suggestions for this script, please don't hesitate to comment below.