1

This is my php code, and i am trying to create a chat

<!DOCTYPE html>
<html>
<body>
<html>
<?php
session_start();
$userinput = $_GET["name"];
if (empty($userinput)){

} else {
    $myfile = fopen("chat.txt", "a");
    fwrite($myfile, $userinput);
    fwrite($myfile, "<br>
");
}

$chatfile = fopen("chat.txt", "r");
echo fread($chatfile,filesize("chat.txt"));
echo $chatfile;
?> 

</body>
</html>

<form method="get" id="myForm" action="">
<input type="text" name="name"
placeholder="type in your comment" autofocus>
<input type="submit">
</form>

<script>
</script>
</body>
</html>

I know that php isn't the way to go, but I want to have some fun, and it seems to be working out. Also, I want to ask how do you let the chat let real time. I created a document called chat.txt and maybe i can detect when it is changed, and then reload. There are 2 problems to this method. 1. after each reload the message you previously typed is typed again 2.I don't know how to detect change, and have not found any way to do this does anyone have solutions to this? (other methods would be fine) P.S. I'm horrible at php

MatthewProSkils
  • 364
  • 2
  • 13

2 Answers2

0

PHP is a weird way to do this. To be honest, the only way I can see a "live" chat in PHP is by refreshing constantly, and even then, the user won't be able to type. Something like this is difficult in PHP because PHP really just generates a page for you to view and it's done. It would be much easier for you to do this with javascript. You could make the JS look into the text file every x amount of seconds and if it detects a change, then make the page refresh, or even better, update the chat's HTML. It wouldn't be too hard:

Read the contents of a text file every 15 seconds

You'd just need to find a way, likely in javascript, to write something to file when you press a button. That way, the other client can get your messages and you won't need to refresh the page every time you want to send a message. Of course, this would only work with text files on local machines.

  • Ok Let me go check it out – MatthewProSkils Jun 08 '20 at 04:39
  • It seems it isn’t so easy to write to a file in Javascript, in that case, may be easier to use PHP to send a message, and in turn write to a file, but it’s annoying getting a browser refresh when you want to send a message (IMO), but reading a text file should be no problem. – GuyInABlueShirt Jun 08 '20 at 04:45
0

You should use database for messages, because every message must have insert date. Try sqlite for first time php.net/manual/en/sqlite3.installation.php In other (bad) way you can add every message in new file, every file has datetime create mark. To take only updates of chat the better way (and only one way with php) is use javascript. And jQuery library for start is best, chat is good practice to start use they toghether. https://www.w3schools.com/jquery/jquery_get_started.asp You can not get only updates with php, because php show all page entirely and no way to update only parth of html page, but javascript can. There are no normal sites written only on php without js.