Get the last created auto-increment ID from a MySQL table
April 9th, 2009If you’re like me then you’re developing like crazy. Once in a while you may need to retrieve the auto-increment ID value from the last created row you inserted into a particular mysql table. Yea… when you insert a new row into a mysql table there is no apparent way to just return the ID from the row’s auto-increment field. So to do this using PHP, simply use the
functin, which will return the last created ID as an interger.
The only catch is that you will want to run this function RIGHT after you do your insert, else some other process might create a new row after yours which would prevent you from obtaining the correct ID.
Anyways, short and sweet but saved a bunch of time on my end!
How to Eliminate Spam with SPF Records
March 4th, 2009I really hate spam. I’m sure just about everyone does. I really hate it though when spammers forge my own email address as the sender, then I mark it as spam, then I inadvertantly end up having legit emails dumping into my spam box! Ouch, talked about a breakdown in communication.
One way to solve the issue is to setup what’s called an “SPF” record (Sender Policy Framework). An SPF record is simply a line in your domain’s zone file (each domain name has a zone file on it’s primary DNS server that tells the world where to find IP’s who serve that domain) that specifies which IP is allowed to send mail FROM your domain.
For example, adding this line to your zone file:
domain.com IN TXT v=spf1 a mx ip4:<IP_ADDRESS> ~all
will tell your server, and other third party servers, to reject mail that contains domain.com as the sender but also is NOT coming from <IP_ADDRESS>. So this means if Joe spammer decides to send a messages to a Yahoo Mail address, but forges the email to look as if it came from you, and Yahoo checks for SPF records, then the spammer will not succeed.
Obviously, the down side to SPF is that
- not everyone implements SPF checks
- if Yahoo or another mail provider does check SPF records and your domain doesn’t have an SPF record in place, you might be at risk of being denied sending mail to their servers even though you are legit.
But really, SPF records, I love it. No more forging from spammers. Simple. Now everyone should do it.
Valentines Day Dance
February 14th, 2009My girl friend sent me this dancing video for valentines day, so good!
Mac vs PC, OS X vs Windows and why OS X just Rocks
February 12th, 2009I grew up using wordstar on dos as a kid. I moved into windows 3.1, 95, 98, ME (very short lived), XP (and not Vista) and finally dabbled in linux with really, truly reaching that developer Zen. I always really liked how windows was focused on the user GUI and how linux was very developer friendly - but Apple was always that weird proprietary OS with kind of a nice GUI (like windows) but was not at all developer friendly like linux… so there was never a reason to use it.
that is until Apple switched to a UNIX core, which raised a few eyebrows:
![]() |
![]() |
A leap of faith (into OS X)
…so about 3 years ago I decided to take a leap of faith and try out the new macbook pro once I found out Apple was switching from their powerPC based architecture to the intel chips (a very smart move by Apple in my opinion) - besides, those powerPC chips just sucked anyway!
When I first started using my macbook pro I noticed how incredible intuitive, light weight and efficient the operating system was. I was a bit frustrated though because many of the applications I’d used for almost a decade on my windows machine did not have a direct replacement on OS X (not at that time anyway). So I had to stick with my windows and linux dabbled for a littl while.
What OS X did for me
Over time though I became a much better developer beacuse I started using the built in terminal app on OS X and found that editing files and code was just soooo much more efficient and easier than having to deal with the clunky and slow dreamweaver or other windows based editors. For example
- With the vi editor I could open up any file, no BS extra characters, and know exactly what I was going to get once I saved it (I could even rename a file without wondering if my right click->rename operation in windows really did make the change I wanted).
- I could search through the file system with just a few keystrokes, for any given string, even leaving out certain files or parts of the file system when executed.
- I could log into remote computers using similar operating system (linux, unix) and work without needing to learn a new OS interface
- I can run windows XP, windows Vista, Linux, etc. all in virtual contains on my OS X machine which lets me view website’s in all the different browsers before sending to clients for approval
- OS X is supper fast, compared to windows
- No annoying “your hardware is installed”, “your computer may be unprotected”, or “”
- No viruses like windows - sorry windows lovers, the UNIX core actually has a permissions architecture that prevents against those retarded trojans many consumers fall victim to on Mircosoft’s hunk of junk OS.
- OS X has a built in search feature call “Spotlight” that is light years ahead of windows search (even on Vista)
- OS X default browser doesn’t suck (yea safari is fast and doesn’t take up a heap of memory when you run it which means you can have more tabs and windows open without computer slow down)
- OS X comes with almost no maintenance - no drivers and extra crap to intsall to get your devices and apps working.
If you’re a developer I highly recommend trying the Apple OS X over any other system. They may seem a bit expensive but you really do get what you pay for in terms of quality (my macbook pro is still going strong after 3 years) - and if you are actually a developer then you will probably afford it just fine, else your employer is ripping you off and you’re just dumb working for peanuts.
In my opinion you get the best graphical GUI on the market for the least amount of maintenance which means you can make more money in less amout of time - damn I love being smart!
But wait, I’m sure there’s an ignorant, opinionated linux geek or windows liberal (I’ve worked with both) reading my post - so this is for you kids: you’re right, Apple’s OS X isn’t perfect. Windows is better suited for some things, like uhh, micosoft programs such as word docs, spreadsheets and power point presentations (I still use these too!). And linux is better suited for customizing your setup to the max, besides it’s free (OS X is not). But come on get a grip and just admit that…
OS X rocks! You know you want to try it.
Searching for a string in a gz compressed file
February 10th, 2009Gzip is a compression technology available on most unix/linux systems. Once in a while you might see a file such as filename.gz or filename.tar.gz and you may need to search through that file… I know some popular web server programs, such as apache, compress old log files to save space…
If you need to search through a .gz file, just use the zcat command (similar to cat):
zcat file.gz
You could even zcat the file then grep it’s contents or you could head or tail the file to see the start or end, respectively. Pretty cool, now you don’t have to do a gunzip or tar xvfz.
zcat file.gz | grep ’some string’
or
zcat file.gz | head ’some string’
One note would be that greping the contents of a zcat will take some time, where using head or tail seems to execute a little faster.
Linux: change a large number of file names, all at once
February 5th, 2009How do I change a lot of file names all at once, and using the linux command line? Look no further. Let’s say you have a bunch of images in your current directory (let’s create them like so for testing purposes):
for x in {1..100}; do touch “image$x.gif”; done
Now that you have all 100 gif images files, just run the following command to change them to have the .jpg file extension:
for x in *.gif; do mv “$x” “${x%.gif}.jpg”; done
And presto, there you go! Your .gif files are now .jpg files.
The Semantic Web, Oh So Cool
January 28th, 2009I’ve recently taken an interest in the semantic web because I keep hearing about how it will revolutionize the word wide web. After doing a bit of research I’m certain it will.
Right now the web is designed for humans to read web sites and understand the information contained therin. But the web isn’t made for a computer to visit a web page and understand what the information is about, I’ve come to realize the semantic web is the concept where inteligent bots are able to roam around and understand the meaning of data, not just fetch and process data based on pre-programmed instructions.
So really this post is just a mind dump on the exiting possibilities of the semantic web and it’s making me want to start looking into ways to capitalize on this concept. I really like how stuff work’s article on the semantic web… it explains how a subject is related to an object and how the connection is given meaning through something called a “namespace”.

Anyway, not enough time to spend hours on it right now but a very cool concept worth more posts in the future…
What is the difference between GET and POST?
November 28th, 2008I’ve actually been asked this question in interviews before and knowing when to use either GET or POST is a fundamental tool for web developers.
The difference bewteen GET and POST lies in something called “idempotence” - GET requests are said to be idempotent while POST requests are said to be not idempotent. If you lookup “idempotence” on wikipedia it says “multiple applications of the operation does not change the result”. Lets take an example to understand what that means:
Example of idempotence
Think of it this way, when you request a web page from a web server via your browser, sometimes the web page URL contains a few extra variables in the URL after the ? mark, which usually indicates to the web server that you’re looking to pull some dynamic information based on those variables. Maybe you want to see all results from the month of November, or just results 1 through 10, etc. This kind of request would be a GET request, because no matter how many times you make the request you are not changing data on the web server (you’re not changing the state of the web server’s data) - so a GET request is said to be idempotent.
Example of non-idempotence
Now, lets say you happen to be signing up for a new user account on your favorite social networking site, in that case you would usually need to fill out a POST form that asks for your name, desired username, password, etc. and then you press “submit”. By pressing “submit” you are sending data to the web server that will be stored - you’ve just changed the state of the social networking site, and if you submit another user signup request you would be changing the state once again. So we say that POST requests are not indempotent.
What can you take away from all this?
So if you’re just skimming this article, know that GET requests are idempotent and POST requests are not idempotent. No matter how many times you perform a GET request you will not change the web server’s data, while performing a POST request more than once may very well change data inside the web server.
GET and POST variables using PHP, the lazy way
October 22nd, 2008If you want to catch form variables in $<variable name> format instead of the $_GET['variable name'] or $_POST['variable name'] format, just call import_request_variables() at the start of your script and supply either a ‘p’ or a ‘g’ argument for post or get respectively. Kind of nice for those lazy programmer out there.
For example, if you had an “email” variable submitted from your form and wanted to use $email instead of using $_POST['email'] in the script that gets the submission, or lets say you inherited a script where somebody wrote their code using the lazy format, then import_request_variables will come in handy.
The function’s second, optional parameter also specifies a prefix for your variable. You could also set register_globals = on if you didn’t want to use the import_request_variables function.
Version control with Subversion is Essential!
October 20th, 2008After working with countless files and making changes to those files over the years it gets difficult to remember exactly what you did, say, 2 years and 4 months ago on a specific file. Wouldn’t it be great to know what you did way back then… wouldn’t it be great if you could see what somebody else did way back then so you could continue their work?
Enter subversion - A friend of mine pointed me to it and now I use it everyday because it makes my development so much more efficient than without it. I really can’t imagine developing complex applications or even administering client websites without being able to track my changes. Subversion is installed on most linux distributions and is a pretty popular open source version control program. Subversion or just “svn” is run from the command line.
Why do I think subversion rocks?
- I can see what I did (and what my coworkers did) in the past
- I can better estimate future projects based on how long past projects took me and how many files I had to edit
- I can import past work into current projects instead of writing things from scratch!
- I can create template projects so that when I get a new job I just suck that template code in and make minor modifications
- I can hand off work to learning developers or another developer, track their changes, while I’m out getting another deal, and then finish their work knowing exactly what was done.

