Re: Writing data to HTML file BEFORE the </body> tag.



..oO(Shawn)

I have the following code in a PHP file. An HTML form passes user
comment data to the PHP, which then appends the user comments to the
end of the HTML file on which the form is located. This PHP code
works: the HTML file with added comments displays correctly in my
browser. However, appending text to the very end of the HTML file
creates what is, strictly speaking, invalid code.

I am looking for a way to tell PHP to write data to the file JUST
BEFORE the </body> tag. I have read about fseek(), but don't know for
sure if the number of characters (or HTML tags) after my "user
comments" section is going to remain constant.

You want to insert data into the middle of a file. This means you have
to recreate the entire file. Usually you would read it into memory,
write all of its data to a new empty file until you reach the insert
position, then write the new data, then the rest of the original file.
After that you replace the old file with the new one.

You could do this with file() and looping through the resulting array
until you reach the "</body>" line. Another way would be to load the
entire file with file_get_contents() into a string. Then use string
functions to prepend "</body>" with your new content, finally use
file_put_contents() to write it all back to disk.

But there are a lot of other problems:

<HTML>
<HEAD></HEAD>

This code is invalid anyway. There's no document type declaration and
the 'title' element is missing.

<BODY>
<?

Don't use short open tags. They are unreliable and will be turned off by
default in the coming PHP 6. Use the correct <?php instead.

$name = $_POST['name'];
$website = $_POST['website'];
$message = $_POST['message'];
$timestamp = $_POST['timestamp'];

No error checking that these $_POST values really exist?

$fp = fopen(basename($_SERVER[HTTP_REFERER]), 'a');

Holy sh*t... The HTTP referrer is not only totally unreliable, but also
easy to fake. This opens a _huge_ security hole here - an attacker could
easily manipulate _any_ file your web server is allowed to write to and
inject arbitrary code!

Have a look at the various predefined values in $_SERVER instead, the
elements 'SCRIPT_NAME' or 'PHP_SELF' could be of interest.

if (!$fp)
{
echo "There was an error. Please try again later.";
exit;

The exit call here will prevent the script from returning a complete
HTML document to the browser. In case of an error you should just stop
or skip the further file processing, but not kill the entire script.

}
else
{
$outputstring = "<hr>" .$timestamp. "<br>" .$name. "<br>" .$message.
";

You should also have a look at htmlspecialchars(). Your code allows a
user to insert arbitrary markup, which means that your page can be
abused for cross-site scripting attacks (XSS). Even worse: it also
allows easy code injection - the most severe of all security problems.

My suggestion: Drop the idea of a self-modifying script - this calls for
a lot of _serious_ trouble! Instead write the posted messages to another
file (plain text or CSV for example) or to a database. Then use a little
load function to show these messages on your page.

Micha
.



Relevant Pages

  • Re: Writing to a server text file
    ... That way php will know that there is something for it to do ... the script will be ignored in an html file ... Etrust/Vet/CA.online Antivirus scan http://www3.ca.com/securityadvisor/virusinfo/scan.aspx ... Or could there be something else wrong with my script? ...
    (php.general)
  • Re: HTML/PHP include files
    ... not sure if this is the problem but your two php files are different: ... see if its the problem by copying your includes dir to the jennifer dir and ... or use the absolute paths above(make sure to delete jennifer/includes so you ... I thought that I could include an html file from a php statement ...
    (alt.html)
  • Re: Can PHP be used to write permanent file changes?
    ... perl, php, cgi or something else so I was wondering what you would ... write the new html file on the server? ... Something to process user submissions and store them ... PHP has no problem writing an HTML file ...
    (php.general)
  • Re: php requires ".php" not ".html" ?
    ... Is there anyone else reading here who thinks Jerry is correct? ... those who write .php files, as if a bloody file extension matters. ... server parse an .html file for .php it places a greater work load on ... the server more so than if the server know if the file was a .php When you ...
    (comp.lang.php)
  • Re: Change included code
    ... Index.php is basically an html file uses a linked css file and when its included in the new file its referencing a css file in the wrong spot. ... It would actually be a bit more trouble since any time I want to upload to the server to test I will have to change the link. ... Would be easier just to have php just add "../" to one spot instead manually doing it every time I upload to the server. ...
    (comp.lang.php)