Page 1 of 1

PHP Code Problem (Fatal Error)

Posted: Tue Sep 13, 2005 3:02 pm
by youngloopy
Can anyone look at this code and tell my what could be wrong?

edit.php

Code: Select all

<?php 
session_start(); 

if($_SESSION['logged'] != 1) { 
exit("Please login"); 
} 

while(true) { 
if(isset($_POST['edit'])) { 

$fs = fopen("events.txt", "w"); 
if(fwrite($fs, $_POST['text'])) { 
echo "Saved"; 
break; 
} else { 
echo "Could not be saved"; 
break; 
} 
fclose($fs); 
break; 

} 
} 

?> 

<html> 
<head> 
</head> 
<body> 
<form action="<?php $_SERVER['PHP_SELF']  ?>" method="post"> 
<textarea name="text"> 
<?php 
$text = file_get_contents("events.txt"); 
echo $text 
?> 
</textarea> 
<input type="submit" name="edit" value="Edit"> 
</form> 
</body> 
</html>
this is accessed by a login.php which works fine, I want this code to open the events.txt file and allow the user to edit it and then save back to the server.
Here is a link, http://www.nucklewebdesign.com/dloads/ I put all three on a dloads page if you want to see what happens. Username = admin , Password = 777777 , I get a Fatal Error for some kind of time out on line 9.
Thanks,
Josh

Posted: Tue Sep 13, 2005 4:00 pm
by Ambush Commander
Please use php tags.

Code: Select all

while(true) {
  if(isset($_POST['edit'])) {
    $fs = fopen("events.txt", "w");
    if(fwrite($fs, $_POST['text'])) {
      echo "Saved";
      break;
    } else {
      echo "Could not be saved";
      break;
    }
  fclose($fs);
  break;

  }
}
Think carefully. What happens if $_POST is not set? Why do you have a while loop on the value true (effectively infinite if you don't break it).

Posted: Wed Sep 14, 2005 8:25 am
by youngloopy
Here is my code now, still getting the same Fatal Error on the same line 9 I must be lost cause I don't see it!

Code: Select all

<?php 
session_start(); 

if($_SESSION['logged'] != 1) { 
exit("Please login"); 
} 

while(true) { 
  if(isset($_POST['edit'])) { 
    $fs = fopen("events.txt", "w"); 
    if(fwrite($fs, $_POST['text'])) { 
      echo "Saved"; 
      break; 
    } else { 
      echo "Could not be saved"; 
      break; 
    } 
  fclose($fs); 
  break; 

  } 
}

?> 

<html> 
<head> 
</head> 
<body> 
<form action="<?php $_SERVER['PHP_SELF']  ?>" method="post"> 
<textarea name="text"> 
<?php 
$text = file_get_contents("events.txt"); 
echo $text 
?> 
</textarea> 
<input type="submit" name="edit" value="Edit"> 
</form> 
</body> 
</html>

Posted: Wed Sep 14, 2005 8:40 am
by shiznatix
if $_POST[] is not set

if(isset($_POST['edit'])) {

if that retuns false then your fclose($fp); might be the problem becuase you can't close somthing that is not open! and if post['edit'] is not set then it will never open anything thats maybe where you get a fatal error but maybe not

Posted: Wed Sep 14, 2005 8:45 am
by raghavan20

Code: Select all

while(true) { 
  if(isset($_POST['edit'])) { 
    $fs = fopen("events.txt", "w"); 
    if(fwrite($fs, $_POST['text'])) { 
      echo "Saved"; 
      break; 
    } else { 
      echo "Could not be saved"; 
      break; 
    } 
  fclose($fs); 
  break; 

  } 
}
I still do not see you remove the while(true) which obviously keeps on getting $_POST variable and continuously writing into the file.
i think the file should be flooded.

I dont think using exit() is a good idea rather you can force a redirection which is user-friendly.

Posted: Wed Sep 14, 2005 8:46 am
by youngloopy
I have reached my peak of PHP knowledge on this one, do you have any suggestions cause I am completly stumped!

Posted: Wed Sep 14, 2005 9:04 am
by raghavan20
try this

Code: Select all

<?php 
session_start(); 

if($_SESSION["logged"] != 1) { 
exit("Please login"); 
} 


if(isset($_POST["edit"])) { 
	echo "size of the old file:".filesize("events.txt");
	unlink("events.txt");
	$fs = fopen("events.txt", "a+"); 
	if(fwrite($fs, $_POST['text'])) { 
		echo "Saved"; 
		break; 
	}else { 
		echo "Could not be saved"; 
		break; 
	} 
	fclose($fs); 
	break; 
} 

?> 

<html> 
<head> 
</head> 
<body> 
<form action="<?php $_SERVER['PHP_SELF']  ?>" method="post"> 
<textarea name="text"> 
<?php 
	$text = file_get_contents("events.txt"); 
	echo "size of the file:".filesize("events.txt");
	echo "Contents of the file:<br />".$text;
?> 
</textarea> 
<input type="submit" name="edit" value="Edit"> 
</form> 
</body> 
</html>