PHP Code Problem (Fatal Error)

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
youngloopy
Forum Newbie
Posts: 9
Joined: Fri Mar 04, 2005 9:07 am
Location: NC
Contact:

PHP Code Problem (Fatal Error)

Post 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
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post 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).
youngloopy
Forum Newbie
Posts: 9
Joined: Fri Mar 04, 2005 9:07 am
Location: NC
Contact:

Post 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>
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Post 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
User avatar
raghavan20
DevNet Resident
Posts: 1451
Joined: Sat Jun 11, 2005 6:57 am
Location: London, UK
Contact:

Post 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.
youngloopy
Forum Newbie
Posts: 9
Joined: Fri Mar 04, 2005 9:07 am
Location: NC
Contact:

Post by youngloopy »

I have reached my peak of PHP knowledge on this one, do you have any suggestions cause I am completly stumped!
User avatar
raghavan20
DevNet Resident
Posts: 1451
Joined: Sat Jun 11, 2005 6:57 am
Location: London, UK
Contact:

Post 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>
Post Reply