Я новичок в 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
Вывод результирующей ошибки: Не определеносмещение: 2 в index.php в строке 29
Строка 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 ) )