MENU

PHP验证电子邮件-密码保护和随机密码

• October 20, 2019 • Read: 150 • 代码分享阅读设置

做表单验证的时候需要用到这些东西

验证邮箱:

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

随机密码:

function generatePassword($length=9, $strength=0) {
    $vowels = 'aeuy';
    $consonants = 'bdghjmnpqrstvz';
    if ($strength & 1) {
        $consonants .= 'BDGHJLMNPQRSTVWXZ';
    }
    if ($strength & 2) {
        $vowels .= "AEUY";
    }
    if ($strength & 4) {
        $consonants .= '23456789';
    }
    if ($strength & 8) {
        $consonants .= '@#$%';
    }
    $password = '';
    $alt = time() % 2;
    for ($i = 0; $i < $length; $i++) {
        if ($alt == 1) {
            $password .= $consonants[(rand() % strlen($consonants))];
            $alt = 0;
        } else {
            $password .= $vowels[(rand() % strlen($vowels))];
            $alt = 1;
        }
    }
    return $password;
}

密码保护:

function passwordProtect($username, $password){
    if (
            (
                !isset($_SERVER['PHP_AUTH_USER']) ||
                (
                    isset($_SERVER['PHP_AUTH_USER']) &&
                    $_SERVER['PHP_AUTH_USER'] != $username
                )
            ) &&
            (
                !isset($_SERVER['PHP_AUTH_PW']) ||
                (
                    isset($_SERVER['PHP_AUTH_PW']) &&
                    $_SERVER['PHP_AUTH_PW'] != $password
                )
            )
        )
    {
        header('WWW-Authenticate: Basic realm="Login"');
        header('HTTP/1.0 401 Unauthorized');
        echo 'Please login to continue.';
        exit;
    }
}
Archives QR Code Tip
QR Code for this page
Tipping QR Code