
Originally Posted by
Crazykld69
I dont think I am. However, I may have to close to txt file. i think that may be the problem:
PHP Code:
<?php
$file_name = 'chat.txt';
if(isset($_POST['send'])){
if($_POST['message'] != NULL && $_POST['message'] != ''){
$current = '<div class="box"><div class="name">Something</div><div class="message">'.$_POST['message'].'</div></div>';
$current .= file_get_contents($file_name);
file_put_contents($file_name, $current);
}
}
I know this isn't part of your original question, but this code is vulnerable to XSS attacks. Let me know if you would like me to explain.

Originally Posted by
Crazykld69
It does change but lets say if i open two pages and do one of these the other doesnt change.
Right - that is as expected. Let me explain what I meant earlier:
PHP Code:
<html>
<body>
<p id="demo"></p>
<script>
setInterval(document.getElementById("demo").innerHTML='<?php echo file_get_contents("chat.txt") ?>',1000);
</script>
</body>
</html>
This is a PHP script. When it runs, it gets the contents of your chat.txt file, as a string. The script's output will look something like this:
HTML Code:
<html>
<body>
<p id="demo"></p>
<script>
setInterval(document.getElementById("demo").innerHTML='<div class="box"><div class="name">Something</div><div class="message">Message #1</div></div>
<div class="box"><div class="name">Something</div><div class="message">Message #2</div></div>
<div class="box"><div class="name">Something</div><div class="message">Message #3</div></div>
<div class="box"><div class="name">Something</div><div class="message">Message #4</div></div>',1000);
</script>
</body>
</html>
Once it gets to the browser, the javascript will run, and add that string to your p#demo. However, there's no point in doing it again, since the contents of the paragraph will be replaced by the same string.
The "one page" (the page where you submit the new comment) is updated because it is reloaded after you submit the form.
- - - - -- - - - -- - - - -- - - - -- - - - -
If you used ajax instead, you could get the *current* version of the chat.txt file. For example:
Code:
setInterval(
function(){
var xmlhttp = window.XMLHttpRequest? new XMLHttpRequest(): new ActiveXObject( 'Microsoft.XMLHTTP' );
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
document.getElementById("demo").innerHTML = xmlhttp.responseText;
}
}
xmlhttp.open("GET", "/path/to/chat.txt", true);
xmlhttp.send();
}
,1000
);
consider, however, that if you have a significant number of users and a typical shared hosting setup, you could quickly shut your own site down with so frequent requests.
Bookmarks