溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

PHP字符串操作實戰(zhàn)用戶注冊檢測界面

發(fā)布時間:2020-06-25 04:49:07 來源:網(wǎng)絡 閱讀:794 作者:一百個小排 欄目:開發(fā)技術

PHP字符串操作實戰(zhàn)用戶注冊檢測界面
一.重點

用字符串的內(nèi)置函數(shù)對輸入的字符串進行處理

substr() 取字符串
ord()轉為ascii碼
str()把ascii碼轉為字符串
strcmp()比較兩個字符串,轉為ascii碼比較
strcasecmp()忽略大小寫比較
strpos($string,字符)字符串中查找,返回第一次出現(xiàn)的值,沒有返回false
stripos()忽略大小寫
strrpos()最后出現(xiàn)的位置
strip_tags()過濾字符串的html和php標記
strip_tags($str,'<a>')留下<a>標簽
strtolower()轉為小寫
trim()過濾兩端空格
ltrim()過濾左空格
rtrim()過濾右空格
empty()是否為空
join(‘,‘,$string)以‘,‘分隔 ,數(shù)組轉為字符串
md5()加密字符串
sha1()加密字符串

二.練習代碼用戶登錄注冊頁面
兩個頁面 register.php doaction.php

register.php 首頁代碼

<?php

//簡單的制作驗證碼字符串
$string = 'qwertyuiopasdfgjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM1234567890';
//echo $string{mt_rand(0,strlen($string)-1)};
$code = '';
for($i=0;$i<4;$i++) {
    $code .= '<span >'
    .$string{mt_rand(0,strlen($string)-1)}.'</span>';
}
//echo $code;

?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset='UTF-8'>
    <title>注冊練習</title>
</head>
<body>
<h2 align="center">個人注冊頁面</h2>
<div align="center">
<form method="post" action="doaction.php">
    <table border="1" cellspacing="0" cellpadding="0" width="60%" bgcolor="#ABCDEF">
        <tr>
            <td align="center">用戶名</td>
            <td><input type="text" name="username" id="" placeholder='請輸入合法的用戶名...'>用戶名首字母以字母開始,并且長度6~10</td>
        </tr>
        <tr>
            <td align="center">密碼</td>
            <td><input type="password" name="password" placeholder='請輸入密碼'>密碼不能為空長度6~10</td>
        </tr>
        <tr>
            <td align="center">確認密碼</td>
            <td><input type="password" name="password1" id="" placeholder='請輸入確認密碼'>兩次密碼輸入一直</td>
        </tr>
        <tr>
            <td align="center">郵箱</td>
            <td><input type="text" name="email" id="" placeholder='請輸入合法的郵箱名'>郵箱必須包含@</td>
        </tr>
        <tr>
            <td align="center">興趣愛好</td>
            <td>
                <input type="checkbox" name="fav[]" id="" value="php">php
                <input type="checkbox" name="fav[]" id="" value="java">java
                <input type="checkbox" name="fav[]" id="" value="python">python
                <input type="checkbox" name="fav[]" id="" value="javascript">javascript
                <input type="checkbox" name="fav[]" id="" value="vue">vue
            </td>
        </tr>
        <tr>
            <td align="center">驗證碼</td>
            <td>
            <input type="text" name="verify"><?php echo $code?>
            <input type="hidden" name="verify1" value="<?php echo strip_tags($code)?>">
            </td>
        </tr>
        <tr>
            <td align="center" colspan="2"><input type="submit" value="立即注冊"></td>
        </tr>
    </table>
</div>
</form>
</body>
</html>

doaction.php 后端驗證代碼

<?php
header("Content-type:text/html;charset=utf-8");

//接收數(shù)據(jù)
$username = $_POST['username'];
$password = $_POST['password'];
$password1 = $_POST['password1'];
$email = $_POST['email'];

$fav = $_POST['fav'];
//判斷用戶是否選擇了愛好,并將數(shù)據(jù)轉化為字符串顯示出來,并用,分隔
if (!empty($fav)) {
    $favStr=join(',',$fav);
}

$verify = trim(strtolower($_POST['verify']));//都轉化為小寫,并去掉兩邊的空格
$verify1 = trim(strtolower($_POST['verify1']));//都轉化為小寫,并去掉兩邊的空格
//echo $verify1;

$redirectUrl = '<a href="register.php">重新注冊</a>';

//檢測第一個字符是不是字母
//$char = $username{0};
$char = substr($username,0,1);
$ascli = ord($char);//得到指定字符的ascli碼

//檢測ascli是否在65~90(A~Z)或者97~122(a~z)之間表示是字母
if (!(($ascli>=65 && $ascli<=90) || ($ascli>=97 && $ascli<=122))) {
    exit('用戶名首字母不是以字母開頭開始<br>'.$redirectUrl);
}

//檢測用戶名長度6~10
$userLen = strlen($username);
if ($userLen<6 || $userLen>10) {
    exit('用戶名長度必須是6~10<br>'.$redirectUrl);
}

//檢測密碼不能為空
$passwdLen = strlen($password);
if ($passwdLen == 0) {
    die('密碼不能為空<br>'.$redirectUrl);
}

//檢測密碼長度6~10
if($passwdLen<6 || $passwdLen>10) {
    die('密碼長度不符合規(guī)范<br>'.$redirectUrl);
}

//檢測密碼兩次是否一致
//if ($password != $password1 ) {
//  exit('兩次密碼不一致<br>'.$redirectUrl);
//}
if (strcmp($password, $password1) != 0) {
    exit('兩次密碼不一致<br>'.$redirectUrl);
}

//檢測郵箱的合法性
if (strpos($email, '@') == false) { //0==false 0也返回false
    exit('非法郵箱<br>'.$redirectUrl);
}

//檢測驗證碼是否符合規(guī)范
if ($verify != $verify1) {
    exit('驗證碼錯誤<br>'.$redirectUrl);
}

//使用md5進行加密密碼
$password = md5($password);

echo "恭喜您注冊成功,用戶信息如下:";
$userinfo=<<<EOF
<table border="1" cellspacing="0" cellpadding="0" width="50%">
    <tr>
        <td>用戶名</td>
        <td>密碼</td>
        <td>郵箱</td>
        <td>興趣愛好</td>
    </tr>
    <tr>
        <td>$username</td>
        <td>$password</td>
        <td>$email</td>
        <td>$favStr</td>        
    </tr>
</table>
EOF;

echo $userinfo;
?>

三.截圖
PHP字符串操作實戰(zhàn)用戶注冊檢測界面
PHP字符串操作實戰(zhàn)用戶注冊檢測界面

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權內(nèi)容。

AI