PHP hundred lines of code to quickly build a simple chat room _ PHP Tutorial

Source: Internet
Author: User
PHP hundred lines of code quickly build a simple chat room. Bored, I saw that I could use less code to build a chat room. at the beginning, I wrote a full row of 100 lines to complete the chat room. later I felt that there was no function and I modified it again, when a lot of code is added and boring, you can use a relatively small amount of code to build a chat room. at the beginning, you wrote a full 100 lines to complete the chat room. later, you felt that there were no functions and modified them again, added a lot of code. In fact, we can do a good job in chatting rooms without using complex technologies such as templates and databases. it is suitable for individual users.

Basic functions: logging in, chatting, recording online user IP events, controlling the chat font color, automatically converting the chat URL into a link address, and customizing the chat room Title and advertisement information. If you are interested in using text as a storage media, you can refer to the code and scale it out.

In fact, php (as the mainstream development language) is a great script language for rapid development!

=== Code ===

/**
* Simple chat room for night travelers
* Author: heiyeluren
* Creation:
* Modification:
*/
Error_reporting (7 );
Session_start ();
Header ("ContentType: text/html; charset = gb2312 ");
Define ("SCRIPT", $ _ SERVER [SCRIPT_NAME]);
Define ("CHAT_NOTE", "./chat.txt ");
Define ("ONLINE_LIST", "./online.txt ");
Define ("REF_TIME", 5 );
Define ("CHAT_NAME", "night passers-by chat room ");
Define ("AD_MSG", "Today is Chinese Valentine's day. I wish you a happy Valentine's Day !! ");

// Obtain the value
If (isset ($ _ GET [action]) &! Empty ($ _ GET [action]) {
$ Action = $ _ GET [action];
}

// If you have logged on, jump to the chat page.
If (! Isset ($ _ GET [action]) & isset ($ _ SESSION [username]) {
Header ("location:". SCRIPT ."? Action = chat ");
}

// Logon prompt
If (! Isset ($ _ GET [action])
{
If (! Session_is_registered (username ))
{
Echo"

[". CHAT_NAME."]©2005




";
Exit;
}
}

// Verify login
If ($ action = login)
{
If (isset ($ _ POST [login_user]) &! Empty ($ _ POST [login_user]) {
$ Username = $ _ POST [login_user];
} Else {
$ Username = "tourist ";
}
Session_register (username );
Save_online ($ username, get_client_ip ());
Header ("location:". SCRIPT ."? Action = chat ");
}

// Start chatting with www.acnow.net
If ($ action = "chat ")
{
$ Online_sum = get_online_sum ();
Echo"[". CHAT_NAME."]




". AD_MSG ." [Currently online: $ online_sum]
&lt;BR&gt; "; &lt;BR&gt; &amp; #125; &lt;/P&gt; &lt;P&gt; // User Interface &lt;BR&gt; if (&amp; #36; action =" say ") &lt;BR&gt; &amp; #123; &lt;BR&gt; echo "<pead&gt; &lt;title&gt; [". CHAT_NAME. "] &lt;/title&gt; </pead&gt; &lt;center&gt; &lt;body bgcolor = # C4BFB9 style = font-size: 12px;&gt; &lt;BR&gt; &lt;form action = ". SCRIPT. "? Action = save method = post name = chat onSubmit = return check ()&gt; &lt;BR&gt; [". &amp; #36; _ SESSION [username]. "] description: &lt;input type = text size = 80 maxlength = 500 name = chatmsg style = background-color: #99CC99; width: 550px; height: 22px; border: 1px solid: #000000&gt; &lt;/p&gt; &lt;p align = "left"&gt; &lt;p style = "display: none;"&gt; empty... &lt;/p&gt; &lt;p class = "art_confoot"&gt;

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.