星期一, 9月 26, 2005

Valid email address correctly (PHP)

Nowadays, valid the format of the email is not enough, so, we need to valid it with making a testing connection to the email server and ask the email server that the user provided if the email account exisit! Here is a PHP function that can do this for you:
<?php

/* Return value: (true|false)
   true if valid, false if invalid */

function valid_email($email)
{
    global
$HTTP_HOST;

    if (!
eregi("^[_a-z0-9-]+(.[_a-z0-9-]+)*@[a-z0-9-]+(.[a-z0-9-]+)*(.[a-z]{2,3})$", $email)) {       
        return
false;
    }

    list(
$username,$domain) = split("@",$email);

    if (
checkdnsrr($Domain,"MX")) {
        
getmxrr ($Domain, $mxhost);
        
$host = $mxhost[0];
    }
    else {
        
$host = $domain;         
    }

    
$connect = fsockopen($host,25};
    if (
$connect) {
        if (
ereg("^220", $returns=fgets($connect,1024))) {
            
fputs($connect,'HELO ' . $HTTP_HOST . "\r\n");
            
$returns = fgets ($connect,1024);
            
fputs($connect,'MAIL FROM: <' . $email . ">\r\n");
            
$test1 = fgets($connect,1024);
            
fputs ($connect,'RCPT TO: <' . $email . ">\r\n");
            
$test2 = fgets($connect,1024);
            
fputs ($connect,"QUIT\r\n");
            
fclose($connect);
            if ((!
ereg("^250",$test1)) or (!ereg("^250",$test2))) {
                return
false;
            }
        }
    }
    else {
        return
false;
    }
    return
true;
}

?>

星期一, 8月 01, 2005

A better way to detect IP in PHP

Sometimes we cannot get the correct IP of the client by $_SERVER['REMOTE_ADDR'], so we need the following function!
<?php
function getrealip()
{
    
$ip = FALSE;
    if (!empty(
$_SERVER["HTTP_CLIENT_IP"])) {
        
$ip = $_SERVER["HTTP_CLIENT_IP"];
    }

    if (!empty(
$_SERVER['HTTP_X_FORWARDED_FOR'])) {
        
$ips = explode (", ", $_SERVER['HTTP_X_FORWARDED_FOR']);
        if (
$ip) {
            
array_unshift($ips, $ip);
            
$ip = FALSE;
        }

        for (
$i = 0; $i < count($ips); $i++) {
            if (!
eregi ("^(10|172\.16|192\.168)\.", $ips[$i])) {
                if (
version_compare(phpversion(), "5.0.0", ">=")) {
                    if (
ip2long($ips[$i]) != false) {
                        
$ip = $ips[$i];
                        break;
                    }
                } else {
                    if (
ip2long($ips[$i]) != -1) {
                        
$ip = $ips[$i];
                        break;
                    }
                }
            }
        }
    }
    return (
$ip ? $ip : $_SERVER['REMOTE_ADDR']);
}
?>

星期日, 7月 31, 2005

Simple language detect function(PHP)

Well, I have some extra time so I develop this code snippet.



<?php

function hw_delectlang() {

    if (
substr($_SERVER['HTTP_ACCEPT_LANGUAGE'],0,2) == 'en'){


        
$lang = 'en'; //English

    
} elseif (substr($_SERVER['HTTP_ACCEPT_LANGUAGE'],0,2) == 'zh'){


        if ((
substr($_SERVER['HTTP_ACCEPT_LANGUAGE'],3,2) != 'cn') && (substr($_SERVER['HTTP_ACCEPT_LANGUAGE'],3,2) != 'sg')){


            
$lang = 'cht'; //Chinese Trad.

        
} else {

            
$lang = 'chs'; //Chinese Simp.

        
}

    } else {

        
$lang = 'en'; //Default is English


    
}

    return
$lang;

}

?>



Hope this works.

星期三, 6月 08, 2005

PHP & Java both have their 10 years old birthday in this month

Well, today is the PHP's 10 years old birthday! And Java will have its 10 years old birthday in this month too! As a PHP programmer, I think today is quite special and I should do something special today!