WAMP: Standard installation issues

I just installed a WAMP environment (Windows Apache Mysql PHP) on my computer as I haven’t really been writing any PHP-code recently and I felt like doing it again. I had forgotten the initial issues one might have when installing WAMP and so I though I’d just write down two typical problems you might have when installing your WAMP environment:

Forbidden You don’t have permission to access /phpmyadmin/ on this server.

This problem shows up when your phpmyadmin settings are set to deny all connections. Go to C:\wamp\alias and open phpmyadmin.conf and you’ll see the following section:

<Directory "c:/wamp/apps/phpmyadmin3.5.1/">
    Options Indexes FollowSymLinks MultiViews
    AllowOverride all
        Order Deny,Allow
    Deny from all
    Allow from 127.0.0.1
</Directory>

change it to:

<Directory "c:/wamp/apps/phpmyadmin3.5.1/">
    Options Indexes FollowSymLinks MultiViews
    AllowOverride all
        Order Allow,Deny
    Allow from all
</Directory>

Restart WAMP.

You might also get the following problem:

Forbidden You don’t have permission to access / on this server.

If you get this problem you might’ve tried to access ‘localhost’ in your web browser. Try to access ‘127.0.0.1’ instead. If you can access 127.0.0.1 but not localhost you need to edit your httpd.conf in your Appache installtion.
Open the httpd.conf and find the following section:


#
# Listen: Allows you to bind Apache to specific IP addresses and/or
# ports, instead of the default. See also the
# directive.
#
# Change this to Listen on specific IP addresses as shown below to
# prevent Apache from glomming onto all bound IP addresses.
#
#Listen 12.34.56.78:80
Listen 80

change it to:


#
# Listen: Allows you to bind Apache to specific IP addresses and/or
# ports, instead of the default. See also the
# directive.
#
# Change this to Listen on specific IP addresses as shown below to
# prevent Apache from glomming onto all bound IP addresses.
#
#Listen 12.34.56.78:80
Listen 0.0.0.0:80

Restart WAMP.

PHP: Enable CURL in WAMP environment

So I needed to enable Curl to run a PHP script today and my WAMP installation didn’t have CURL enabled “out-of-the-box”. This was however easily fixed by following these steps:

  1. Close WAMP
  2. Go to your WAMP installation directory and enter bin\php\php[VERSION] (normally this will be C:\wamp\bin\php\php[VERSION])
  3. Open php.ini in your favorite editor
  4. Search for curl and you’ll find a line similar to “;extension=php_curl.dll“, uncomment it (remove the semi-colon)
  5. Save and close the file
  6. Go to your WAMP installation directory and enter bin\apache\apache[VERSION]\bin\ (normally this will be C:\wamp\bin\apache\apache[VERSION]\bin)
  7. Open php.ini in your favorite editor
  8. Search for curl and you’ll find a line similar to “;extension=php_curl.dll“, uncomment it (remove the semi-colon)
  9. Save and close the file
  10. Restart WAMP

 

To fake a PHP enum

Today I was sort of in a need of an enum in PHP. This, of course, doesn’t exist so I had to make the best of what I had. I wanted to both use an enum as a way to organize digits, but also to use the literal form of it.
One way to do it is to define string constants, like this:

<?php 
	define("MY_CONSTANT", 0);
?>

But I didn’t want that, because that can clutter your code up pretty badly. So while googling this issue I saw some interesting solutions which I mixed together. This example isn’t excellent in that it uses swedish letters in the code (oh the horror), but it does actually work and in this particular situation it was just what I needed:

<?php 
	/**
	 *	A class representing various swedish types of jewelry
	 */
	class Items
	{
		const Halsband = 0;
		const Armband = 1;
		const Örhänge = 2;
		const Barnsmycke = 3;
		const Övrigt = 4;
 
		/**
		 *	A function that returns the name of an Items constant 
		 *
		 *	@param 		int 	$const	A constant in the Items class 
		 *	@returns	string			The name of the constant
		 */
		public static function getName($const)
		{
			$itemClass = new ReflectionClass("Items");
			$constants = $itemClass->getConstants();
 
			foreach($constants as $name => $value)
			{
				if($value == $const)
					return $name;
			}
 
			//Nothing was found
			return -1;
		}
	}
?>

This can be used like this:

<?php 
$item = Items::getName(Items::Halsband); //$item will now hold the string 'Halsband'
?>

Intercepting all errors in PHP

Sometimes you get annoying errors in PHP that refer to the server error log. If you do not have access to the log, because your pretentious web hosting company does not allow you to access them, you need to catch any errors before they end up in the error log. Therefore, there is the amazing feature, set_error_handler , that allows you to intercept errors halfway to hell and manage them yourself.

Here is an example of a simple error handling:

<?php 
	function simpleErrorHandler($errno, $errstr, $errfile, $errline)
	{
		if (!(error_reporting() & $errno)) 
		{
			// This error code is not included in error_reporting
			return;
		}
 
		switch ($errno) 
		{
			case E_USER_ERROR:
				echo "<b>User error:</b> [$errno] '$errstr' - Error line $errline in file $errfile\n";
				exit(1);
				break;
 
			case E_USER_WARNING:
				echo "<b>User warning:</b> [$errno] '$errstr'\n";
				break;
 
			case E_USER_NOTICE:
				echo "<b>User notice:</b> [$errno] $errstr\n";
				break;
 
			default:
				echo "<b>Unknown error:</b> [$errno] $errstr\n";
				break;
		}
 
		/* Don't execute PHP internal error handler */
		return true;
	}
 
	$errorHandler = set_error_handler("simpleErrorHandler");
?>

Obtaining the current file name

For some reason I just needed to get the name of the file I was currently in. So, here is a small function I cooked up which returns the name of the file you are in:

<?php 
	/**
	 *	Returns the name of the file of execution
	 *
	 *	@param	bool	$baseFile 	If false, this function will return the name of the file this function resides in, if true, the current file of execution
	 */
	function getCurrentFileName($baseFile = false)
	{
		if($baseFile)
		{
			return basename(__FILE__);
		}
		else
		{
			return basename($_SERVER['SCRIPT_NAME']);
		}
	}
?>

This function takes in a parameter (optional) which, if false, will take in to account whether or not the function will account for imports or not. Sometimes the function being called is not located in the file that’s being executed.