A simple PHP getting started source program

Source: Internet
Author: User
An example of the Chinese version of "Star prodigal son" php: chatting room is a secret weapon for boring people on the Web site. At the same time, the webmaster or other personnel can also kill the time here. Even if you do not have a strong online relationship, you can at least increase the typing speed. The chat room is actually a CGI program used by many people. The program will reference the words entered by each person to the "star prodigal son" php Chinese document example:

Chatting room is a secret weapon for boring people on the Web site. At the same time, the webmaster or other personnel can also kill the time here. Even if you do not have a strong online relationship, you can at least increase the typing speed.
The chat room is actually a CGI program used by many people. The program sorts the strings entered by each user according to the time the system receives and then sends them to each user. The difference between the Web chat room and the BBS chat room is that the BBS chat room can be sent to each online user in the chat room immediately after each sentence is received; because the Web CGI program cannot be connected like the BBS telnet, Web CGI must send the information as quickly as possible and end the connection. This situation occurs because the Web chat room or the HTTP transmission protocol is used. in the HTTP version, whether it is 0.9, 1.0 or 1.1, it cannot occupy the Port of the network connection for a long time.

To solve the problem that data cannot be transmitted immediately and update information, Netscape used a new technology after the 3.0 browser, internet Explorer also implements the technologies developed by Netscape. Netscape divides it into two technologies: Server Push and Client Pull. Server Push is used by the Web Server to send data in multi-MIME encoding to the user end. Currently, few websites use this method, while Client Pull uses the meta tag of HTML, the http-equiv = "Refresh" attribute is used to indicate that the data needs to be reloaded. As for the loading time, the content attribute is used to achieve this.

Labels are usually placed in..So that the browser can only prepare to update the user's webpage. The example used in combination with meta and PHP is set to reload every 15 seconds.



If Server Push or Client Pull is not used as the chat room, is there any other way for Web browsers to chat? The answer is yes. You can use Java or ActiveX (IE4 or 5 only) to develop exclusive Browser Plug-in programs (such as Qimo's chat room), but this has nothing to do with PHP, this is not the focus.

In addition, because the comments of all netizens are updated on a regular basis, in order to avoid writing half of the strings that have not been written due to refresh, it is necessary to use the frame page frame technology in the chat room. The following example shows the main program of the chat room.




Chat room




<Br> <body> <br> This chat room requires a page box, which cannot be used by your browser <br> </body> <br>






The program uses frame to bring out two PHP programs. we recommend that you put them in the same directory, such as/chatroom, for future maintenance. In addition, to list. php and post. php can use the same variable. in the following example, the common path change is placed in env. inc, which can be placed in the/chatroom or the PHP include setting path of the Web server (such as Apache.


// File name: env. inc
$ Tempdir = "/tmp /";
$ Chatfile = "/tmp/abc ";
?>


The backend of the chat room can be designed very easily. you can simply use archives to create a database and drop the chat content. if you really care about the system efficiency, perhaps you can consider using UNIX-based trip communication IPC.

In this section, the user's message content is put into the file. most of the examples here use UNIX/Linux external commands. If the system does not have this command (or program), install the relevant program on your own.

In fact, dropping data into an archive is faster than using a database. if you are still concerned about the speed, you can install RAM Disk on a UNIX machine, and then set the file access path to this RAM Disk, ensure that the access speed meets stringent requirements. In some websites that call for high-speed search engines, even storing the entire database data in RAM Disk, the system speed is immediately increased by 10 times and times, in addition, RAM is cheaper than other solutions. If you use Windows NT, you can't do it. check when Microsoft provides it or use Third Party products.

Some users may not be familiar with UNIX. here we will introduce the instructions used:

Touch: create a new file or modify the last update date of the old file.

Echo adds two symbols greater than the symbol: the string is displayed to the specified place.

Tail: displays the data of the last few rows of the file. The value is 10 rows. you can use the minus sign to add a number to modify the number of rows to be displayed.

The following is a program for sending and processing message strings. The program uses the env. inc file.


// File name: post. php
Require ("env. inc ");
If ($ chatuser! = "") And ($ chattext! = "")){
$ Chatstr = "". date ("h: I: s"). "-". $ chatuser. ":". $ chattext;
$ Response str = "echo \" ". $ chatstr." \ ">". $ chatfile;
If (! File_exists ($ chatfile) passthru ("touch". $ chatfile );
Passthru ($ response str );
}
?>






The program first checks whether there is any input string. if there is no anonymous and the string of the speech content, the Form of the speech is displayed ), if there is any data, the string and the time will be stored in the file (using UNIX redirection instructions ). Of course, to prevent errors, check whether an archive exists. If no archive exists, touch the archive first. In this example, the archive is/tmp/abc.






// File name: list. php
Require ("env. inc ");

If (! File_exists ($ chatfile )){
Echo "not opened yet";
Exit;
}

$ Uniqfile = $ tempdir. uniqid (rand ());
$ Shellcmd = "/usr/bin/tail-50". $ chatfile. ">". $ uniqfile;
Passthru ($ shellcmd );
$ Chatfilearray = file ($ uniqfile );
$ J = count ($ chatfilearray );
For ($ I = 1; $ I <= $ j; $ I ){
Echo $ chatfilearray [$ j-$ I]."
\ N ";
}
Unlink ($ uniqfile );
?>




The above program uses the Client Pull technology, which is updated every five seconds. Similarly, it also uses the env. inc files can be used by all programs immediately when the variables need to be changed. this is a very important method for developing websites, it can be used in all web programs. For example, the Copyright (C) 1996-2000 string is stored in a file. in the next year, the entire website is changed as long as the file is changed.

If (! File_exists ($ chatfile )){
Echo "not opened yet";
Exit;
}

$ Uniqfile = $ tempdir. uniqid (rand ());
$ Shellcmd = "/usr/bin/tail-50". $ chatfile. ">". $ uniqfile;
Passthru ($ shellcmd );

The program first checks whether the user has sent the chat content file/tmp/abc. if not, it indicates that the user has not opened the chat content, and waits for the user to send the chat content. If you already have chat materials, capture the last 50 pieces and prepare to display them in another file.

$ Chatfilearray = file ($ uniqfile );
$ J = count ($ chatfilearray );
For ($ I = 1; $ I <= $ j; $ I ){
Echo $ chatfilearray [$ j-$ I]."
\ N ";
}
Unlink ($ uniqfile );

Read the file into the array variable $ chatfilearray and send it to the browser in the first display of the final information. of course, you can sort the array columns, however, it is a waste of CPU time to sort the last stored data at the end of a certain period. Therefore, it is necessary to echo the last data to the first data. The unlink () command is also used to kill the temporary 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.