有人可以向我解釋爲什麼這種模式被識別爲字母數字?字母數字模式PHP與preg_match
\ 13 \ 13 \ 13 \ 13 \ 13 \ 13 \ 13 \ 13 \ 13234234 \ 3
請參見本鏈接enter link description here
像我寫的代碼只是爲了告訴你,它是公認的字母數字
編輯15.06.214 21:40
我補充一些資料,我很抱歉,我忘了。
您看到的變量取自輸入表單。這是我爲了使信息清潔並準備插入數據庫而使用的代碼。
PS:只爲您的信息,變量$ errform用於以後的頁面
<?php
$username = $password = $fname = $lname = $mail = $id_dept = "";
$usernameERR = $passwordERR = $fnameERR = $lnameERR = $id_deptERR = "";
$errform = 2;
?>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$username = clean_data($_POST["username"]);
$password = clean_data($_POST["password"]);
$fname = clean_data($_POST["fname"]);
$lname = clean_data($_POST["lname"]);
$mail = clean_data($_POST["mail"]);
$id_dept = clean_data($_POST["id_dept"]);
if (!preg_match("/^[a-zA-Z0-9]*$/", $username)) {
$usernameERR = "Only letters and numbers allowed";
}
if (!preg_match("/^[a-zA-Z0-9]*$/", $password)) {
$passwordERR = "Only letters and numbers allowed";
}
if (!preg_match("/^[a-zA-Z ]*$/", $fname)) {
$fnameERR = "Only letters and white space allowed";
}
if (!preg_match("/^[a-zA-Z ]*$/", $lname)) {
$lnameERR = "Only letters and white space allowed";
}
}
function clean_data($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
if (isset($_POST['submit'])) {
if ((!preg_match("/^[a-zA-Z0-9]*$/", $username))
|| (!preg_match("/^[a-zA-Z0-9]*$/", $password))
|| (!preg_match("/^[a-zA-Z ]*$/", $fname))
|| (!preg_match("/^[a-zA-Z ]*$/", $lname))) {
$errform = 1;
} else {
$errform = 0;
$con = mysqli_connect($hostdb,$userdb,$passwdb,$dbTEST);
if (mysqli_connect_errno()) {
echo "Impossibile connettersi a MySQL: " . mysqli_connect_error();
}
$username = mysqli_real_escape_string($con, $_POST['username']);
$password = md5(mysqli_real_escape_string($con, $_POST['password']));
$fname = mysqli_real_escape_string($con, $_POST['fname']);
$lname = mysqli_real_escape_string($con, $_POST['lname']);
$mail = mysqli_real_escape_string($con, $_POST['mail']);
$id_dept = mysqli_real_escape_string($con, $_POST['id_dept']);
if (!mysqli_query($con, "INSERT INTO user (id_user, username, password, fname, lname, mail, id_dept) VALUES (NULL, '$username', '$password', '$fname', '$lname', '$mail', '$id_dept')")) {
die ("Error: " . mysqli_error($con));
}
mysqli_close($con);
}
}
?>
編輯15/06/2014 22:41
的 「觸發」 錯誤消息好吧,
對不起,大家我理解了問題的根源。
什麼我檢查是
(preg_match("/^[a-zA-Z0-9]*$/", $username))
在我的代碼,$用戶名得到這個待遇
$username = clean_data($_POST["username"]);
if (!preg_match("/^[a-zA-Z0-9]*$/", $username)) {
$usernameERR = "Only letters and numbers allowed";
}
其中
function clean_data($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
所以當我發佈 \ q \ q \ q \ q \ q \ q \ q \ q \ q 這被剝離並且變成 qq qqqqqqq
但是,有兩個問題。首先的問題是,我與此相呼應
echo "post username: " . $_POST["username"];
以及與此
echo "preg username " . preg_match("/^[a-zA-Z0-9]*$/", $username);
比較,很明顯的是$ _ POST [「用戶名」]是不是==至$的用戶名,因爲這最後一個拿到了處理clead_data函數,而POST不行。
所以當我在做preg_match時,我正在做$ username = qqqqqqqqqqq(這是字母數字),而我告訴你的是$ _POST ['username'] = \ q \ q \ q \ q \ q \ q \ q \ q \ q \ q \ q
的第二個問題是,我是發送到數據庫
$username = mysqli_real_escape_string($con, $_POST['username']);
,而我應該就這麼傳這
$username = mysqli_real_escape_string($con, $username);
數據庫到達了NON CLEANED數據。
請編輯該問題以包含相關代碼和輸出,並使其更好地描述情況。現在,即使使用屏幕截圖也不清楚,使用屏幕截圖來轉儲這樣的信息無論如何都是不被接受的。 – Jon