Creating a simple debug() function

If you are ever like me and hate having to type print_r and echo pre blocks whenever you want to debug a variable or array, this function is for you! This is a global function that can be used to output any data that you need to debug and it comes equipped with some nifty features. For one thing, the debug() will not output any data on the website if error_reporting is turned off, which is good for instances where you forget to remove the debug() code! It will also display the file and line number where the debug() was called, and you can pass true as the second argument to do a var_dump instead of print_r. Here's the code for your enjoyment!

/**
 * Outputs/Debugs a variable and shows where it was called from
 *
 * @param mixed $var
 * @param boolean $dump
 * @param boolean $backtrace
 * @return string
 */
public function debug($var, $dump = false, $backtrace = true) {
	if (error_reporting() > 0) {
		if ($backtrace) {
			$calledFrom = debug_backtrace();
			echo '<strong>' . trim(str_replace($_SERVER['DOCUMENT_ROOT'], '', $calledFrom[0]['file'])) . '</strong> (line <strong>' . $calledFrom[0]['line'] . '</strong>)';
		}
		echo '<pre class="debug">';
		$function = ($dump) ? 'var_dump' : 'print_r';
		$function($var);
		echo '</pre>';
	}
}

Please note that this is a very basic debug function with very basic backtrace functionality. Here are a few examples on how to use the function (if for some reason you don't understand it).

debug($_SERVER);
// var_dump() instead of print_r()
debug($_SERVER, true);
// Do not display the backtrace
debug($_SERVER, false, false);

Two new scripts, Resession and Decoda

It has been a while since I released some of my scripts, but now I have two to reveal! Well actually the Resession script has been up for nearly 2 months now, but I'm finally getting around to announcing. And without further ado, I give you Resession (Session Manager) and Decoda (A BBcode style parser).

Decoda

Decoda is a lightweight class that extracts and parses a custom markup language; based on the concept of BB code. Decoda supports all the basic HTML tags and manages special features for making links and emails auto-clickable, using shorthand emails and links, and finally allowing the user to add their own code tags.

Download the latest versions of Decoda

Resession

A small lightweight script that can manage and manipulate Session data. Calls session_start() in memory so that no header errors are thrown, as well as stores the session id in the object.

Download the latest version of Resession

If you have any suggestions for either of these classes, please feel free to comment this post or send me an email!

XAMPP in Windows: Setting up vHosts

If you have ever used a local server, you would immediately realize that you can only develop one site at a time using the given root. You could however setup multiple folders for different websites within your local servers root, but the problem with that is they would all share the same root, big problem! To fix this you would have to set up virtual hosts, or vhosts for short. A vhost is a small server side trick (using Apache) that allows for multiple domains (websites) under the same root or IP address.

To get this working in XAMPP, we will need to edit our hosts file and the httpd-vhosts.conf file within your XAMPP setup. But before we begin, lets set up a quick example of what we will be trying to achieve. We will want to setup two websites within the htdocs folder, we will create 2 folders with the first one "site1" and the second "site2" (you can name these whatever you wish).

The first thing we need to do is open the Windows hosts with Notepad, which can be located at C:/WINDOWS/system32/drivers/etc. If you are using Windows Vista, you will need to right click on Notepad and click "Run as Administrator" to be able to open the hosts file. Once you have opened the hosts file, add the following 2 lines to the file and save it.

127.0.0.1		site1
127.0.0.1		site2

Secondly we will need to open the vhosts file located at C:/xampp/apache/conf/extra/httpd-vhosts.conf and add the following code. Copy and paste this code for each additional vhost you wish to setup.

# Uncomment the following
NameVirtualHost *:80 
<VirtualHost *:80>
	ServerAdmin webmaster@localhost
	DocumentRoot C:/xampp/htdocs/site1
	ServerName site1
	<Directory "C:/xampp/htdocs/site1">
		Options Indexes FollowSymLinks Includes ExecCGI
		AllowOverride All
		Order allow,deny
		Allow from all
	</Directory>
</VirtualHost>
<VirtualHost *:80>
	ServerAdmin webmaster@localhost
	DocumentRoot C:/xampp/htdocs/site2
	ServerName site2
	<Directory "C:/xampp/htdocs/site2">
		Options Indexes FollowSymLinks Includes ExecCGI
		AllowOverride All
		Order allow,deny
		Allow from all
	</Directory>
</VirtualHost>

Once you have completed both these tasks you must restart Apache. Once restarted, you can go to http://site1/ and all files and folders within "site1" should be displayed. You may do this for as many websites as you wish, just make sure to complete both tasks!

Fresh websites, Fresh updates

Well its been a while since I have posted anything useful, and I'm sorry for the lack of activity. I actually have many post ideas up on my whiteboard, its just that I have been extremely busy lately. Tomorrow I will be going on a week long cruise to Canada, so I have been preparing for that all week. Besides preparing for my trip, I have been working on two of my own websites, I will review them both below. Both of which are CakePHP applications... for your information, hah.

GameSync

http://www.gamesync.com

This is a personal project I started on over a year ago. I wanted to create a online interactive social community that is geared towards gaming and the competitive scene. It has many of the features a social community has, and on top of that has a fully robust gaming and activity system. Its something you need to try out for yourself, so please sign up! I am also looking for help (volunteered would be best) on this site, as well as searching for investors.

Starcraft 2 Armory

http://www.sc2armory.com

About 2 years ago I launched a fansite for the game Starcraft 2. Since then it has grown tremendously (just hit 10 million views!) and I recently decided to redesign it. In that process, I also decided to rebuild the whole system in CakePHP from the ground up, check it out! Oh and by the way, I am the #1 unofficial fansite for Starcraft 2... of course!

Anywho, once I get back from my vacation, I promise to put out more posts then I am now. With 2 websites out of the way, I will have so much free time. Hats off to that!

Fixing a weird cache issue in Firefox

About two weeks ago my Firefox started acting up and would not cache pages correctly. Actually, the content would cache, but any image on the actual page would load 1 by 1 and I can watch it go in sequence. This was extremely annoying, especially working in PhpMyAdmin, watching each image icon load 1 by 1 until the page was complete. This process would take nearly 5x what it usually did, so it got quite annoying real fast. I wasn't sure what caused this to happen as all my settings were normal and untouched.

So once I had enough of this, I checked Firefoxs core config (by typing about:config in the address bar). I then typed "cache" in the filter field and once it populated, I noticed the following fields were set to false (turned off).

browser.cache.disk.enable
browser.cache.memory.enable

Once I reset these to defaults (enabling it to true), my weird image cache problem vanished. I am not entirely sure if messing with these settings are ok, so if anyone has any input on the subject it would be appreciated. But besides that, everything seems to be running smoothly, and I thought it would be a good idea to let others know in case they run into it as well.