我试图编写此程序,以将文件中的用户名与输入的用户名进行比较以检查其是否存在,但是该程序似乎无法正常工作。请帮忙。该程序应该打开一个名为allusernames的文件以比较用户名。如果找不到用户名,请将其添加到文件中。
<?php
$valid=1;
$username = $_POST["username"];
$listofusernames = fopen("allusernames.txt", "r") or die("Unable to open");
while(!feof($listofusernames)) {
$cmp = fgets($listofusernames);
$val = strcmp($cmp , $username);
if($val == 0) {
echo ("Choose another user name, the user name you have entered has already been chosen!");
$valid=0;
fclose($listofusernames);
break;
} else {
continue;
}
}
if($valid != 0) {
$finalusers = fopen("allusernames.txt", "a+");
fwrite($finalusers, $username.PHP_EOL);
fclose($finalusers);
?>
您需要替换每行中的换行符/换行符以进行比较。
while(!feof($listofusernames)) {
$cmp = fgets($listofusernames);
$cmp = str_replace(array("\r", "\n"), '',$cmp);
$val = strcmp($cmp , $username);
if($val == 0) {
echo ("Choose another user name, the user name you have entered has already been chosen!");
$valid=0;
fclose($listofusernames);
break;
} else {
continue;
}
}
我在您的代码中添加了以下行
$cmp = str_replace(array("\r", "\n"), '',$cmp);
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句