A simple PHP getting started source program

Source: Internet
Author: User

The following is an example of the Chinese Document "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 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.

<Meta> labels are usually placed in the
<Meta http-equiv = "Refresh" content = "15; url = <? Echo $ PHP_SELF;?> ">

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.


<Html>
<Head>
<Title> Chat Room </title>
</Head>
<Frameset rows = "*, 40" border = 1>
<Frame src = "list. php" name = list scrolling = auto>
<Frame src = "post. php" name = post scrolling = no>
<Noframes>
<Body>
This chat room needs to use a page box, which cannot be used by your browser
</Body>
</Noframes>
</Frameset>
</Html>




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.


<? Php
// 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.


<? Php
// File name: post. php
Require ("env. inc ");
If ($ chatuser! = "") And ($ chattext! = "")){
$ Chatstr = "<font color = 8080ff> ". date ("h: I: s "). "</font>-<font color = ff8080> ". $ chatuser. "</font> :". $ chattext;
$ Response STR = "echo \" ". $ chatstr." \ ">". $ chatfile;
If (! File_exists ($ chatfile) passthru ("touch". $ chatfile );
Passthru ($ response Str );
}
?> <Html>
<Body bgcolor = ffffff leftmargin = 0 topmargin = 0 marginheight = 0 marginwidth = 0>
<Form action = <? Echo $ PHP_SELF;?> Method = post>
<Table border = 0 width = 100%> <tr>
<Td align = right> alias: </td>
<Td> <input type = text name = chatuser size = 8 value = "<? Echo $ chatuser;?> "> </Td>
<Td align = right> statement: </td>
<Td> <input type = text name = chattext size = 30 maxlength = 500> </td>
<Td> <div align = right> <input type = submit value = "send"> </td>
</Tr> </table>
</Form>
</Body>
</Html>


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.


<Html>
<Meta http-equiv = "Refresh" content = "5; url = <? Echo $ PHP_SELF;?> ">
<Meta content = "text/html; charset = gb2312" http-equiv = Content-Type>
<Body bgcolor = ffffff leftmargin = 0 topmargin = 0 marginheight = 0 marginwidth = 0>
<?
// File name: list. php
Require ("env. inc ");

If (! File_exists ($ chatfile )){
Echo "not opened yet </body> 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]. "<br> \ n ";
}
Unlink ($ uniqfile );
?>
</Body>
</Html>


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 </body> 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]. "<br> \ 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.