Start Services Using a Batch File

If you use your person computer as a testing server you probably have several services running in the background (a web server, database, caching service, etc). Individually these services tend to have a small footprint. If you’re running enough of them however, they can start to impact your machine’s performance. If you have them set to auto start they can also increase your startup time. Ideally you would want to start them when you need them and stop them when you’re done with them, but that can be a hassle for multiple services. A simple solution is to create a batch file to start all of your services, and another one to stop them.

Follow these steps to create your batch file:
Continue reading

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

Google is translating my code!

Recently I added the ‘Google Translate’ widget to my sidebar. After some testing I noticed something problematic: All of the code snippets in my posts were also being translated. Unfortunately the PHP interpreter does not speak Spanish (it’s not that kind of interpreter apparently). Fortunately there is a simple solution. The content of any HTML element with the class ‘notranslate’ will not be translated; all you have to do is add the class ‘notranslate’ to the element containing your code.

If you’re using a plugin to format and display your code this may not be so simple. Currently I’m using SyntaxHighlighter Evolved which has a convenient field in the settings page for adding addition classes to the ‘pre’ element used to display code. If this isn’t the case for you you’ll have to do some digging in the plugin code. Alternatively you could use javascript to add the class to a particular element. This way you won’t have to make any modifications to core plugin files, which will be overwritten if you decide to update.

Here is a 100% untested example (using jQuery):

$("pre").addClass("notranslate");

The above code should prevent any content within a ‘pre’ element from being translated by Google.

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

NoSQL

Recently I’ve been working with a non-relational, graph DBMS called Neo4j. I’ve really only scratched the surface and it might just be a the temporary euphoria of working with something so new, but it feels liberating to be able to approach problems in a new way. It might also be that for the first time in a long time I’m working in Java. At this point I’ve written far more PHP code than Java, but I still feel like Java is my native language.

I think anyone who works with database driven applications should at least try some form of non-relational database, if only just to see things from another perspective.