« Avoiding sql injection attacks || My Top Five Favorite Firefox Addons »

Handy PHP Shorts

1


I like simple things...things that require little effort on my behalf. Here are some php functions/"shorts" that will probably prove useful somewhere in your future.

Get META tag information from practically any website (works locally, too!)

$thetags = get_meta_tags ( 'PATH OR URL' );print_r ( $thetags);

Get a ton of information about a user's browser: (also check out phpsniff)

echo $_SERVER['HTTP_USER_AGENT'] . "\n\n";$browser = get_browser(null, true);
print_r($browser);

Show the source of a file:

show_source("http://voolia.com");show_source(__FILE__);  //shows source of current file

Highlight source and number lines (great when you're stuck without an editor...no other use, in my opinion):

 
<style type="text/css">.line { float: left; color: gray; text-align: right; margin-right: 6pt; padding-right: 6pt; border-right: 1px solid gray;} </style>
 
<?php
function highlight_num($file)
{  echo '&lt;code class="line"&gt;', implode(range(1, count(file($file))), '&lt;br /&gt;'), '&lt;/code&gt;'; highlight_file($file); }
highlight_num('highlightfile.php');
?>

Create a very hard to guess unique id (impossible?) - php5 only:

 
$better_token = md5(uniqid(rand(), true));

Delay script execution (great for download scripts or advertising):

 
sleep(60); //in seconds.  this script will sleep for 1 minute as is.

Also check out time_sleep_until.

Print files in a directory:

 
$direct = dir('sample_directory/'); // the directory here
if ($direct)
{
while (false !== ($list = $direct-&gt;read())) {
if (!in_array($list, array('.', '..')))
{ echo "<a href='http://sampledomain.com/sample_directory/$list%5C%22'>$list</a>"; }
}
}

This entry was posted on Sunday, March 9th, 2008 at 5:52 pm and is filed under Blog, PHP, WebDev. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

Get a Trackback link

1 Trackbacks/Pingbacks

  1. Pingback: links for 2008-03-11 at Kevin Smith on March 11, 2008

Leave a comment