PHP chat room program based on redis

Source: Internet
Author: User

Redis is a lightweight key-value data storage system. Compared with memcache/memcached, redis provides more redundancy and transfers stored data to disk files, so that it can be restored at the next startup. Redis also provides a collection, list, and other data structures.

Combined with the above features, apsaradb for redis is used as a data storage solution in the chat room to achieve high execution efficiency. Directly paste the server code and the php_redis extension is required. No user system has not done it. This program is currently mounted to a phpwind forum and reads users and user groups directly from global variables. The Interface part is omitted here. For a complete demonstration, see http://bbs.91d2.cn/. you can open the Forum room in the lower right corner of the Operation bar.
<? Phpdefine ('Disable _ refresh_limit ', 1); require_once ('Global. PHP '); header ('content-type: text/html; charset = UTF-8'); $ redis = new redis (); $ redis-> connect ('2017. 0.0.1 ', 6379); $ if_init = get_string ('chat _ init'); If (! $ If_init) {If (get_string ('chat _ init_lock ') {die;} set_string ('chat _ init_lock', '1'); set_array ('chat _ channel ', array ('global'); set_array ('chat _ motd ', array ('global' => 'Hello! '); Set_array ('chat _ online', array (); set_string ('chat _ init', '1'); set_string ("0_name ", 'system'); set_string ("0_flag", 'sa');} If (! $ Winduid) {echo 'chatlogin (); '; die;} set_string ("{$ winduid} _ name", $ windid ); set_string ("uid _ {$ windid}", $ winduid); $ channel_list = get_array ('chat _ channel'); $ channel_motd = get_array ('chat _ motd '); $ my_channel = get_string ("{$ winduid} _ in"); $ my_index = get_string ("{$ winduid} _ Index"); If (! $ My_channel |! In_array ($ my_channel, $ channel_list) {$ my_channel = $ channel_list [0]; set_string ("{$ winduid} _ in", $ my_channel); $ my_index = 0 ;} $ channel_index = get_string ("{$ my_channel} _ Index"); $ channel_ca = get_array ("{$ my_channel} _ Ca "); if ($ winddb ['groupid'] = 3 | $ winddb ['groupid'] = 4 | $ winddb ['groupid'] = 22) {$ user_flag = 'sa ';} else if (in_array ($ winduid, $ channel_ca) {$ user_flag = 'CA';} else {$ user_flag = 'U';} set_string ("{$ winduid} _ flag", $ user_flag); initgp (Array ('action', 'first'); If (! $ My_index | $ first) {if ($ my_index = 0) {send_message (0, 'motd :'. $ channel_motd [$ my_channel], 'info', $ my_channel, $ winduid);} $ my_index = $ first? ($ Channel_index-10): ($ channel_index + 1); if ($ my_index <0) {$ my_index = 0;} set_string ("{$ winduid} _ Index ", $ my_index);} if ($ action = 'ui') {require_once printeot ('chat _ Ui');} if ($ action = 'channel ') {echo "chatchannellistbegin (); \ r \ n"; foreach ($ channel_list as $ channel_name) {echo "chathandlechannellist ('". mb_convert_encoding ($ channel_name, 'utf-8', 'gbk '). "'); \ r \ n";} echo "chatchannellistend ();\ R \ n ";}if ($ action = 'send') {initgp (Array ('content', 'receiver'); If (! $ Content |! $ Author ER) {echo '0'; die;} if ($ winddb ['postnum'] <100) {send_message (0, 'Your posts are less than 100, no permission to speak ', 'warn', $ my_channel, $ winduid);} else {If (get_string ("{$ winduid} _ block")> $ timestamp) {send_message (0, 'you have been banned and cannot speak ', 'warn', $ my_channel, $ winduid);} else {$ content = htmlspecialchars ($ content ); send_message ($ winduid, stripslashes (mb_convert_encoding ($ content, 'gbk', 'utf-8'), 'info', $ my_channel, $ receiver );}} Echo '0';} if ($ action = 'get') {$ channel_ca_list = ''; if ($ user_flag = 'sa '| $ user_flag = 'CA') {foreach ($ channel_ca as $ CA) {$ channel_ca_list. = get_string ("{$ ca} _ name "). ',';} $ channel_ca_list = mb_convert_encoding ($ channel_ca_list, 'utf-8', 'gbk');} echo "chathandlestatus ('". mb_convert_encoding ($ my_channel, 'utf-8', 'gbk '). "','". mb_convert_encoding ($ windid, 'utf-8', 'gbk '). "',' {$ user _ Flag} ',' {$ channel_ca_list} '); "; for (; $ my_index <= $ channel_index; $ my_index ++) {$ message = get_array ("{$ my_channel }_{ $ my_index }"); if ($ message ['Referer'] = 'all' | $ message ['Referer'] = $ winduid) {$ message ['content'] = addslashes (mb_convert_encoding ($ message ['content'], 'utf-8', 'gbk ')); $ sender_flag = get_string ("{$ message [author]} _ flag"); $ sender_flag = 'sa '& $ message ['content'] =' <Span Style = "color: red; font-weight: bold"> '. $ message ['content']. '</span>'; $ sender_flag = 'CA' & $ message ['content'] = '<span style = "color: Blue"> '. $ message ['content']. '</span>'; $ message ['autor'] = addslashes (mb_convert_encoding (get_string ("{$ message [author]} _ name"), 'utf-8 ', 'gbk'); echo "chathandlemessage ('{$ message [type]}', '". $ message ['autor']. "',' {$ message [content]} ', {$ message [date]} ); \ R \ n ";}} set_string (" {$ winduid }_index ", $ my_index);} if ($ action = 'block ') {initgp (Array ('username', 'timespan '); $ username = mb_convert_encoding ($ username, 'gbk', 'utf-8 '); if ($ user_flag = 'sa '| $ user_flag = 'CA') {set_string (get_string ("uid _ {$ username }"). '_ Block', $ timestamp + $ timespan * 3600); send_message (0, 'Ban user ['. $ username. '] succeeded', 'warn', $ my_channel, $ winduid); echo '0';} else {send_m Essage (0, 'authorization', 'warn', $ my_channel, $ winduid); echo '-1' ;}} if ($ action = 'setchannel ') {initgp (Array ('option', 'channel', 'Channel _ name', 'Channel _ ca'); If ($ option = 'join ') {$ channel = mb_convert_encoding ($ channel, 'gbk', 'utf-8'); If (in_array ($ channel, $ channel_list )) {set_string ("{$ winduid} _ Index", 0); set_string ("{$ winduid} _ in", $ channel); echo '0 ';} else {send_message (0, 'Channel does not exist', 'warn', $ my_cha Nnel, $ winduid); echo '-1'; die ;}} else {if ($ user_flag! = 'Sa ') {send_message (0,' currently only the global administrator can create channels ', 'warn', $ my_channel, $ winduid); echo'-1'; die ;} if (! $ Channel_name) {send_message (0, 'Enter the channel name', 'warn', $ my_channel, $ winduid); echo '-1'; die ;} $ channel_name = mb_convert_encoding ($ channel_name, 'gbk', 'utf-8'); $ channel_ca = mb_convert_encoding ($ channel_ca, 'gbk', 'utf-8 '); if (in_array ($ channel, $ channel_list) {send_message (0, 'Channel name duplication ', 'warn', $ my_channel, $ winduid); echo'-1 '; die;} $ channel_list [] = $ channel_name; $ channel_motd [$ channel_name] = 'Hello! '; Set_array ('chat _ channel', $ channel_list); set_array ('chat _ motd', $ channel_motd); set_array ("{$ channel_name} _ Ca ", explode (',', $ channel_ca); send_message (0, 'Channel created successfully', 'warn', $ my_channel, $ winduid );}} if ($ action = 'setmotd ') {initgp (Array ('motd'); $ motd = mb_convert_encoding ($ motd, 'gbk', 'utf-8 '); if ($ user_flag = 'sa '| $ user_flag = 'CA') {$ channel_motd [$ my_channel] = $ motd; set_array ('chat _ motd ', $ Channel_motd); send_message (0, 'motd set successfully', 'warn', $ my_channel, $ winduid); echo '0';} else {send_message (0, 'permission insufficiency ', 'warn', $ my_channel, $ winduid); echo'-1' ;}} if ($ action = 'delchannel ') {if ($ user_flag = 'sa '| $ user_flag = 'CA') {unset ($ channel_list [array_search ($ my_channel, $ channel_list)]); unset ($ channel_motd [$ my_channel]); set_array ('chat _ channel', $ channel_list); set_array ('chat _ motd ', $ channel _ Motd); remove ("{$ my_channel} _ Ca"); set_string ("{$ winduid} _ Index", 0); set_string ("{$ winduid} _ in ", 'global'); send_message (0, 'Channel deleted successfully', 'warn', 'global', $ winduid); echo '0';} else {send_message (0, 'permission insufficiency ', 'warn', $ my_channel, $ winduid); echo'-1' ;}} if ($ action = 'setca ') {initgp (Array ('username', 'option'); If ($ user_flag = 'sa '| $ user_flag = 'CA ') {$ username = mb_convert_encoding ($ username, 'gbk', 'utf -8'); $ uid = get_string ("uid _ {$ username}"); If (! $ UID) {send_message (0, 'user not logged on ', 'warn', $ my_channel, $ winduid); echo'-1'; die ;} if ($ option = 'add') {If (! In_array ($ uid, $ channel_ca) {$ channel_ca [] = $ uid; send_message (0, 'add Administrator ['. $ username. ']', 'warn', $ my_channel, $ winduid) ;}} else {If (in_array ($ uid, $ channel_ca )) {unset ($ channel_ca [array_search ($ uid, $ channel_ca)]); send_message (0, 'cancel Administrator ['. $ username. ']', 'warn', $ my_channel, $ winduid) ;}} set_array ("{$ my_channel }_ca", $ channel_ca); echo '0 ';} else {send_message (0, 'authorization', 'warn', $ my_channel, $ winduid); echo '-1' ;}} function remove () {Global $ redis; $ redis-> Delete ($ name);} function set_string ($ name, $ Val) {Global $ redis; $ redis-> set ($ name, $ Val );} function get_string ($ name, $ Val) {Global $ redis; return $ redis-> get ($ name);} function append_string ($ name, $ Val) {Global $ redis; return $ redis-> append ($ name, $ Val);} function set_array ($ name, $ Val) {Global $ redis; $ redis-> set ($ name, serialize ($ Val);} function get_array ($ name) {Global $ redis; return unserialize ($ redis-> get ($ name);} function send_message ($ author, $ content, $ type = 'info', $ channel = 'global', $ Cycler = 'all') {Global $ redis, $ timestamp; If (strlen ($ content)> 200) {$ content = substr ($ content, 0,200) ;}while (get_string ("{$ channel} _ Lock") {usleep (100 );} set_string ("{$ channel} _ Lock", 1); $ channel_index = get_string ("{$ channel} _ Index"); $ channel_index = $ channel_index + 1; set_array ("{$ channel }_{ $ channel_index}", array ('author' => $ author, 'content' => $ content, 'type' => $ type, 'channeler' => $ explorer, 'date' => $ timestamp); set_string ("{$ channel} _ Index", $ channel_index ); $ clean_index = $ channel_index-100; remove ("{$ channel }_{ $ clean_index}"); $ author_name = get_string ("{$ author} _ name "); append_string ("{$ channel} _ record", get_date ($ timestamp, 'Y-m-d h: I: s '). "[{$ type}] {$ author_name}-> {$ channeler }:{ $ content} \ r \ n"); check_channel_record ($ channel ); set_string ("{$ channel} _ Lock", 0);} function check_channel_record ($ channel) {Global $ timestamp; $ lastsave = get_string ("{$ channel} _ lastsave"); if ($ timestamp-$ lastsave> 600) {set_string ("{$ channel} _ lastsave ", $ timestamp); save_record ($ channel) ;}} function save_record ($ channel) {Global $ db, $ timestamp; $ record = str_replace (Array ("\\", "\" "," '"), array ("\\\\","\\\"","\\'"), get_string ("{$ channel} _ record"); set_string ("{$ channel} _ record", ''); If (strlen ($ record)> 0) {$ db-> Update ("insert into 'PW _ chatrecord '('channel', 'time', 'content') values (' $ channel', '$ timestamp ', '$ record ')");}}
Related Article

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.