The inspiration for this code was New-Window Links in a Standards-Compliant World. They were using the “rel” attribute and checking it for “external” to update the anchor’s target attribute. I lean towards not opening new windows unless it’s an off-site link to improve the user’s experience. Furthermore, I try to avoid adding more code when I can simply use an element’s existing attributes to serve my needs.
Author Archives: Robert
Table/record sorting made easier for CodeIgniter
In a project I am currently working on, I needed a way to efficiently allow sorting of the records in an admin area while preserving the functionality of a search function (that passes the query in the url) and the pagination library. Initially it started off as a section of each method where sorting was allowed (record lists of things like countries, states, etc). My sorting routines finally ended up being put into their own helper.
Less 404 errors by adding robots.txt to your site
Even if you don’t think you need it, it’s still good practice to provide a “robots.txt” file in the root directory of your site for search engine spiders to find. Not only will it remove the 404s from your error_log (happens every time a spider/bot looks for it and it doesn’t exist), but it also provides a quick and efficient way to block certain sections of your site from being indexed. This is by far a better method than adding rel=”nofollow” to your links or the following meta tag to the header of each page in question.
<meta name="robots" content="noindex, nofollow" />
The most basic robots.txt file would include the following. This tells the search engines to index everything it can find.
Rewrite all unresolvable URLs to a script using .htaccess
Another simple one.
The following code checks to see if the request is for a real directory or file and reroutes the request to “/myscript.php” if not.
<IfModule mod_rewrite.c> RewriteEngine On RewriteBase / RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)$ myscript.php?/$1 [L] </IfModule>
Take note of the the excamation point (it means “not”). To do the opposite (reroute valid directory/file requests through a script) just change the rewrite conditions (RewriteCond) to…
Force “www” subdomain using .htaccess
Short and sweet.
<IfModule mod_rewrite.c> RewriteEngine On RewriteCond %{HTTP_HOST} !^www\. RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L] </IfModule>
And if you want to remove the “www” portion, just change the condition (RewriteCond) and rule (RewriteRule)…
You may ask “Why should I do this?” and the asnwers are simple. It promotes linking uniformity and ensures you don’t end up being negatively affected by having “duplicate content” across (sub)domains in search engine indexes.
Codeigniter Image_lib convert() v1.3 JPG to GIF to PNG
I recently performed some code-fixing for a library extension I found over in CodeIgniter’s forum. The mod provided a convert() method for the Image_lib library. It lets you to convert (duh) the original image from/to JPG (default), GIF or PNG and optionally delete the original image.
When I first added this extension into my CI distribution, everything still worked without throwing errors so I started calling the new function. Then…
Blog Spam Bots… Who Actually Lets These Comments Through?
Just a little rant from my end after going through my blog comment spam list…
I am so sick and tired of automated bots slamming my blog TRYING to get their bogus comments in the promote their sites. I mean seriously, unless you’re an idiot and just auto-approve all comments (in which case you should be flogged repeatedly and be cut off by your ISP), who would approve obvious spam comments?
For any of you that are still manually flagging WordPress comments, you can get Akismet for free for personal use (if you’re running a business blog, be honest and just pay for the software). If you look to the right side of my page, you’ll see the number of comments automatically marked as spam since I installed it on September 9th, 2010.
Unfortunately it makes me realize how unpopular my blog is since 99% of my comments are spam, lol.
Convert extended ASCII in a string using PHP strtr instead of Normalizer or iconv
I am working on a CodeIgniter project that has information stored in the database with extended ascii characters (128-255) and UTF-8 encoding. As most of you know, CodeIgniter is fairly strict on what is allowed in a url (they only allow what SHOULD be permitted per RFC 1738 ;)). Furthermore sunce you can create basically any character by allowing a percent sign (%) in the URL, alowing that was not a viable option for me.
For instance you cannot use “ñ” (n with a tilde) in the URL parameters. Replacing it with “n” for the database query will however return a valid match. Problem solved! I can just use Normalizer or iconv, right?
Codeigniter Core Mod for $_SERVER[‘DOCUMENT_ROOT’] on GoDaddy subdomains with $_SERVER[‘SUBDOMAIN_DOCUMENT_ROOT’]
I was tinkering around with a CodeIgniter project earlier today and at some point, I needed to use PHP’s file_exists() function. I tend to use $_SERVER[‘DOCUMENT_ROOT’] for my starting location for finding files located in the web-viewable areas (makes sense huh?). This makes it easy to get the file url by simply replacing the document_root from the path of the filename which results in a nice absolute path for linking. In this particular instance, I was checking for an image to show in place of a heading tag. Granted the code was simple and straight forward, but it could not get PHP to locate the file and I knew it was there.
MySQL split field values using the SUBSTRING_INDEX() function
Earlier today a client requested I change the output of their lead form to split the “full name” entry into “first name” and “last name”. My initial thought was to use a Regular Expression (RegExp) to match the parts of the string for my UPDATE query…
PHP/MySQL Nested <select> using <optgroup> with LEFT JOIN on same table in a single query.
I looked around on the web for a while trying to find an example of what I was trying to do. Some were close, but none performed the JOIN portion of the query and on the same table from the SELECT portion (self referencing). So, here is the code I used to created a sub-category only dropdown using the parent categories as the optgroup labels.
Minutes to Decimals Conversion Chart
Save time calculating your billable hours by using this handy conversion chart. It was modeled after http://www.p1m.com/unit.htm and placed here to ensure I could locate it at a later date in case it is ever removed from that site.