php-Preg_Matchが機能していません。エラー:「要求されたURLがこのサーバーで見つかりませんでした。」
ユーザー名が自分のサイトで有効かどうかを確認するさまざまな方法を試しましたが、それでもpreg_matchで検出されません。
サインアップボタンをクリックすると、ブラウザに「invalidusername」と表示されます:[1]: https://i.stack.imgur.com/fznXA.png
signup.inc.php:
<?php
if(isset($_POST["submit"])){
$firstname =$_POST["firstname"];
$lastname =$_POST["lastname"];
$username =$_POST["username"];
$email =$_POST["email"];
$gender =$_POST["gender"];
$password =$_POST["password"];
$repeatPassword =$_POST["repeatpassword"];
require_once 'dbh.inc.php';
require_once 'functions.inc.php';
if (emptyInputSignup($firstname, $lastname, $username, $email, $gender, $password, $repeatPassword ) !==false){
header("location:../signup.php?error=emptyinput");
exit();
}
if (invalidUsername($username) !==false){
header("location:../signup.php?error=invalidusername");
exit();
}
if (invalidUsername($email) !==false){
header("location:../signup.php?error=invalidemail");
exit();
}
if (passwordMatch($password, $repeatPassword) !==false){
header("location:../signup.php?error=passworddontmatch");
exit();
}
if (usernameExists($con, $username) !==false){
header("location:../signup.php?error=passworddontmatch");
exit();
}
createUser($con,$name,$email,$username,$pwd);
}
else {
header("location:../signup.php");
exit();
}
function.inc.php:
function invalidUsername($username){
$result;
if (!preg_match("/[^A-Za-z0-9_\-]/", $username)) {
$result= true;
}
else{
$result= false;
}
return $result;
}
答え :
解決:
「見つかりませんでした。要求されたURLがこのサーバーで見つかりませんでした」というエラーは、正確にはPHPエラーではありません。これはapacheエラーです。ルートを作成したことのないパスにフォームをリダイレクトしています。これは、いわばデフォルトの「404」ページです。
preg_matchに関しては、それはあなたのパターンかもしれません。私は個人的にフィルターパターンに精通していませんが、それらはすべてオンラインですぐに利用できるので問題ありません。
https://www.w3schools.com/PHP/php_form_url_email.asp
また、このエラーが実際にコードに含まれているのか、ここでの質問に誤って入力したのかはわかりませんが、signup.inc.phpページの3番目のifステートメントでinvalidUsernameを使用してメールをチェックしていることに注意してください。
次のフィルターを使用してメールをチェックします:"/^[A-Za-z0-9._%-][email protected][A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/"
コードを次のように書き直します。
if (!preg_match("/^[a-zA-Z0-9]+$/", $username)) {
$result=true;
}
else{
$result=false;
}
答え :
解決:
まず、冗長なコードを邪魔にならないようにします。if
!
、結果はすでにブール値になり、if
はtrueに分岐します。したがって:
function invalidUsername(string $username): bool { return (bool) !preg_match("/[^A-Za-z0-9_-]/", $username); }
すでに十分です。
正規表現をより深く理解したり、テストまたはデバッグしたりするには、 https: //regex101.comを使用できます。これは、PHP正規表現をサポートし、パターンについても説明しています(互換性のあるテキスト表現のみを参照してください。より読みやすいバリアント):
/[^A-Za-z0-9_\-]/
Match a single character not present in the list below
[^A-Za-z0-9_\-]
* A-Z matches a single character in the range between A (index 65)
and Z (index 90) (case sensitive)
* a-z matches a single character in the range between a (index 97)
and z (index 122) (case sensitive)
* 0-9 matches a single character in the range between 0 (index 48)
and 9 (index 57) (case sensitive)
* _ matches the character _ with index 95 (x5F or 0x137)
literally (case sensitive)
* \- matches the character - with index 45 (x2D or 0x55)
literally (case sensitive)
それは通常、物事をうまく始めるのに役立ちます。
同様の質問
私たちのウェブサイトで同様の質問で答えを見つけてください。