Just started coding php, so dont laugh :p cant get past this foreach error to debug the other code, any ideas? PHP Warning: Invalid argument supplied for foreach() in /var/blah/parse.php on line 20
<?php
//read file in and store in array for parsing
$log_file = '/var/www/html/kylehguild/access_list.txt';
$fp = fopen($log_file, "r");
$contents = fread($fp, filesize($log_file));
fclose($fp);
echo "File read and stored";
//set file that contains only the username and passwords for later, at which time will be read in and compared
$temp_file = '/var/www/html/kylehguild/temp_access.txt';
//parse each line in $contents, find spaces and assign their position to variables
$space1 = 0;
$space2 = 0;
$space3 = 0;
$space4 = 0;
$space5 = 0;
foreach ($contents as $value) {
$start_position = 0;
$end_value = strlen($value)+1;
for ( $i=0; $i <= $end_value; $i += 1) {
$current_char = substr($value, $start_position, 1);
if ($current_char == " ") {
if ($space1 == "0") {
$space1 = $start_position;
}
else if ($space2 == "0") {
$space2 = $start_position;
}
else if ($space3 == "0") {
$space3 = $start_position;
}
else if ($space4 == "0") {
$space4 = $start_position;
}
else if ($space5 =="0") {
$space5 = $start_position;
}
else {
$start_position = $start_position + 1;
}
}
echo "all spaces found, writing IP, Username";
$name = substr($value, $space2+1, $space3-$space2);
$ip_addy = substr($value, $space5+1, $end_value - $space5);
$fp = fopen($temp_file, "a");
fwrite($fp, $name + " " + $ip_addy);
fclose($fp);
}
echo "All done";
}
?>