简体   繁体   English

如果声明不起作用,但是有点起作用

[英]If statement not working, but kinda working

I made a php script to see if what the contents of a txt file are the same as the users input. 我制作了一个php脚本,以查看txt文件的内容是否与用户输入的内容相同。 I wrote an if statement to see if it is correct and if it isn't send it to an else. 我编写了一个if语句,以查看它是否正确以及是否没有将其发送给else。 If i dont send any variables, then it works, and displays 如果我不发送任何变量,则它可以工作并显示

a is b is file is 1

but if i enter the file and the correct contents it displays 但是如果我输入文件并显示正确的内容

a is test.txt b is hello file is hello 0

Here is my code 这是我的代码

<?php
session_destroy();
$a=htmlspecialchars($_REQUEST['a']);
$b=htmlspecialchars($_REQUEST['b']);
$my_file = file_get_contents($a);
echo "a is $a\n";
echo "b is $b\n";
echo "file is $my_file\n";
if ( $my_file == $b ) {
echo "1";
}else {
echo "0";
}
?>

Any ideas? 有任何想法吗?

These are un-initialized variables, so if you enter nothing, then you're getting if (false == false) which is true. 这些是未初始化的变量,因此,如果您不输入任何内容,那么您将获得true(false == false)。

This is because php interprets null as being false. 这是因为php将null解释为false。 It's weird, I acknowledge. 我承认这很奇怪。

Either check to see if your variables have been set, or initialize them with defaults. 检查是否已设置变量,或使用默认值对其进行初始化。

为了安全起见,请尝试在从文件读取的内容中使用htmlspecialchars函数,然后在从文件读取的输入和用户输入中也使用trim

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM