PHP, Python, GoLang, and Node.js all served by Apache port 80

I dabble in node.js, php, python, and more. Often, these services will produce webservers, and I’d like to try them out in my browser. I also would like to be able to test redirects for things like oauth. In order to get a proper passback from somewhere like Facebook, I need to simulate a full website url without port. It also would be nice to do this without having to shut down one, adjust settings, and restart the other.

The concept:
/etc/hosts or Windows/system32/drivers/hosts will handle incoming request handling to a url

  • Apache handles all requests from port 80
  • Apache uses VirtualHosts to redirect traffic from port 80 to various ports
  • Node/Python/Go/Etc. Will handle requests from specific ports.

An example:

This tells your computer that every program that requests data from should be forwarded to your computer. This is true for all protocols, http, https, ftp, etc.


<VirtualHost *:80>
    ProxyPass / retry=0

We’ve told apache to create a VirtualHost on port 80. The servername matches the entry in /etc/hosts. The proxypass indicates to where we should forward this request, in this case, to local port 3000. retry=0 tells apache not to cache the results and to always try to serve the page (even if node was down last time)


https.createServer(options, function (req, res) {
  res.end("hello world\n");

This is a basic node server using port 3000. If we receive a request to we should see it handled by node.

Similar servers can be set up in Python, GoLang, etc. And as long as they don’t steal port 80 from Apache, they can handle requests as normal. I’ve found this very useful.

Determine Goal Width and Height in Image Resizing

I find myself doing image manipulation on a regular basis. One of the common things I have to do is to fit an image into a max width or height. I’ve distilled this as best I can, and I think this is as simple as it gets. It’s in PHP, but the logic should be applicable to any language.

$ratio = $image_width/$image_height;
	$goal_width = $max_size;
	$goal_height = $max_size/$ratio;
	$goal_width = $max_size*$ratio;
	$goal_height = $max_size;

Coding for Understandability

Often I have a number of options in how to code a particular piece of functionality. I’m constantly balancing between brevity, simplicity, speed, size, and readability. I often find that if I put a little more focus on simplicity and readability, I save myself time and headaches, in addition to making my code more readable, more object oriented, and more accessible to other coders.

I’m going to provide some examples in ECMAScript, but I know that similar problems exist in other languages, as well.

I recently ran across some code that was coded for brevity:

var userdefined:Object = (_model.partner || {}).userdefined || {},
bsb:BannerShownBeacon = new BannerShownBeacon(
		? userdefined.dims
		: userdefined.width && userdefined.height
			? userdefined.width + 'x' + userdefined.height
			: 'fixed_bottom'

Now, this is fully valid, well-written code, and it’s also tabbed and line-broken for readability, but what exactly is it doing? What is the purpose of this code?

The difficulty I find is that when someone is trying to determine whether their particular implementation is going to be executed — lets say we have dims, width, and height in our userdefined param, and we’re looking at this the first time. Since coding standards aren’t set for how to structure ? : style if statements, it’s difficult to go through this by instinct and take a guess at what it’s doing. Every language has if/else statements and most coders are intimately familiar with them.

First, we look to see if _model.partner exists and if it doesn’t, we create it, then we check to see if _model.partner.userdefined exists and if not, we create it and assign that to a variable.
Then, we create a second variable called bsb that expects a BannerShownBeacon;
If userdefined has a dims param, we use it, otherwise, we check if it has width and height params, and if it does, we combine them with an x in between, otherwise, we just use the term “fixed_bottom” and make a BannerShownBeacon out of that.

The final option is to include large amounts of comments (which is excellent for non-compiled languages like JS), but in compiled languages, this often doesn’t save any time or lines of code.

I’ve rewritten this here using if/else statements

var bsbText:String = "fixed_bottom";
		var userdefined:Object = _model.partner.userdefined;
			bsbText = userdefined.dims;
		else if(userdefined.hasOwnProperty("width") && userdefined.hasOwnProperty("height"))
			bsbText = userdefined.width + "x" + userdefined.height;
var bsb:BannerShownBeacon = new BannerShownBeacon(bsbText);

Measuring PHP Page Load Time

Note: This is for PHP 5.0 and higher.
I’ve seen a bunch of these out there, but I don’t see why they should be so complex. Here’s the code for the top of your page
$start_time = microtime(true);

Then, at the bottom, put this:
$load_time = microtime(true) - $start_time;

Microtime produces a number that can be used to measure milliseconds. by including true, we request a number instead of a string like “ms sec”, which isn’t what we want.

The $load_time var contains a float (number) that indicates how long the page took to load. Use this as you see fit.

PHP DateTime and NOW() vs UTC_TIMESTAMP()

In a previous post, I complained about the lack of connection of dates from AS3 to PHP. This is partially due to incompatible formats, but also may have had something to do with server vs. local time.

AS3 always returns the local time of the user. PHP always returns the local time of the server. This can cause all sorts of problems when using mysql to bridge the two. The obvious solutions are to have your computers in the same time zone OR to use unix epoch time.

I’ve always used NOW() as a mysql system to add the date, but this provides local server time.

UTC_TIMESTAMP() instead provides the GMT time, which is what Unix epoch time is based upon. This should be more consistent and more easily converted.

Keep in mind, however, that storing a unix timestamp in an int field is still a bit more simple to understand, easier to sort, and just as easy to convert.

Remove Unnecessary PHP GET Variables

When building PHP forms, I often use fields to produce my get vars. Some forms don’t need all of the fields filled in, and when that happens, you get a lot of empty variables that don’t mean anything. If you’re providing a URL that people will want to copy and paste, removing those extra variables can make your URLs look a LOT cleaner. Here’s a really simple way to do that in PHP.

I’ve also included an example of how to remove default vars as well. I’ve assumed you have a GET var called myvar and the defaultvalue is “default”

//First, we get the main URL of the page, without the get vars
$mainurl = "http://".$_SERVER['SERVER_NAME'];
//set up an empty var for vars
//we assume no changes need to be made until we see any
//for each get var
foreach($_GET AS $key => $value)
	//if it's not something we've set as a default and it's not empty
		//if it's the first one
		if($vars == "")
			$vars .= "?";
			$vars .= "&";
		//attach the var to the end again
		//if something existed before and is left out now, 
		//we mark this as changed and reload the page with 
		//the new headers
	//reload the page with the new location, nice and clean. 
	header("Location: " . $mainurl.$vars);
	//include the main file

‘$var’ won’t resolve in single quotes

Ahh the little nuances of PHP. The more I use it, the more I wonder who decided some of these “features” were a good idea.
Usually you can evaluate variables in your strings in the following way:

$years = 15;
echo "I am $years years old.";
//produces: I am 15 years old.

but the moment you do this, it fails:

$years = 15;
echo 'I am $years years old.';
//produces: I am $years years old.

See the difference? One uses ‘ and one uses ” to create the string.

My friend Steve just informed me that this also does fun things with escape characters like
“\n” and ‘\n’
but apparently if you don’t need the extra happy usefulness of double quotes, single quotes are faster.