【杂谈】PHP怎样过滤post提交数据中的空格
2019-11-18杂谈搜奇网58°c
A+ A-
本篇文章主要给人人引见PHP如何将post提交过来的数据举行空格过滤。
人人应当都晓得,我们在开辟登录界面时,除了前台的一些js考证,另有一些提交数据到背景后的考证。
那末关于登录界面form表单提交的前台考证要领,在这两篇文章【jQuery表单考证提交:前台考证一】【jQuery表单考证提交:前台考证二】中也给人人举行相干的引见了,须要的朋侪能够挑选参考。
下面我就经由过程简朴的代码示例,给人人引见php背景过滤数据空格考证的要领。
1、login.html代码示例:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>登录</title> <style type="text/css"> body { background: url(images/bg.png); } .login { width: 370px; margin: 100px auto 0px; text-align: center; } #username{ width: 360px; height: 50px; border: none; background: #fff; border-radius: 10px; margin: 5px auto; padding-left: 10px; color: #745A74; font-size: 15px; } #password{ width: 360px; height: 50px; border: none; background: #fff; border-radius: 10px; margin: 5px auto; padding-left: 10px; color: #745A74; font-size: 15px; } .botton { width: 130px; height: 40px; background: #745A74; border-radius: 10px; text-align: center; color: #fff; margin-top: 30px; line-height: 40px; } </style> </head> <body> <div class="login"> <form action="check1.php" method="post"> <img src="images/header.png"><br> <input type="text" id="username" name="username" placeholder="请输入用户名!" value=""><br> <input type="password" id="password" name="password" placeholder="请输入暗码!" value=""><br> <input type="submit" class="botton" onclick="add()" value="login"> </form> </div> </body> </html>
这里的form表单主假如经由过程post体式格局提交数据到check1.php中。
2、check1.php代码示例:
<?php $arr = ['admin']; if (in_array(trim($_POST['username']),$arr)){ echo "登录胜利!"; }else{ echo "用户名不存在!"; } var_dump($_POST['username']); var_dump(trim($_POST['username']));
前台登录界面效果以下图:
假如我们将上述check1.php代码中trim函数删撤除。
注:trim() 函数示意移除字符串两侧的空缺字符或其他预定义字符。
那末当我们输入带空格的用户名,效果就以下图:
如图显现用户名不存在,这是因为我们背景没有举行过滤数据空格的操纵。
当我们依据上述check1.php的完全代码来处置惩罚提交过来的数据,那末即使是带有空格的用户名,都邑显现登录胜利。
效果以下:
如图我们胜利过滤了用户名前后的空格。
其实在我们一样平常登录种种背景的过程当中,想必有一部分朋侪都习气在输入用户名或许暗码时顺手加空格。那末人人能够依据本身项目的需求,来决议是不是须要举行过滤空格的操纵。
本篇文章就是关于PHP将post提交过来的数据举行空格过滤的要领引见,通俗易懂,愿望对须要的朋侪有所协助!
想要相识更多PHP学问,能够关注Ki4网PHP视频教程,迎接人人参考进修!
以上就是PHP怎样过滤post提交数据中的空格的细致内容,更多请关注ki4网别的相干文章!
标签:过滤空格