PHP program hundred lines of code to quickly build a simple chat room method

Source: Internet
Author: User
& Nbsp; basic functions: logging in, chatting, recording online user ip events, controlling the chat font color, and automatically converting the URL in the chat into a link address, customizes 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 is a great scripting language for rapid development! Code & lt ;? Php *** simple chat room for night passers-by * author: basic functions: ability to log in, chat, record online user ip events, and control the font color of chat, automatically converts the URL in the chat to a link address, and customizes 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 is a great scripting language for rapid development!

=== Code ===

       
        * 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, happy Valentine's Day !! "); // Obtain the value if (isset ($ _ GET ['action']) &! Empty ($ _ GET ['action']) {$ action = $ _ GET ['action'];} // if you have logged on to the chat interface, skip to the chat interface if (! Isset ($ _ GET ['action']) & isset ($ _ SESSION ['username']) {header ("location:". SCRIPT ."? Action = chat ");} // if (! Isset ($ _ GET ['action']) {if (! Session_is_registered ('Username') {echo"
        

[". CHAT_NAME."]©2005

"; Exit ;}/// verify if ($ action = 'login') {if (isset ($ _ POST ['login _ user']) &! Empty ($ _ POST ['login _ user']) {$ username = $ _ POST ['login _ user'];} else {$ username = "";} session_register ('Username'); save_online ($ username, get_client_ip (); header ("location :". SCRIPT. "? Action = chat ") ;}// start chatting www. cncms. comif ($ action =" chat ") {$ online_sum = get_online_sum (); echo" [". CHAT_NAME."]


". AD_MSG ." [Currently online: $ online_sum] ";}// User interface if ($ action =" say ") {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; script function check () {if (document. chat. chatmsg. value = '') {; alert ('Enter the chat information! '); Return false;} return true;} script ";} // save the speech if ($ action =" save ") {if ($ _ POST ['chatmsg ']! = "") {Save_chat ($ _ POST ['chatmsg '], $ _ SESSION ['username'], $ _ POST ['usercolor']);} header ("location :". SCRIPT. "? Action = say ");} // display the chat record if ($ action =" show ") {echo" &lt;body style = 'font-size: 12px '&gt; "; echo "&lt;META HTTP-EQUIV = refresh content = '". REF_TIME. "; URL = ". SCRIPT. "? Action = show '&gt; "; if (file_exists (CHAT_NOTE) {$ chat_msg = @ file_get_contents (CHAT_NOTE); echo $ chat_msg ;} else {echo "no speaker currently" ;}}// exit the chat room if ($ action = "logoff") {unset ($ _ SESSION ['username']); session_destroy (); header ("location :". SCRIPT);}/* Basic function * // save chat record function save_chat ($ msg, $ user, $ color) {if (! $ Fp = fopen (CHAT_NOTE, "a +") {die ('failed to create the chat record file. check whether you have the permission. ') ;}$ msg = htmlspecialchars ($ msg); $ msg = PReg_replace ('/([http | ftp: \/]) * ([a-zA-]) + \. ([a-zA-Z0-9 _-]) + \. ([a-zA-Z0-9 _-]) + (a-zA-Z0-9 _) */',' \ 0', $ msg ); $ msg = preg_replace ('/([a-zA-Z0-9 _ \.]) + @ ([a-zA-Z0-9-]) + \. ([a-zA-Z0-9-] {2, 4}) +/',' \ 0', $ msg); $ msg = date ('H: I: s '). "[". $ user. :". $ msg. "&lt;br&gt; \ r \ n"; if (! Fwrite ($ fp, $ msg) {die ('failed to write the chat record. ');} fclose ($ fp);} // write it in the asset information function save_online ($ user, $ ip) {if (! $ Fp = fopen (ONLINE_LIST, "a +") {die ("An error occurred while creating the online list file. check whether you have the permission. ") ;}$ user = str_replace (" | "," ", $ user); $ line = $ user. "| ". $ ip. "| ". time (). "\ r \ n"; if (! Fwrite ($ fp, $ line) {die ("An error occurred while writing to the online list. ");} fclose ($ fp);} // Obtain the number of online users. function get_online_sum () {if (file_exists (ONLINE_LIST) {$ online_msg = file (ONLINE_LIST ); return count ($ online_msg);} else {return 0 ;}// obtain the current login user's IPfunction get_client_ip () {if ($ _ SERVER ['remote _ ADDR ']) {$ cip = $ _ SERVER ['remote _ ADDR '];} elseif (getenv ("REMOTE_ADDR") {$ cip = getenv ("REMOTE_ADDR ");} elseif (getenv ("HTTP_CLIENT_IP ") {$ Cip = getenv (" HTTP_CLIENT_IP ") ;}else {$ cip =" unknown ";}return $ cip ;}?&gt; &lt;/CCID_CODE&gt; &lt;/PRE&gt; &lt;/TD&gt; &lt;/TR&gt; &lt;/TABLE&gt; &lt;/CENTER&gt; &lt;p class = "bdlikebutton"&gt; &lt;/p&gt; &lt;p id = "a_l_2" class = "ad_b6"&gt; &lt;/p&gt; &lt;p class = "clear"&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.