Basic PHP Chat Tutorial
- Verify that your website is hosted on a server that supports PHP. Not all servers do, and some hosting providers limit access to the PHP compiler. Without this access, you cannot code PHP sites.
- Create a simple HTML form that will accept the user information for each chat message. All that is really necessary is the name and message of the chat user. This form can be small with no extra user input, if desired. Output the form to the name of the PHP file that will process the chat message. This file name will go in the "action" field of the form tag.
- Program the PHP script to process the HTML chat message information. The script should save all the HTML form variables to a separate text file. Then, the file should be read by the script and its contents returned to the user's browser. In this way, the chat conversation will be presented in its entirety with the new chat message appended.
The file can be read using the PHP file() command. Read the text into an array for easier processing. Save the HTML form data to the same file using PHP file management commands, including fopen(), fputs(), and fclose(). The variables in these commands will be identical to the variables used in the HTML form. Set the PHP script to autorefresh so it automatically receives new chat messages without any intervention from the server.
Create a blank text file with the same name as the file manipulated by the PHP file commands.
Finally, create a second HTML form that acts as a frameset to house the chat entry form and the PHP script output. This way, the user form and chat messages will appear on one web page. - Upload all four files to the Web server. This includes the HTML form, the PHP script, the HTML frameset and blank text file that holds the chat conversation.
Visit the URL of the HTML frameset and verify that two sections appear on the page. One area is for typing the chat message, and the other is for viewing the chat conversation.
Test the HTML form to ensure that data is routed properly, and if not, double check all variable names and file names to ensure consistency across all the documents.
Server Requirements
HTML Form
PHP Script
Upload and Test
Source...