& 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> <title> [". CHAT_NAME. "] </title> </pead> <center> <body bgcolor = # C4BFB9 style = 'font-size: 12px; '> 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" <body style = 'font-size: 12px '> "; echo "<META HTTP-EQUIV = refresh content = '". REF_TIME. "; URL = ". SCRIPT. "? Action = show '> "; 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. "<br> \ 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 ;}?> </CCID_CODE> </PRE> </TD> </TR> </TABLE> </CENTER> <p class = "bdlikebutton"> </p> <p id = "a_l_2" class = "ad_b6"> </p> <p class = "clear">
|