星期二, 11月 28, 2006

javascript xEmail() function

今天我在 three.com.hk 上找到這段javascript程式碼:
function xEmail(strEmail) {
invalidChars ="~!$^&\*()+|=`\"\'";
if (strEmail==null || isWhitespace(strEmail))
return false;
if (strEmail.indexOf(' ') > -1)
return false;
if ((strEmail.indexOf(".com") == -1) && (strEmail.indexOf(".net") == -1) && (strEmail.indexOf(".org") == -1))
return false;
for (ai=0;ai badChar = invalidChars.charAt(ai);
if (strEmail.indexOf(badChar,0) > -1) {
return false;
}
}
atPos = strEmail.indexOf("@",1);
if (atPos ==-1)
return false;
if (strEmail.indexOf("@",atPos+1) > -1)
return false;
if (strEmail.indexOf("@.",1) > -1)
return false;
if (strEmail.indexOf("..",1) > -1)
return false;
periodPos = strEmail.indexOf(".",atPos);
if (periodPos == -1)
return false;
if (periodPos+2 > strEmail.length)
return false;
return true;
}

錯左一樣野。睇睇第七行幾白痴!乜呢個世界只係得.com .net .org !?three.com.hk 個個月食幾千至幾萬蚊人工的程式員冇聽聞過 .biz .us .cc .hk .tv 之類的 domain 咩?簡直係 programmer 的恥辱!我現在以一個熱愛編程的人這個身份對 three.com.hk 作出強烈的遣責,並要求該班程式員立即作出改善!我不希望這些不完善的垃圾程式碼會再次出現在一個 production system 上!!!

星期六, 10月 15, 2005

USL get onto SourceForge.net!

Well, today sf.net approved my registation of the USL(USB System Lock) project. It is a great solution to enhance system security. This program enable us to use a physical USB Drive to be a key to unlock the computer. Without the key, the computer will be locked by the program and cannot access by anyone. Nowadays, security become more and more important, so I am sure that this program will become very popular soon! Visit the USB System Lock webpage at http://usl.sf.net/ !

星期一, 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.