PHP Simple Chat Room (with source code) page 1/2

Source: Internet
Author: User

I. Implementation of the chat room Module
1. Set the main page of the chat room

Copy codeThe Code is as follows: <meta http-equiv = "Content-Type" content = "text/html; charset = gb2312"/>
<! -- Load the configuration file -->
<? Php include_once 'config. php';?>
<! -- Page title -->
<Title> <? Php echo CHAT_NAME;?> </Title>
<Script language = "javascript" src = "main. js"> </script>
<Frameset rows = "80, *, 100" cols = "*" frameborder = "yes" border = "1" framespacing = "0">
<! -- Top window -->
<Frame src = "top. php" name = "topFrame" scrolling = "No" noresize = "noresize" id = "topFrame" title = "topFrame"/>
<Frameset rows = "*" cols = "168, *" framespacing = "0" frameborder = "yes" border = "1">
<! -- Left window -->
<Frame src = "list. php" name = "leftFrame" scrolling = "No" id = "leftFrame" title = "leftFrame"/>
<Frameset rows = "70%, *" cols = "*" framespacing = "0" frameborder = "yes" border = "1">
<! -- Public chat window -->
<Frame src = "center. php" scrolling = "auto" name = "mainFrame" noresize = "noresize" id = "mainFrame" title = "mainFrame"/>
<! -- Private chat window -->
<Frame src = "private. php" scrolling = "auto" name = "priFrame" id = "priFrame" title = "priFrame"/>
</Frameset>
</Frameset>
<! -- Speech window -->
<Frame src = "talk. php" name = "bottomFrame" scrolling = "No" noresize = "noresize" id = "bottomsFrame" title = "bottomFrame"/>
</Frameset>
<Noframes>
<Body> </body>
</Noframes>

2. file operation functions
This chat room module uses text to save user lists and speeches. It mainly considers that file operations are faster than database operations.

(1) fopen () function. Open a file or URL. If opening fails, the function returns FALSE.
Syntax: fopen (filename, mode, include_path, context)
Parameter description:
Filename:
Required. specifies the file or URL to be opened.
Mode:Required: Specifies the access type of the file/stream.
Include_path:Optional. You can set this parameter to 1 or TRUE if you also need to retrieve files in include_path.
Context:Optional. Specifies the file handle environment. context is a set of options for modifying the stream format.
Value of the mode parameter:
"R": open in read-only mode. Point the file pointer to the file header.
"R +": open in read/write mode. Point the file pointer to the file header.
"W": open in writing mode. Point the file pointer to the file header and cut the file size to zero. If the file does not exist, try to create it.
"W +": open in read/write mode. Point the file pointer to the file header and cut the file size to zero. If the file does not exist, try to create it.
"A": open in writing mode. Point the file pointer to the end of the file. If the file does not exist, try to create it.
"A +": open in read/write mode. Point the file pointer to the end of the file. If the file does not exist, try to create it. "X": it is created and opened in writing mode. The file Pointer Points to the file header. If the file already exists, fopen () fails to be called, returns FALSE, and generates an E_WARNING-level error message. If the file does not exist, try to create it. "X +": create a file and open it in read/write mode. Point the file pointer to the file header. If the file already exists, fopen () fails to be called, returns FALSE, and generates an E_WARNING-level error message. If the file does not exist, try to create it.

(2) fclose () function to close open files
Syntax: fclose (file)
Parameter: file: required. It specifies the file to be closed. The file parameter is a file pointer, And the fclose () function closes the file pointed to by the pointer. The file pointer must be valid and be successfully opened through fopen () or fsockopen. If the call succeeds, true is returned. Otherwise, false is returned.

(3) reading file functions
********
The fgets (file, length) function is used to read a row of data at a time.
File: required. specifies the file to be read.
Length: (optional) number of bytes to be read. The default value is 1024 bytes. Read a row from the file and return a string with a maximum length of 1 bytes. When a line break (including the returned value), EOF, or length-1 byte is read, it is stopped. If no length is specified, the default value is 1 K, or 1024 bytes. If it fails, false is returned.
********
File (path, include_path, context), returns the file as an array. Each unit in the array is a corresponding line in the file, including line breaks. If it fails, false is returned.
Path: required. specifies the file to be read.
Include_path: Optional. If you want to search for a file in include_path, set this parameter to 1.
Context: Optional. Specifies the file handle environment. Context is a set of options that can modify the behavior of a stream. If null is used, it is ignored.
********
File_get_contents (path, include_path, context, start, max_length) is the preferred method for reading the file content into a string.
Path: required. specifies the file to be read.
Include_path: Optional. If you want to search for files in include_path, you can set this parameter to "1 ".
Context: (optional) specifies the file handle environment. Context is a set of options that can modify the behavior of a stream. If null is used, this parameter is ignored.
Start: (optional) specifies the position in the file to start reading.
Max_length: (optional) number of bytes to be read.

(4) file Writing Function
Writing data is also a common file operation in PHP. in PHP, you can use the fwrite () and fputs () functions to write data to the file. The fputs () function is the alias of the fwrite () function. They are used in the same way.
Syntax: fwrite (file, string, length)
Parameter description:
File: required. It specifies the open file to be written.
String: required. specifies the string of the file to be written.
Length: Optional. Specifies the maximum number of bytes to write.
Fwrite () writes the string content to the file pointer. If length is specified, the write will stop after the length is written to a byte or the string is written. Write () returns the number of written bytes. If an error occurs, false is returned.

(5) delete an object
The unlink (filename, context) function is used to delete an object. The object must be closed. If the object succeeds, true is returned. If the object fails, false is returned.
Filename: required. specifies the file to be deleted;
Context: (optional) specifies the file handle environment. Context is a set of options for modifying stream behavior.

3. Regularly refresh the technology to delete non-speaking users
Scheduled refresh means that objects can be reloaded once or only once at intervals. This function can be easily implemented using javascript scripts.
(1) setInterval () function
The setInterval () function in JavaScript can perform an operation at intervals. The syntax of this function is as follows:
Window. setInterval (Function, MilliSeconds)
The Parameter Function indicates the operation to be executed. It can be a Function.
The MiliSeconds parameter indicates the interval, in milliseconds.
After the setInterval () function is used, the function will continue to be executed as long as the page is not closed. To end the event, you can use the clearInterval () function. The function format is as follows:
Window. clearInterval (Interval)
The parameter Interval is the return value of the setIntervald () function.

(2) setTimeout () function
The setTimeout () and setInterval () functions are used in the same way, but the setTimeout () function is only executed once. Deletes users who do not speak in a chat room.

4. scroll bar locating technology during screen scrolling
The scrolling function in the chat room. The latest speech information is displayed at the bottom of the page, and the page focus is located at the top of each refresh. The positioning of a scroll bar can be divided into the scroll bar of the entire page window and the scroll bar in the page element.

(1) the scroll bar in the window
You can use the scroll () function to locate the scroll bar in the window.
Syntax: window. scroll (x, y)
The x parameter represents the coordinates of the horizontal position of the scroll bar. The y parameter represents the coordinates of the vertical position of the scroll bar. If you want to set the scroll bar to the bottom, you only need to set the value of y to a greater value.

Copy codeThe Code is as follows: <script language = "javascript"> // locate the scroll bar
Function scrollWindow (){
This. scroll (); // sets the coordinates of the scroll bar.
SetInterval ('rollwindow () ', 200); // execute the aggregate function every 200 milliseconds.
}
ScrollWindow ();
</Script>

(2) the scroll bar in the page Element
You can use the scrollTop attribute to locate the scroll bar in the page element. The scrollTop attribute indicates the distance between the vertex of the current object and its outermost object element. Syntax:
Object. scrollTop = distance;
This attribute is used to locate the scroll bar in the public window of the chat room. The public window uses a div layer to display chat information. First, set the vertical scroll bar of the div layer to automatic, and the horizontal scroll bar to non-Parallel Automatic line feed.Copy codeThe Code is as follows: <div id = "publist" style = "width: 800px; height: 220px; overflow-x: hidden; overflow-y: auto; work-break: break-all; word-wrap: break-word; line-height: 20px; ">... </div>

Set the scrollTop attribute of the vertical scroll bar in Javascript to the scroll height (locate at the bottom of the chat window)Copy codeThe Code is as follows: <script language = "javascript">
// Locate the scroll bar
Function scrollWindow (){
Document. getElementById ('publist'). scrollTop = document. getElementById ('publist'). scrollHeight;
SetTimeout ('rollwindow () ', 200 );
}
</Script>

5. Block Refresh Technology
Blocking refresh means shielding the <F5> key, shielding the right mouse, and hiding the "refresh" button in the browser. Each part uses different technologies.

(1) Hide the "refresh" button
The open () function is used to open a new window and set the window style.
The syntax format of the Open () function is as follows:
Op = window. open (

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.