Posts Tagged ‘RAJI’

PHP:safe_dl — Safely Load a PHP extension at runtime

Monday, June 30th, 2008

<?php
$extension = “php_domxml”;
var_dump(safe_dl($extension));
/**
* Loads extension safely
*
* Checks whether the extension is already loaded, if so does not try to reload it.
* Depending ot current OS it will try to load “.dll” or “.so” file.
* Some checks are made for GD library.
*
* @param    string    extension the extension that will be loaded
* @author RAJI <raji.peaceful@gmail.com>
* @author RAJI
*/
function safe_dl($extension)
{
$res = 0;
$extension = strtolower(trim($extension));
// if these is an extension supplied remove it
if (($posit = strpos($extension, “.”)) !== false)
$extension = substr($extension, 0, $posit);
// special check for gd1 & gd2
if ($extension == ‘gd’ || $extension == ‘gd2′ )
{
$ar = @gd_info();
if (is_array($ar)
&& !empty($ar["GD Version"])
&& (
// gd1
( ($extension == ‘gd’ && strpos($ar["GD Version"], “1.”) !== false)
||
// gd2
$extension == ‘gd2′&& strpos($ar["GD Version"], “2.”) !== false)
)
)
return true;
else
return false;
}
// if already loading return true
if (!empty($extension)
&& extension_loaded($extension))
return true;
// if not extension not loaded try to load it
if ( !empty($extension) &&
!extension_loaded($extension) &&
( @ini_get(”enable_dl”) == 1 ||
strtolower(@ini_get(”enable_dl”) ) == “on”)
)
{
// depending on OS load appopriate extension
$res = @dl($extension . “.”
. (strpos(strtolower(PHP_OS), “win”) !== false
? ‘dll’
: ’so’));
}
return $res
? true
: false;
}
?>

PHP:Validate URL (FTP, HTTP) using regular expression ( regex )

Monday, June 30th, 2008

<?php
/**
* valid_url - validates supplied url with a regular expression ( regex ).
*
* URL can be FTP, HTTP secure
* @param $url String
* @return true if the address is valid
* @author RAJI
*
*/
function valid_url( $url )
{
if ( !preg_match( ‘!^((ht|f)tps?://)?[a-zA-Z]{1}([w-]+.)+([w]{2,5})/?$!i’, $url ) )
{
return false;
} else
{
return true;
}
}
if ( valid_url( “http://blog.tryangled.com” ) )
{
echo “URL OK”;
} else
{
echo “Invalid URL.”;
}
?>