Home  >  Article  >  Backend Development  >  Implementing a simple chat room with PHP (with source code) Page 1/2_PHP Tutorial

Implementing a simple chat room with PHP (with source code) Page 1/2_PHP Tutorial

WBOY
WBOYOriginal
2016-07-21 15:03:03972browse

1. Chat room module implementation
1. Chat room main page window settings

Copy code The code is as follows:





<?php echo CHAT_NAME ; ?>

















<br><body>< /body><br>

2,文件操作函数
本聊天室模块使用文本来保存用户列表和发言信息,主要考虑对文件的操作在速度上要优于对数据库的操作。

(1)fopen()函数,打开文件或者URL,如果打开失败,函数返回FALSE
语法:fopen(filename,mode,include_path,context)
参数说明:
filename:
必须,规定要打开的文件或URL
mode:必须,规定要求到该文件/流的访问类型。
include_path:可选,如果也需要在include_path中检索文件的话,可以将该参数设为1或TRUE。
context:可选。规定文件句柄的环境,context是可以修改流的行文的一套选项
mode参数的取值:
"r":只读方式打开,将文件指针指向文件头。
"r+":读写方式打开,将文件指针指向文件头。
"w":写入方式打开,将文件指针指向文件头并将文件大小截为零。如果文件不存在则尝试创建。
"w+":读写方式打开,将文件指针指向文件头并将文件大小截为零。如果文件不存在则尝试创建。
"a":写入方式打开,将文件指针指向文件末尾。如果文件不存在则尝试创建。
"a+":读写方式打开,将文件指针指向文件末尾。如果文件不存在则尝试创建。"x":创建并以写入方式打开,文件指针指向文件头。如果文件已存在,则fopen()调用失败并返回FALSE,并生成一条E_WARNING级别的错误信息。如果文件不存在则尝试创建。"x+":创建并以读写方式打开,将文件指针指向文件头。如果文件已存在,则fopen()调用失败并返回FALSE,并生成一条E_WARNING级别的错误信息。如果文件不存在则尝试创建。

(2)fclose()函数,关闭打开的文件
语法:fclose(file)
参数:file:必须,规定要关闭的文件。file参数是一个文件指针,fclose()函数关闭指针指向的文件。文件指针必须有效,并且是通过fopen()或fsockopen()成功打开的文件。如果成功则返回true,否则返回false。

(3)读取文件函数
********
fgets(file,length)函数用于一次读取一行数据
file:必须,规定要读取的文件
length:可选,规定要读取的字节数,默认是1024字节。从file指向的文件中读取一行并返回长度最多为length-1字节的字符串。碰到换行符(包括在返回值中)、EOF或者已经读取了length-1字节后停止。如果没有指定length,默认为1K,或者说1024字节。若失败,则返回false。
********
file(path,include_path,context),将文件作为一个数组返回,数组中的每个单元都是文件中相应的一行,包括换行符在内。若失败,则返回false。
path:必须,规定要读取的文件。
include_path:可选。如果想在include_path中搜索文件,将该参数设为1.
context:可选。规定文件句柄的环境。context是一套可以修改流的行为的选项,若使用null,则忽略。
********
file_get_contents(path,include_path,context,start,max_length),是用于将文件的内容读入到一个字符串中的首选方法。
path:必须,规定要读取的文件
include_path:可选,如果也想在include_path中搜寻文件的话,可以将该参数设为"1".
context:可选,规定文件句柄的环境。context是一套可以修改流的行为的选项。若使用null,则忽略。
start:可选,规定在文件中开始读取的位置。
max_length:可选,规定读取的字节数。

(4)写入文件函数
写入数据也是PHP中常用的文件操作,在PHP中可以使用fwrite()和fputs()函数向文件中写入数据。fputs()函数是fwrite()函数的别名。他们的用法相同。
语法:fwrite(file,string,length)
参数说明:
file:必须,规定要写入的打开文件。
string:必须,规定要写入的文件的字符串。
length:可选。规定要写入的最大字节数。
fwrite()把string的内容写入文件指针file处。如果指定了length,当写入了length个字节或者写完了string以后,写入就会停止。发write()返回写入的字节数,出现错误时则返回false。

(5)删除文件
unlink(filename,context)函数用于删除文件,该文件必须是关闭的,若成功则返回true,失败则返回false。
filename:必须,规定要删除的文件;
context:可选,规定文件句柄环境。context是可修改流的行为的一套选项。

3. Scheduled refresh technology to delete silent users
Scheduled refresh means that the object is reloaded every once in a while, or it can be loaded only once. This function 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 syntax format of this function is as follows:
window.setInterval(Function,MilliSeconds)
The parameter Function indicates the operation to be performed, which can be a function.
The parameter MiliSeconds represents the interval time, in milliseconds.
After using the setInterval() function, the function will continue to be 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 has the same usage as the setInterval() function, but the setTimeout() function is only executed once. Used to regularly delete users who do not speak in the chat room.

4. Scroll bar positioning technology when 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 positioned every time it is refreshed. Go to the top. The positioning of scroll bars can be divided into scroll bars in the entire page window and scroll bars in page elements.

(1) Scroll bar in the window
Scroll bar in the window can Positioning is achieved through 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 coordinate of the vertical position of the scroll bar. If you want to put the scroll bar at the bottom, just set the y value to a larger value.

Copy code The code is as follows:



(2) The scroll bar in the page element
The scroll bar in the page element can be scrollTop attributes to achieve positioning. The scrollTop property represents the distance between the current object's vertices and its outermost object element. The syntax is as follows:
object.scrollTop=distance;
The scrolling effect in the chat room public window uses this attribute to position the scroll bar. The public window uses a div layer to display chat information. First, set the vertical scroll bar of the div layer to automatic, set the horizontal scroll bar to none and automatically wrap lines.
Copy code The code is as follows:

...


Set the scrollTop property of the vertical scroll bar in Javascript equal to its scroll height (positioned to the bottom of the chat window)
Copy code The code is as follows :



5 , shielding refresh technology
Shielding refresh means shielding the key, shielding the right mouse button and hiding the "refresh" button in the browser. The techniques used in each section are different.

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

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/327861.htmlTechArticle1. Chat room module implementation 1, chat room main page window setting copy code code is as follows: meta http-equiv= "Content-Type" content="text/html; charset=gb2312" / !--Load configuration file--...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn