Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 237
  • Last Modified:

what wrong with my file?


wat i trying to do here is .. reading content from a file and disply it.
and then get some input from the user and save it into file and display it.

this is the format of my record.txt

web1, www.web1.com // <--- one set of data
web2, www.web2.com 

my problem with my code is that...
i can't get my prog to write one set of data written line by line (one set of data per line)
but all my data will b written on one line even thought i have assigned "\n" to the back of my every set of data.

another prob
while using $_POST
whenever, i 'refresh' the screen, the data that has been post once (from submit-button), will b re-post again.
how can i prevent this from happening?

this is my code.


$my_array = localtime(time(), 1);
$month = $my_array["tm_mon"] + 1 ;
$day = $my_array["tm_mday"] ;
$year = $my_array["tm_year"] +1900 ;
$dayOfWeek = $my_array["tm_wday"] ;
$weekdays = array("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday") ;

echo "<h3>Current Time : $month-$day-$year > $weekdays[$dayOfWeek]<br></h3>" ;


$fs = fopen( 'C:\Program Files\EasyPHP1-7\www\url\record.txt', 'r' );
//$fd = fopen( 'filedestination', 'w' );

while( ! feof( $fs ) )
      $tmp = fgets( $fs );

      # replace this line with whatever you want to do to each line
      //$tmp = process_line( $tmp );
      $process = explode(",", $tmp);
      echo "<a href = \"$process[1]\"> $process[0]</a> <br>";

      //fputs( $fd, $tmp );

//fclose( $fd );
fclose( $fs );

echo "<br>";

echo "<FORM ACTION=\"url.php\" METHOD=POST>";

echo " Comment <br>";
echo "<input type=text name = \"comment\" size=\"25\" ><br>";
echo " Address <br>";
echo "<input type=text name = \"url_address\" size=\"25\" ><br>";
echo "<INPUT TYPE=submit NAME=submit_button VALUE=\"Submit\">";
echo "</FORM>";

echo " -----------------<br>";

$tmp_comment = "";
$tmp_url_address = "";
$tmp_comment = $_POST["comment"];
$tmp_url_address = $_POST["url_address"];

$fd = fopen( 'C:\Program Files\EasyPHP1-7\www\url\record.txt', 'a' );

$write_out = $tmp_comment.",".$tmp_url_address;

fwrite ($fd, "\n$write_out\n");

fclose( $fd );

  • 2
1 Solution
in answser to the reposting question (part 2),
you need to have the screen that you redisplay not be the instant result of the posted data (that's why hitting refresh makes the data get re-entered).  There are a few ways to do this.  Probably the most prevelant is
a header redirect
('location:' . $_SERVER['REQUEST_URI');

in answer to part 1, try using the function nl2br($text)
when you are displaying it for the user.... (assuming this is a web thing).  "\n" don't mean jack for HTML display.
tom_mkAuthor Commented:
('location:' . $_SERVER['REQUEST_URI');  >> localtion is my websaddress?
header('location:' . $_SERVER['REQUEST_URI]');

it's an internal PHP variable that reflects what page was asked for
if you just redirect to the same page, then they can't refresh (since there is no longer any POST data).

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now