Category archives: Snippet

Snippet

How to check if an email from an educational institution?

This PHP snippet (function) allows you to check if an email address from an educational institution.


<?php
/**
 * @title          Check if it is a valid and university email address.
 *
 * @param          string $sEmail
 * @return         boolean
 */
function isUniversityEmail($sEmail)
{
    return preg_match('/^[a-z0-9_\-]+(\.[_a-z0-9\-]+)*@([_a-z0-9\-]+\.)+edu$/i', $sEmail);
}
?>

Example of usage:


if (!isUniversityEmail('you@post.harvard.edu'))
    exit('You must have a valid and .edu email address to register for an account.');

Good code! ;-)

Continue reading

How to check if Apache's mod_rewrite is installed? | PHP Snippet

Hi everyone!

This is a snippet (function) that lets you know if the mod_rewrite of Apache is installed on a Web server.


<?php
/**
 * Check if Apache's mod_rewrite is installed.
 * 
 * @return boolean
 */
function isRewriteMod()
{
    if (function_exists('apache_get_modules'))
    {
        $aMods = apache_get_modules();
        $bIsRewrite = in_array('mod_rewrite', $aMods);
    }
    else
    {
        $bIsRewrite = (strtolower(getenv('HTTP_MOD_REWRITE')) == 'on');
    }

    return $bIsRewrite;
}
?>

How to use this?

Very simple, just look this example below:


if (!isRewriteMod()) exit('Please install Apache mod_rewrite module.');

Good code! ;-)

Continue reading

Get the favicon of a website | PHP Script Snippet

Here's a little feature that allows you to easily retrieve the favicon of a URL


<?php
/**
 * Get favicon from a URL.
 * 
 * @param string $sUrl
 * @return string The favicon image.
 */
function get_favicon($sUrl)
{
	$sApiUrl = 'http://www.google.com/s2/favicons?domain=';
	$sDomainName = get_domain($sUrl);
	
	return $sApiUrl . $sDomainName;
}

/**
 * Get domain name from a URL (helper function).
 * 
 * @param string $sUrl
 * @return string $sUrl Returns the URL to lower case and without the www. if present in the URL.
 */
function get_domain($sUrl)
{
        $sUrl = str_ireplace('www.', '', $sUrl);
        $sHost = parse_url($sUrl, PHP_URL_HOST);
        return $sHost;
}
?>

How to use this


<a href="http://www.php.net/manual ...

Continue reading

Check your Internet connection | PHP Script Snippet

To check if your Internet connection is enabled, a simple solution is this function:


<?php
/**
 * Check Internet Connection.
 * 
 * @param string $sCheckHost Default: http://www.google.com
 * @return boolean
 */
function check_internet_connection($sCheckHost = 'http://www.google.com') 
{
    return (bool) @fopen($sCheckHost, 'r');
}
?>

A function is a little more advanced:


<?php
/**
 * Check Internet Connection.
 * 
 * @param string $sCheckHost Default: www.google.com
 * @return boolean
 */
function check_internet_connection($sCheckHost = 'www.google.com') 
{
    return (bool) @fsockopen($sCheckHost, 80, $iErrno, $sErrStr, 5);
}
?>

Example of use:


<?php
$bIsConnected = check_internet_connection();
$sText = ($bIsConnected) ? '<p style="color:green">You are currently connected to the Internet.</p>' : '<p style="color:red">You are ...

Continue reading