【发布时间】:2020-02-17 04:17:15
【问题描述】:
我是 php 新手,我创建了一个网页来输入用户名和密码,然后将其与存储的列表进行比较以授予或拒绝访问该站点。我的代码可以正常运行,但我不断收到未定义的偏移错误。
<html lang="en">
<head>
<title>Insecure System</title>
</head>
<body>
<?php
// read file and set text to string
$myFile = fopen("includes/users.txt","r");
$content = fread($myFile,filesize("includes/users.txt"));
fclose($myFile);
// set string to two dimensional array
$contentArray = explode("||>><<||",$content);
foreach ($contentArray as $cArr) {
$contentArray2[] = explode(",",$cArr);
}
//print_r($contentArray2);
// retrieve user input values
if (isset($_POST['user']) && isset($_POST['pass'])) {
$enteredUser = $_POST['user'];
$enteredPass = $_POST['pass'];
// if statements to check user/pass combo
for ($i=0;$i<=sizeof($contentArray2);$i++) {
for ($i2=0;$i2<=sizeof($contentArray2[$i]);$i2++) {
if ($contentArray2[$i][$i2] == $enteredUser) {
$i2++;
if ($contentArray2[$i][$i2] == $enteredPass) {
echo "Access Granted";
break 2;
}
else {
echo "Access Denied";
break 2;
}
}
else continue;
}
}
}
?>
<form action="index.php" method="post">
<input type="text" placeholder="Username" name="user">
<input type="text" placeholder="Password" name="pass">
<input type="submit" value="Submit">
<input type="reset" value="Reset">
</form>
</body>
</html>
users.txt 的内容:
beavis,password||>><<||butthead,password2||>><<||dana,alien||>><<||fox,believe
产生的错误输出:未定义的偏移量:第 29 行 index.php 中的 2
第29行指代码中的这一行:if ($contentArray2[$i][$i2] == $enteredUser) {
$contentArray2 的值:Array ( [0] => Array ( [0] => beavis [1] => password ) [1] => Array ( [0] => butthead [1] => password2 ) [2] => Array ( [0] => dana [1] => alien ) [3] => Array ( [0] => fox [1] => believe ) )
【问题讨论】:
-
请打印 $contentArray2 的值
-
$i<=sizeof($contentArray2)和$i2<=sizeof($contentArray2[$i])应该是$i<sizeof($contentArray2)和$i2<sizeof($contentArray2[$i]) -
@SterlingH:不用担心 :)
-
您是否告诉 Beavis 和 Butthead 您将他们的密码以纯文本形式存储在 txt 文件中?所有这些关于密码泄露的新闻报道通常只是泄露密码的“散列”。但是您将其作为纯文本存储在任何人都可以访问的 txt 文件中。想想你在做什么。如果您在欧洲,如果发现这一点,GDPR 将给您带来很多罚款。
-
@Andreas,网页的标题可能会泄露一点
<title>Insecure System</title>
标签: php html arrays multidimensional-array