[PHP字符串]⑧--验证码校验

strip_tags

Paste_Image.png
<?php

$str = "<h1>This is a tets</h1><a href='http://www.imooc.com' target='_blank'>Imooc</a> ";
echo $str;

echo "<hr/>";
echo strip_tags($str);//This is a tetsImooc

echo "<hr/>";
echo strip_tags($str, '<a>');//This is a tetsImooc

?>
Paste_Image.png

trim

ltrim

rtrim

Paste_Image.png
$str = "  abc   ";

echo "X" . $str . "X";//X abc X
echo "<hr/>";

echo "X" . trim($str) . "X";//XabcX
echo "<hr/>";

echo "X" . ltrim($str) . "X";//Xabc X
echo "<hr/>";

echo "X" . rtrim($str) . "X";//X abcX
echo "<hr/>";

修改代码

<input type="hidden" name="verify1" value='<?php echo strip_tags($code) ?>'></td>
$verify = trim(strtolower($_POST['verify']));
$verify1 = trim(strtolower($_POST['verify1']));
if ($verify1 !== $verify) {
    exit("验证码错误<br/>" . $redirectUrl);
}

echo "恭喜您注册成功<br/>";

reg.php

<?php
$string = "ABCDEFGHIJKLMNOPQRSJUVWXYZ1234567890";
$code = "";
for ($i = 1; $i <= 4; $i++) {
    $code .= '<span style="color:rgb(' . mt_rand(0, 255) . ',' . mt_rand(0, 255) . ',' . mt_rand(0, 255) . ')">' . $string{mt_rand(0, strlen($string) - 1)} . '</span>';
}


?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>注册页面</title>
</head>
<body>
<h1>慕课网注册页面</h1>
<form method="post" action="doAction.php">
    <table border="1" cellspacing="0" cellpadding="0" width="80%" bgcolor="#ABCDEF">
        <tr>
            <td align="right">用户名</td>
            <td><input type="username" name="username" id="" placeholder="请输入合法用户名...">用户名首字母以字母开始,并且长度6~10</td>
        </tr>
        <tr>
            <td align="right">密码</td>
            <td><input type="password" name="password" placeholder="请密码...">密码不能为空,长度6~10</td>
        </tr>
        <tr>
            <td align="right">确认密码</td>
            <td><input type="password" name="password1" id="" placeholder="请输入确认密码...">两次密码一致</td>
        </tr>
        <tr>
            <td align="right">邮箱</td>
            <td><input type="text" name="email" id="" placeholder="请输入合法邮箱">邮箱必须包含@,382771946@qq.com</td>
        </tr>
        <tr>
            <td align="right">兴趣爱好</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="ios">Ios
                <input type="checkbox" name="fav[]" id="" value="c">C语言
                <input type="checkbox" name="fav[]" id="" value="c++">C++
                <input type="checkbox" name="fav[]" id="" value="swift">Swift
                <input type="checkbox" name="fav[]" id="" value="meteor">Meteor
                <input type="checkbox" name="fav[]" id="" value="nodejs">NodeJS
                <input type="checkbox" name="fav[]" id="" value="ionic">Iconic
            </td>
        </tr>
        <tr>
            <td align="right">验证码</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 colspan="2"><input type="submit" value="注册"></td>
        </tr>
    </table>
</form>
</body>
</html>

doAction.php

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

$username = $_POST['username'];
$password = $_POST['password'];
$password1 = $_POST['password1'];
$email = $_POST['email'];
//    $fav=$_POST['fav'];
$verify = trim(strtolower($_POST['verify']));
$verify1 = trim(strtolower($_POST['verify1']));
$redirectUrl = '<a href="reg.php" > 重新注册</a > ';

//$char = $username{0};
$char = substr($username, 0, 1);
$ascii = ord($char);
if (!(($ascii >= 65 && $ascii <= 90) || ($ascii >= 97 && $ascii <= 122))) {
    exit('用户名首字母不是以字母开始' . $redirectUrl);
}

$userLen = strlen($username);
if ($userLen < 6 || $userLen > 10) {
    exit('用户名长度不符合规范 <br/>' . $redirectUrl);
}

$pwdLen = strlen($password);
if ($pwdLen == 0) {
    exit('密码不能为空<br/>' . $redirectUrl);
}
if ($pwdLen < 6 || $pwdLen > 10) {
    die("密码长度不符合规范" . $redirectUrl);
}

//if ($password !== $password1) {
//    exit("两次密码不一致<br/>" . $redirectUrl);
//}
if (strcmp($password, $password1) !== 0) {
    exit("两次密码不一致<br/>" . $redirectUrl);
}

//位置0也是false
if (strpos($email, '@') == false) {
    exit("非法邮箱<br/>" . $redirectUrl);
}

if ($verify1 !== $verify) {
    exit("验证码错误<br/>" . $redirectUrl);
}

echo "恭喜您注册成功<br/>";


?>
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 字符串的处理介绍 字符串的处理方式 在C语言中字符串是作为字节数组处理的。在Java语言中字符串是作为对象处理的。...
    dptms阅读 1,312评论 0 1
  • 运算符 一,算术运算符 算术运算符用语完成各种运算; 二,赋值运算符 $a = "hello";$b = $a ....
    寒梁沐月阅读 462评论 0 1
  • 一、输出字符串 1、echoecho是一个语法,不是函数,它没有返回值,可以输出多个值,使用逗号分隔。 二、查找与...
    XZ阳光小熊阅读 319评论 0 1
  • 上大学时,有个同学严重近视,眼镜片巨厚。但理发的时候不方便戴眼镜,所以每次理发都得先交代理发师怎么理,然后每过一段...
    乱红N阅读 466评论 3 6
  • 知乎2万+赞,微博10万+转发。 有个管生不管养的妈(知乎ID:lushark),她生了孩子断母乳,不管养育只管逗...
    我是乔一阅读 661评论 0 2

友情链接更多精彩内容