douzhi7451 2018-08-23 19:13
浏览 86

如何更改此正则表达式“/ ^ [0-9] + $ / m”?

<title>mylogin</title>
<body>
<form action="sql.php" method="POST">
User ID: <input type="text" id="userid" name="userid"/><br/>
Password:  <input type="text" id="password" name="password"/><br/>
<input type="submit" value="send">
</form>
</body>

and my code sql.php is

<?php
include('..\db.php');
$con = mysqli_connect($dbsrvname, $dbusername, $dbpassword, $dbname);
if (!$con){
     echo("Connection ERROR");
     die(print_r(mysqli_error($con)));
   }
if (!preg_match("/^[0-9]+$/m", $_POST["userid"])){
    die("ONLY numbers allowed");
    }
$query = "SELECT * FROM tbl1 WHERE id=" . 
     $_POST["userid"] . " AND password='" .
     mysqli_real_escape_string($con, $_POST["password"]) . "';";
$stms = mysqli_query($con, $query);
if ($stms === false){
    echo("ERROR during query execution: ");
    die(print_r(mysqli_error($con))); 
    }
$row = mysqli_fetch_array($stms, MYSQLI_ASSOC);
if ($row){
    die("Logged in");
    }
else{
    die("Wrong username or password");}

?>

when in this code

if (!preg_match("/^[0-9]+/", $_POST["userid"])){
    die("ONLY numbers allowed");
}

in this code when I input the first number and after this any char like 9a the statement (if) is false and continues without printing "only allowed numbers"

but in this statement

if (!preg_match("/^[0-9]+$/m", $_POST["userid"])){
    die("ONLY numbers allowed");
}

when I input 9a or any first number with char like 9a the statement is true and prints "only allowed numbers"

What is the input value I should enter to make the statement (false) and not go inside the if statement which is printing the allowed numbers?

  • 写回答

2条回答 默认 最新

  • dongyanju1094 2018-08-23 19:20
    关注

    Do not use Regex at all.

    if(!filter_input(INPUT_POST, "userid", FILTER_SANITIZE_NUMBER_INT))
        die ("ONLY numbers allowed");
    
    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度