Mar
09
2008

Handy PHP Shorts

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>"; }
}
}
Written by Kevin in: PHP, WebDev |

1 Comment »

  • [...] Handy PHP Shorts (tags: php programming resources design code blogs blogging blog articles development free geek generator hacks ideas wordpress webdesign webdev wevdev web tutorials tutorial tools tips) [...]

    Pingback | March 11, 2008

RSS feed for comments on this post. TrackBack URL

Leave a comment

Powered by WordPress | Aeros Theme | TheBuckmaker.com WordPress Themes