Weekly Function: gethostbyaddr

This function can be used to retrieve the host name for a particular IP address. For example, if you give it the IP address 74.207.242.169 it will output tinsology.net.

Just off of the top of my head, one potential use for this function might be to check IP logs for content scrapers. Say for example I’m running a bot on my server that is visiting your site, retrieving your content, and then publishing it on my site. If you wrote a script that looked up the host name for each IP address in your access log you would find my domain in the list. From there you can block the IP address.
Continue reading

Weekly Function: ctype_digit

I had actually never heard of this function before my random function grabber spit it out, or for that matter any of the ctype functions. What this function does is takes a value and determines if it consists entirely of digits. At first this sounds a lot like is_int, but there are a couple of differences worth noting:

  • ctype_digit expects its input to a string. It will return false if given a non-string. is_int expects its input to be an int. Anything else, including numeric strings, and it will return false. This makes ctype_digit a little more convenient for form validation since form data is always a string.
  • is_int will return true for hexadecimal values (ex 0x1F), ctype_digit will not.
  • ctype_digit will return false for negative values (basically any character that isn’t a 0-9 will cause the function to return false)

Continue reading

Weekly PHP Function

Starting this week I’m going to be writing a post about a particular PHP function. Each week a function will be chosen at random from php.net’s Function List and I’ll write about (including but not limited to) basic usage, interest ways to use the function, portability issues and alternatives, and caveats.

I’ve written a script that will grab a function and I’m going to try my best to write about the function it spits out, however there are going to be some exceptions to this. For one, if the function is very straight forward or just plain boring I’m going to grab another function. For instance, no one wants to read a post about the ceil function. It rounds up. End of post.
Continue reading

PHP Donts

The following is a non-comprehensive list (in no particular order) of things I often see people doing while using PHP that they probably shouldn’t. The purpose of this list is to inform; hopefully it will shed some light on what some people may be doing wrong and what they can do better. I doubt there is a PHP programmer out there that hasn’t been guilty of some of these things at some point, so if you find that you are doing several or even all of these things it is nothing to be ashamed of.

Continue reading

PHP json_encode and json_decode Alternatives

The number of APIs and other remote services that either support or require json as a means for transferring data is growing. However, PHP does not have guaranteed support for json encoding or decoding functions until version 5.2, which means if you want your application to be as portable as possible you’ll need to find an alternative. The following functions should provide that. Note that I haven’t exhaustively tested either of these functions and they may not completely replicate all of the functionality of their built in counterparts, but they should be sufficient for basic usage:
Continue reading

Fetching Remote Content in PHP

Reading in remote content with PHP can be an incredibly simple task:

$url = 'http://example.com/foo.php?bar=1';
$remote_content = file_get_contents($url);

One problem with the above solution is that it requires allow_url_fopen to be enabled in your php.ini. If you’re writing a portable application that depends on being able to fetch remote content you probably don’t want to tell your users to modify their php.ini. Doing so would mean turning away users who don’t have access to their php.ini (as is the case with some shared hosts). Ideally you would want a solution that offers some redundancy.
Continue reading

Managing URL Parameters in PHP

This is a function that I find myself using more and more often to create or modify URLs that contain a query string:

function build_query($params = array(), $keep_get = true, $unset = null)
{
	if($keep_get)
	{
		$params = array_merge($_GET, $params);
		if(is_array($unset))
		{
			foreach($unset as $key)
				unset($params[$key]);
		}
	}
	return http_build_query($params);
}

Continue reading

Doing Things the Hard Way

Every now and then I discover something that makes me feel like I’ve been doing things the hard way. The feeling is an interesting combination of excitement and embarrassment. I experienced this today while browsing PHP documentation. I discovered two function that will make my life a lot easier in the future: debug_backtrace and debug_print_backtrace. As someone who has written his fair share of Java, I’m very accustomed to stack traces. The fact I’ve made it as far as I have with PHP without them leaves me feeling amazed. The fact that I’ve had access to them all along leaves me feeling ashamed.

I guess you learn something new everyday.

Apparatus 0.4

A new version of Apparatus is available. Previously I wrote about several new features that would be included in this version, however rather than further postpone a release, I’ve decided to hold off on those features until the next version. Version 0.4 incorporates a new theme, the latest version of tab override (which should resolve any browser compatibility issues), and various improvements and modifications.