PHP Implementation Simple chat room (with source) 1th/2 page _php Tutorial

Source: Internet
Author: User
One, the chat room module implementation
1, Chat Room home window settings
Copy CodeThe code is as follows:




<title><?php echo chat_name;?></title>

















&lt;noframes&gt;&amp;lt;BR&amp;gt;&amp;lt;body&amp;gt;&amp;lt;/body&amp;gt;&amp;lt;BR&amp;gt;&lt;/noframes&gt;

2, file Operation function
This chat room module uses text to save the user list and speech information, mainly considering that the operation of the file is faster than the operation of the database.

(1) fopen () function, open file or URL, if open fails, function returns false
Syntax: fopen (Filename,mode,include_path,context)
Parameter description:
FileName:
must, specify the file or URL to open
mode: must specify the type of access required to the file/stream.
include_path: Optional, you can set this parameter to 1 or true if you also need to retrieve the file in Include_path.
context: optional. Setting the context of a file handle, context is a set of options that can modify the flow's wording
Value of the mode parameter:
"R": Read-only open, pointing the file pointer to the file header.
"r+": Open read-write mode, point the file pointer to the file header.
"W": Write to open, point the file pointer to the file header and truncate the file size to zero. If the file does not exist, try to create it.
"w+": Read-write mode opens, pointing the file pointer to the file header and truncating the file size to zero. If the file does not exist, try to create it.
"A": Write to open, point the file pointer to the end of the file. If the file does not exist, try to create it.
"A +": Open read-write mode, point the file pointer to the end of the file. If the file does not exist, try to create it. "X": Created and opened as write, the file pointer points to the file header. If the file already exists, the fopen () call fails and returns false, and generates an E_warning level error message. If the file does not exist, try to create it. "x+": Creates and opens read-write, pointing the file pointer to the file header. If the file already exists, the fopen () call fails and returns false, and generates an E_warning level error message. If the file does not exist, try to create it.

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

(3) Read file function
********
fgets (file,length) function is used to read one row of data at a time
file: Must, specify the file to read
Length: optional, Specifies the number of bytes to read, which is 1024 bytes by default. Reads a row from the file pointed to by the files and returns a string of up to length-1 bytes in length. 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. If it fails, it returns false.
********
File (path,include_path,context), which returns the files as an array, with each cell in the array being the corresponding line in the file, including the newline character. If it fails, it returns false.
Path: Required to specify the file to read.
Include_path: Optional. If you want to search for files in include_path, set this parameter to 1.
Context: Optional. Specifies the environment for file handles. The context is a set of options that modify the behavior of the 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 parameter to "1" If you also want to search for files in include_path.
Context: Optional, specifies the environment for file handles. The context is a set of options that modify the behavior of the stream. If NULL is used, it is ignored.
Start: optional, which specifies where to start reading in the file.
Max_length: Optional, specifies the number of bytes to read.

(4) Write file function
Writing data is also a common file operation in PHP, where you can use the fwrite () and fputs () functions to write data to a file. The fputs () function is an alias for the fwrite () function. They use the same.
Syntax: fwrite (file,string,length)
Parameter description:
File: Must specify an open file to write to.
String: Required to specify the strings to be written to the file.
Length: Optional. Specifies the maximum number of bytes to write.
Fwrite () writes the contents of the string to the file pointer. If length is specified, the write stops when the length byte is written or when the string is finished. Send Write () returns the number of bytes written, or False if an error occurs.

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

3, timed refresh technology Delete Do not speak user
timed refresh refers to the object being reloaded once every time, or only once. This functionality can be easily implemented using JavaScript scripts. The SetInterval () function in the
(1) setinterval () function
JavaScript can perform an operation every once in a while. The function syntax is as follows: The
Window.setinterval (function,milliseconds)
parameter function indicates the action to be performed, which can be a function. The
parameter miliseconds represents the time interval, in milliseconds. After the
uses the SetInterval () function, the function will continue to execute as long as the page is not closed. If you want to end the event, you can use the Clearinterval () function. The function is formatted as follows: The
Window.clearinterval (Interval)
parameter Interval is the return value of the Setintervald () function. The

(2) setTimeout () function
setTimeout () function and setinterval () function use the same, but the setTimeout () function executes only once. Used to periodically delete users who do not speak in a chat room.

4, scroll bar positioning technology
Chat room 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 positioning of the scroll bar can be divided into scroll bars for the entire page window and scroll bars in the page elements. The scroll bars in the

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


(2) Scroll bars in the page element's scrollbar
page element can be positioned using the ScrollTop property. The ScrollTop property represents the distance of the current object's vertices from its outermost object element. The syntax is as follows:
Object.scrolltop=distance;
The scrolling effect in the common window of a chat room is the property used to locate the scroll bar. The public window uses a div layer to display the chat information. First, the vertical scroll bar of the DIV layer is set to automatic, and the horizontal scroll bar is set to none and wraps automatically.
copy code code as follows:
... The ScrollTop property of the

setting the vertical scrollbar in JavaScript is equal to its scroll height (to the bottom of the chat window)
copy code code is as follows:


5, Mask Refresh technology
Mask Refresh is the masking key, masking the right mouse button and hiding the" refresh "in the browser Button 3-part. Each section uses a different technique.

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

http://www.bkjia.com/PHPjc/327861.html www.bkjia.com true http://www.bkjia.com/PHPjc/327861.html techarticle One, the chat room module realizes 1, the Chat Room Main Page window sets the copy code code as follows: Meta http-equiv= "Content-type" content= "text/html; charset=gb2312"/!--Loading configuration file-- ...

  • 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.