Jump to content

file writing problems...


clonetrooper9494

Recommended Posts

I thought it would be fun to create a guest book that didn't use a SQL to store the entries... But when I tried to use the fopen function, and the fwrite function, it does work... Is there anything wrong with this code:

<?php$ip = $_SERVER['REMOTE_ADDR'];$name = $_POST['name'];$location = $_POST['location'];$comment = $_POST['comment'];$comment2 = str_replace("\n", "<br>", $comment);$file = file('http://clone-drone.net/home/downloads/projects/guest_book/example/data.txt');$errors = true;if($name != '' && $location != '' && $comment != ''){ for($i=1;$i<(count($file));$i+9) {  if($file[$i] != '$ip')  {   $errors = false;  } }}if($errors = false){$entrie = '<hr width="100%"><br><!--$ip-->By $name, from $location:$comment2<!-- --><!-- --><!-- -->';$f = fopen("http://clone-drone.net/home/downloads/projects/guest_book/example/data.txt","a");$write = fwrite($f,"$entrie");$close = fclose($f);}if($errors = true){echo '<meta http-equiv="refresh" content="0;url=http://clone-drone.net/home/downloads/projects/guest_book/example/">';}?>

Link to comment
Share on other sites

That's the right format. I'm not sure if it's the correct path, print the __FILE__ constant to see the current path:echo __FILE__;Also, remove all the other code and just have a script that opens and writes to the file until you get that working. At this point you're not sure if it's not opening the file correctly or if something else is the problem. Also make sure error messages are enabled.ini_set("display_errors", 1);error_reporting(E_ALL);

Link to comment
Share on other sites

I think I have found the problem... My IP checking loop doesn't work... it echos the meta refresh, so it somehow can't get past the loop.Can anybody see anything wrong with my loop??the contents of data.txt is 9 blank lines...

Link to comment
Share on other sites

First of all, how do you know the loop is even running? Have you checked the data in $file to see what it is? Your loop will either run once or forever, because of the last part. "$i+9" doesn't do anything.for($i=1;$i<(count($file));$i+9)You also don't want to start the loop at 1 (again, inspect at the $file variable).And about this part:if($file[$i] != '$ip')Why do people insist on quoting variables? It's this:if($file[$i] != $ip)This line:if($errors = false)is always setting $errors to false, it should be this:if($errors == false)likewise with this:if($errors = true)

Link to comment
Share on other sites

Just random additions:Your entrie variable assignment looks like you want to interpolate $ip, $name, $location, and $comment2. To do this, you need to put the r-value in double quotes or use heredoc format (see below).Also, judging from the fact that your for loop tests for equality between a whole file line and $ip, two things have to happen:1. Remember that file() returns an array of lines with the newline characters still attached. So if you're going to test for equality in your for loop, you need to use trim or rtrim to strip the newline off the end of $file[$i]; or more simply, test for strpos > -1 rather than simple equality.2. Looks like you want $entrie to contain all those line breaks between the quotes. For that to work you have to ditch the quotes in the r-value and definitely use heredoc format. (Look that up if you don't know it, and make sure you get the PHP syntax, because all unix-based scripts have their own variation, and you don't want to put a semi-colon in the wrong spot.)

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...