Dec/093
Snippet: Fetch Twitter Feed (With Retweets) in PHP
Here is a PHP that fetches a Twitter stream for a specified user. It also combines retweets with the regular updates. This puts them all in an array to make it easy to output them however you want.
This snippet uses APC to cache the data, but you can use any caching method such as memcached or database. It should be fairly easy to edit in your own caching.
Aug/091
Snippet: Maintain a Single Database Object in PHP 5 Using the Singleton Pattern
When creating a PHP application, it is usually necessary to connect to a database to perform certain tasks. In some cases you only want to open a connection when necessary, but limit it to a single connection. This way you don't waste resources on unnecessary database connections. For these situations I use the Singleton Pattern, which is perfect for this.
In this example, we are creating a MySQLi object and forcing it to a single instance. We just need to call DB::get() to create and/or access the object.
Jun/094
Snippet: Converting Seconds to Readable Time (PHP & JS)
Sometimes, you might need to convert an integer representing seconds into a format that is easier to read. These functions can be used to turn a number of seconds into a simple format of HH:MM:SS, with leading zeros (ex. 15272 = 04:14:32). This can be used for countdown scripts, which is why I also include both a PHP and a JavaScript version.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | function formatTime($secs) { $times = array(3600, 60, 1); $time = ''; $tmp = ''; for($i = 0; $i < 3; $i++) { $tmp = floor($secs / $times[$i]); if($tmp < 1) { $tmp = '00'; } elseif($tmp < 10) { $tmp = '0' . $tmp; } $time .= $tmp; if($i < 2) { $time .= ':'; } $secs = $secs % $times[$i]; } return $time; } |
May/091
Snippet: Autoloading Objects (PHP 5)
When writing the PHP that powers TempServers, I wrote the core functions into classes for better organization and efficiency. To make things further organized, each class is stored in individual source files, no matter how small. The problem here is keeping track of which classes are used where, in order to include the appropriate source files. Fortunately, PHP 5 offers a solution: autoloading.
This comes in the form of the __autoload function which automatically gets called if a class is called that does not exist. Here's what it looks like:
function __autoload($class) { require_once(strtolower($class) . '.class.php'); }
Usage
- Place each PHP class in individual files named after the class followed by .class.php (Example: class MyClass goes in a file named myclass.class.php).
- Include the code snipped above into any file that might use one of your classes.
For more details, check out the PHP manual: http://www.php.net/manual/language.oop5.autoload.php
Dec/082
Reloading Images Using JavaScript
There are some situations where you want to reload an image without refreshing the page containing it. This is especially handy with CAPTCHA images, where you might want to give the user the option to get a new code in case the first is too hard to read.
It sounds simple enough, but then there is browser caching that you need to work around. Fortunately, this problem is easy to solve just by altering the URL of the source, adding a unique string to the end (such as a timestamp) as a GET parameter. Here is a little JavaScript function that will accomplish this:
Dec/080
Bypassing Register_Globals in PHP
Register_globals is an option in PHP that automatically turns special variables (GET, POST, COOKIE, etc.) into global variables. For example, $_GET['id'] becomes $id and this can pose a problem if you already use $id as an internal variable. This option is deprecated and defaults to off in current PHP versions, but may still be enabled on some servers.
If you want to be sure your script works with or without register_globals, here is a snippet the you can add to the beginning of your script:
if(ini_get('register_globals')){ $globals = array_merge($_REQUEST, $_COOKIE, $_SESSION, $_SERVER, $_ENV); $rg = array_keys($globals); foreach($rg as $var){ unset(${$var}); } }
This will unset all variables that match the name in those superglobals, negating register_globals. This should work on PHP version 4.1.0+.
