Function: is_admin();
I recently got this all polished up, and it's working so well I decided I had to share. This little gem allows me to completely do away with setting up a dev server to make changes on a live site, as the functionality is basically created on an as-needed basis. It's easy, but oh so useful:
| <?php | |
| // I put this in my session class, as it's guaranteed to be called on each and every page | |
| function is_admin() { | |
| $ip = array( | |
| file_get_contents($_SERVER['DOCUMENT_ROOT'].'/secret/text/file.txt'), | |
| '123.45.678.90', | |
| ); | |
| return (in_array($_SERVER['REMOTE_ADDR'], $ip)); | |
| } | |
| ?> |
So, what does it do? Simple. It checks to see if the IP address of the computer making the request is the same as the predefined IP address it's got in 1) the hard-coded array and 2) the /secret/text/file.txt I've set up elsewhere on the site. What text file? Oh, yes, almost forgot.
This function worked great for a while, but one client in particular had an IP address that changed fairly rapidly. Every other week I'd get a request to change the embedded IP, and eventually I just got sick of it. The solution was the following:
| <?php | |
| // Save as /super/secret/ip/reset.php, or whatever suits your fancy. | |
| $ip = $_SERVER['REMOTE_ADDR']; | |
| $filename = 'ip.txt'; | |
| if (is_writable($filename)) { | |
| if (!$handle = fopen($filename, 'w')) { | |
| echo "Cannot open file ($filename)"; | |
| exit; | |
| } | |
| if (fwrite($handle, $ip) === FALSE) { | |
| echo "Cannot write to file ($filename)"; | |
| exit; | |
| } | |
| fclose($handle); | |
| header('Location: /'); | |
| die(); | |
| } else { | |
| echo "The file $filename is not writable"; | |
| } | |
| ?> |
Now control was back in the clients' hands. Each time their IP changes, they just click a bookmark that accesses the above script. It opens the aforementioned text file, grabs the current IP address, and writes a single line of text. This is followed immediately by a redirect to home (/), but you could just as easily have it point anywhere.
So, now the function works. How do you use it? The short answer would be "everywhere" -- at least, that's been my experience:
| <? if (is_admin()) { ?> | |
| <p>New Content</p> | |
| <? echo '<p>Why not some PHP as well?</p>'; ?> | |
| <? } else { ?> | |
| <p>Old Content</p> | |
| <? } ?> |
This can be used anywhere you want to make changes to a site, but leave everything completely intact for anyone that's not on your VIP list. I've managed to complete some pretty extensive modifications by simply wrapping everything I was working on in this function. Clients love it because they can see progress and know that customers aren't seeing development in progress. As for the amount of time I've saved by not hassling with copying files, making changes, reuploading, renaming, etc., well, it's safe to say that I'm never going back to the old ways.
Utilities section online
My utilities section is now up and running. For the first round of files, I've got Dreamweaver Sitepeek, the Javascript Linkerizer, and a whole slew of Greasemonkey scripts. Oh, and a php date reference (pdf) I made a few days ago. Check it out.
New Theme! Hooray!
Okay, I found a great theme called Silhouette by a guy named Brian Gardner, who graciously provided it free of charge. It's going to take some gentle coaxing (read: hammer and axe) to get it to do exactly what I want, but for the first time in a long while, I think I've got a theme I can live with for more than a week.
As I'm sure you'll figure out if you poke around a bit, there is much that is broken around these parts. I'll be fixing them up as time allows, and possibly documenting my progress as well.
