PHP脚本将无法识别匹配的字符串
我正在尝试编写一个php脚本,它将滚动文本文件中的字符串列表,并将每个字符串与html表单中的用户输入进行比较。 如果找到匹配项,则应将用户输入的字符串发布到屏幕上。不知何故,尽管存在相同的字符串,但两个字符串之间的比较从未产生匹配 下面是php代码PHP脚本将无法识别匹配的字符串,php,forms,file,Php,Forms,File,我正在尝试编写一个php脚本,它将滚动文本文件中的字符串列表,并将每个字符串与html表单中的用户输入进行比较。 如果找到匹配项,则应将用户输入的字符串发布到屏幕上。不知何故,尽管存在相同的字符串,但两个字符串之间的比较从未产生匹配 下面是php代码 <?php session_start(); $myFile = "usernamelist.txt"; if (isset($_POST['originaluserid'])){//verifies the creation user i
<?php
session_start();
$myFile = "usernamelist.txt";
if (isset($_POST['originaluserid'])){//verifies the creation user input from the html page(for users signing up for the first time)
$userid = $_POST['originaluserid'] . PHP_EOL;
$fh = fopen($myFile, 'a') or die("can't open file");
fwrite($fh, $userid);
}
if(isset($_POST['userid'])){//verifies the existence of username information for an old user logging back in
$userid = $_POST['userid'];
$fh = fopen($myFile, 'r');
}
$theData = fgets($fh);
$_SESSION['id'] = $userid;//so that userid can be called in another page
if ($fh) {
while (($line = fgets($fh)) !== false) {
if($userid == $theData){//errors in matching input with collected data in text file
echo "<html>
<head></head>
<body>
<p>the ID of the user is: $userid</p> <!--I want userid to be displayed here-->
<p>welcome to My Shopping Page</p>
</body>
</html>";
exit;
}
}
fclose($fh);
} else {
echo "error";
exit;
}
echo "access not granted";
?>
首先,您使用的是变量
$theData
,而不是变量$line
。另外,fgets
不会去除包括换行符在内的空白字符,因此需要使用trim
。试试这个,看看它是否有效:
if (trim($userid) == trim($line)) {
您还需要删除
$theData=fgets($fh)
因为它正在获取第一行,并且不会使用上述逻辑进行检查。首先,您使用的是变量$theData
,而不是变量$line
。另外,fgets
不会去除包括换行符在内的空白字符,因此需要使用trim
。试试这个,看看它是否有效:
if (trim($userid) == trim($line)) {
您还需要删除
$theData=fgets($fh)代码>因为它正在获取第一行,并且不会使用上面的逻辑进行检查。实际上其中有一些隐藏字符,如换行符和回车符如果($userid==trim($theData))
使用DB会容易得多。在循环中,您将fgets返回到$line
,然后对照$theData
检查$userid
。这就是你想做的吗?他在Don'tPanic上做了两次fgets
,但你是对的-他应该使用$line
,因为这表示循环中的每一行trim($line)
实际上其中有一些隐藏字符,比如换行符和回车符如果($userid==trim($theData))
使用DB会容易得多。在循环中,您将fgets返回到$line
,然后对照$theData
检查$userid
。这就是你想做的吗?他在Don'tPanic上做了两次fgets
,但你是对的-他应该使用$line
,因为这表示循环中的每一行<代码>修剪($line)
还需要删除$theData=fgets($fh)
否则它将永远不会与名字匹配。好眼力(“;.;”)
还需要删除$theData=fgets($fh)
否则它将永远不会匹配名字。好眼力(“;.;”)
if (trim($userid) == trim($line)) {