PHP to implement a simple chat room (with source code) 1th/2 page _php tips

Source: Internet
Author: User
Tags setinterval

One, the chat room module realizes
1, Chat Room home page window settings

Copy Code code as follows:

<meta http-equiv= "Content-type" content= "text/html; charset=gb2312 "/>
<!--load 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 of <!---->
<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>
<!--speaking window-->
<frame src= "talk.php" name= "BottomFrame" scrolling= "No" noresize= "noresize" id= "Bottomsframe" title= "BottomFrame" />
</frameset>
<noframes>
<body></body>
</noframes>

2, file Operation function
This chat room module uses the text to save the user list and the speech information, mainly considers to the file operation in the speed to be superior to the database operation.

(1) fopen () function, open file or URL, and if open fails, function returns false
syntax: fopen (Filename,mode,include_path, Context)
parameter description:
FileName:
must, specify the file or URL you want to open
mode: must, specify the request to the file/ The access type of the stream.
include_path: Optional, you can set this argument to 1 or true if you also need to retrieve the file in Include_path.
context: optional. The environment in which the file handle is specified, the context is a set of options that can modify the text of the stream the
mode argument:
"R": Read-only open, pointing the file pointer to the file header.
r+: Read-write mode opens, pointing the file pointer to the file header.
W: Write open, point the file pointer to the file header, and truncate the file size to zero. Try to create if the file does not exist.
w+: Read-write mode opens, points the file pointer to the file header, and truncates the file size to zero. Try to create if the file does not exist.
A: Write as open, pointing the file pointer to the end of the file. Try to create if the file does not exist.
A +: Read-write opens, pointing the file pointer to the end of the file. Try to create if the file does not exist. ' x ': Created and opened as write, with the file pointer pointing to the file header. If the file already exists, the fopen () call fails and returns FALSE, and a e_warning level error message is generated. Try to create if the file does not exist. "x+": Create and read-write to open, pointing the file pointer to the file header. If the file already exists, the fopen () call fails and returns FALSE, and a e_warning level error message is generated. Try to create if the file does not exist.

(2) fclose () function, close open file
Syntax: fclose (file)
Parameters: File: Must, specify the files to be closed. The file parameter is a fclose () function that closes the file that the pointer points to. The file pointer must be valid and is a successfully opened file through fopen () or Fsockopen (). Returns true if successful, otherwise returns false.

(3) Read the file function
********
Fgets (file,length) function to read one row of data at a time
File: Must, specify the files to read
Length: Optional, specify the number of bytes to read, default is 1024 bytes. Reads a row from the file pointed to by files and returns a string with a maximum length of length-1 bytes. Stops when a newline character is encountered (including in the return value), eof, or length-1 bytes have been read. If length is not specified, the default is 1 K, or 1024 bytes. Returns False if the failure occurs.
********
File (Path,include_path,context), which returns files as an array, and each cell in the array is the corresponding line in the file, including line breaks. Returns False if the failure occurs.
Path: Must, specify the file to read.
Include_path: Optional. If you want to search for a file in include_path, set this argument to 1.
Context: Optional. The environment that prescribes the file handle. The context is a set of options that can modify the behavior of a stream, which is ignored if NULL is used.
********
File_get_contents (path,include_path,context,start,max_length) is the preferred method for reading the contents of a file into a string.
Path: Must, specify the file to read
Include_path: Optional, you can set this argument to "1" If you also want to search for files in include_path.
Context: Optional, specify the environment of the file handle. The context is a set of options that can modify the behavior of a stream. If NULL is used, it is ignored.
Start: Optionally, specify where to start reading in the file.
Max_length: Optionally, specify the number of bytes read.

(4) Write file function
Write data is also commonly used in PHP file operations, in PHP, you can use the fwrite () and fputs () function to write data to the file. The fputs () function is an alias to the fwrite () function. Their usage is the same.
Syntax: fwrite (file,string,length)
Parameter description:
File: Must, specify the open file to write to.
String: Must, specify the strings of the files to be written.
Length: Optional. Specify the maximum number of bytes to write.
Fwrite () writes the contents of a string to the file pointer files. If length is specified, the write stops when the length byte is written or after a string is written. Write () returns the number of bytes written and false when an error occurs.

(5) Delete files
The unlink (filename,context) function is used to delete a file, which must be closed, returns true if it succeeds, and false if it fails.
FileName: It is necessary to specify the files to be deleted;
Context: Optional, specify the file handle environment. The context is a set of options for modifying the behavior of a stream.

3, timed Refresh technology Delete not speakers users
A timed refresh is a time when the object is reloaded, or it can be loaded only once. This functionality can be easily implemented using JavaScript scripts.
(1) setinterval () function
The SetInterval () function in JavaScript can perform an operation every once in a while. The function syntax is formatted as follows:
Window.setinterval (Function,milliseconds)
The parameter function indicates the action to be performed, and can be a function.
The parameter miliseconds represents the time of the interval, in milliseconds.
With the SetInterval () function, the function is executed as long as the page is not closed. If you want 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 () function is the same as the SetInterval () function, but the settimeout () function executes only once. Used in chat rooms to regularly delete users who do not speak.

4, roll-bar positioning technology when scrolling display
In the chat room, the scrolling function, the latest speech information is displayed at the bottom of the page, and each refresh, the page focus is positioned to the top. The scroll bar positioning can be divided into scroll bars for the entire page window and scroll bars in the page elements.

(1) The scroll bar in the window
The scroll bars in the window can be positioned by using the scroll () function.
Syntax: Window.Scroll (x,y)
The parameter x represents the coordinate of the horizontal position of the scroll bar. The parameter y represents the coordinates of the scroll bar's vertical position. If you want to base the scroll bar, just set the value of Y to a larger number.

Copy Code code as follows:

<script language= "JavaScript" >//Positioning scroll bar
function ScrollWindow () {
This.scroll (0,75000); Set scroll bar coordinates
SetInterval (' ScrollWindow () ', 200); Perform a function every 200 milliseconds
}
ScrollWindow ();
</script>

(2) scroll bars in page elements
Scroll bars in page elements can be positioned using the ScrollTop property. The ScrollTop property represents the distance between the vertex of the current object and its outermost object element. The syntax is as follows:
object.scrolltop=distance;
The scrolling effect in the Chat Room public window is the property used to position the scroll bar. The public window uses a div layer to display the chat information. First, the DIV layer's vertical scroll bar is set to Automatic, and the horizontal scroll bar is set to none and wraps automatically.
Copy Code code 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>

Setting the ScrollTop property of a vertical scroll bar in JavaScript is equal to its scrolling height (navigates to the bottom of the chat window)
Copy Code code as follows:

<script language= "JavaScript" >
Positioning scroll Bars
function ScrollWindow () {
document.getElementById (' Publist '). scrolltop = document.getElementById (' publist '). scrollheight;
SetTimeout (' ScrollWindow () ', 200);
}
</script>

5, Shielding brush new technology
Screen refresh is the screen <F5> key, the right to screen the mouse and hide the browser "Refresh" button 3 part. Each section uses a different technique.

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

Current 1/2 page 12 Next read the full text

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.