Jump to content

Archived

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

tomfmason

Editing a static html file

Recommended Posts

I am creating a live support chat script and am running into a little trouble.

The way that I decided to do this was to create a static html page and use fwrite to update the new message. The samples below are very simple.

[b]The chat login[/b]

[code=php:0]<?php
if (empty($nick)) {
   $error = "nick";
}
if (isset($nick)) {
include('db.php');
$sql = mysql_query("INSERT INTO chat(nick) VALUES('$nick')") or die (mysql_error());

if (!$sql) {
    $error = "sql";
}
$filename = "$nick.html";
$handle = fopen($filename, "x+b");
$body = "
<html>
<head><meta http-equiv=\"refresh\" content=\"8\">
<meta name=\"robots\" content=\"noindex\"></head>
<body bgcolor=\"#CCCCCC\" text=\"#000000\">
<p>
Welcome $nick, an operator will be with you shortly.
</p>
<!--Start of chat-->
<!--end of chat-->
<font color=\"#000000\">&copy; OWPT 2006</font>
</p>
</body>

</html>";
fwrite($handle, $body);
fclose($handle);
include('chat.php');
}   
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Untitled Document</title>
</head>

<body>
<p><h3>Please Enter your nick name</h3></p>
<?php
if ($error == "nick") {
    echo "You did not enter a nick name";
}
if ($error == "sql") {
    echo "Something when wrong with the MySQL query.";
}
?>
<form action="enter_chat.php" method="post">
<input type="text" name="nick" size="20">
<input type="submit" name="submit" value="Submit">
</form>

</body>
</html>[/code]

This creates a static html file with the name of there nick. I plan on using the chat id as well. Any suggestions as to do this would be great. For example, the generated page would be named [b]$nick.$chat_id.html[/b] So far this works fine. It creates the html file with out any problems.

The problem is with the chat.php. It loads just fine but when I try to enter a new message, I get a 403 access denied error. I know that I have the permissions set properly on my server but I guess that I am not setting them right in the script.

[b]The Chat.php[/b]

[code=php:0]<?php
if (isset($submit)) {
   if (empty($message)) {
       $error == "messge";
   }   
$file = "$nick.html";
$fp = fopen($file, 'r+b'); 
$newline = "$nick: $message<br> ";

while (!feof($fp)){
    $line = trim(fgets($fp, 1024));
    if ($line == '<!--Start of chat-->'){
        $fpos = ftell($fp); 
        $rest = fread($fp, filesize($file));

        fseek($fp, $fpos, SEEK_SET); 
        fwrite($fp, "\n\n");
        fwrite($fp, $newline); 
        fwrite($fp, $rest);
    }
}
fclose($fp);
}
?> 
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Test chat</title>
</head>

<body>
<p>
<iframe name="chat_box" width="550" height="250" src="<?php echo "$nick" ?>.html">
Your browser does not support inline frames or is currently configured not to display inline frames.
</iframe></p>
<form action="chat.php" method="post">
<?php
if ($error == "message") {
    echo "You did not enter a message";
}
?>
<p>Enter your message</p>
<p>
<input type="text" name="message">
<input type="submit" name="submit" value="send">
</p>
</form>


</body>
</html>[/code]

I am pretty sure that I have not set the permission properly when opening the file. Any suggestions as to what I am doing wrong or suggetions as to a better way to do this would be great.

Thanks,
Tom


Share this post


Link to post
Share on other sites
Ok I don't think that explained the error that I was having properly. I get the 403 error that the file is not read able.

[code]Access forbidden!
You don't have permission to access the requested object. It is either read-protected or not readable by the server.[/code]

This is from the chat.php I open the file with the r+ command. I will post the relivant piece of script again.

[code=php:0]<?php
if (isset($submit)) {
  if (empty($message)) {
      $error == "messge";
  } 
$file = "$nick.html";
$fp = fopen($file, 'r+b'); 
$newline = "$nick: $message<br> ";

while (!feof($fp)){
    $line = trim(fgets($fp, 1024));
    if ($line == '<!--Start of chat-->'){
        $fpos = ftell($fp); 
        $rest = fread($fp, filesize($file));

        fseek($fp, $fpos, SEEK_SET); 
        fwrite($fp, "\n\n");
        fwrite($fp, $newline); 
        fwrite($fp, $rest);
    }
}
fclose($fp);
}
?>[/code]

I think that I am going to set up another test and try posting to a seperate file then using include or header to bring them back to the chat.php or maybe have two frames. One with the $nick.htm and one with the message submit form. If I get it figured out I will post the fix.

In the mean time any suggestions would be great.

Thanks,
Tom

Share this post


Link to post
Share on other sites
I am now sure that it is the way that I calling to open the file. When I login and create the static html file I am redirected to the chat.php and it shows the $nick.html inside the frame but when I add a new message that is when I get the error that the file is not readable. Not sure what the problem is but I am officaly lost.

I tried createing another file called add_message.php and posted the new message to it then included the chat.php again. I got the same result. So I now that There is a problem with the way that I am calling the file to be opened. Also it is not writting the new message to the file.

Share this post


Link to post
Share on other sites
I looked at the source and it is not passing the $nick variable around the second time. here is the source code for chat.php after the a new message has been submited.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Test chat</title>
</head>

<body>
<p>
<iframe name="chat_box" width="550" height="250" src=".html">
Your browser does not support inline frames or is currently configured not to display inline frames.
</iframe></p>
<form action="chat.php" method="post">
<p>Enter your message</p>
<p>
<input type="text" name="message">
<input type="submit" name="submit" value="send">
</p>
</form>


</body>
</html>

Any ideas why the $nick variable is not being passed the second time.

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.