PHP simple multi-person chat interface Design code
The following is a brief introduction of a simple login multiplayer chat system design, only four files, respectively, is the login page login.php, multiplayer chat interface chat.php, Design the database operation of the sql.php file and the registration page regester.php, where the registration page and login page code%98 code is identical. are all using the same structure. In fact, the difficulty is still in the sql.php file, because this is related to the database operation of the file, so a lot of problems are basically caused by this page, of course, there are many people chat interface is also very prone to problems. In general, it is simply to remove the corresponding conversation content from the database based on the username and the recipient's name. This shortcoming is very big, in the actual application is almost useless, just to learn to think of doing so, the dialogue content in the database, it is also very expensive system resources behavior. However, saving a small amount of conversation information should still be possible. Gossip is not much to say, As long as the code can read the sql.php file basically understand the whole multiplayer chat system, note that my four files are all placed under a file, so when you want to copy the system, it is important to ensure that the four files are in the same folder, and it is better to ensure that the four files are UTF-8 encoded, if not please change to UTF-8 encoding. Otherwise, the Chinese garbled phenomenon will appear. A Chinese character occupies 2 characters under the GBK encoding and three characters under the UTF-8 code, which can be tested with the strlen () function. Here to create two tables, two data tables are as follows:
This table, called Comm, is a data sheet dedicated to storing user conversation information:
The following table, named Rege, is designed to store user registration information:
Here is the login page login.php
<title>Welcom to login page</title>Click to register
Checkpassword ($user, $pwd)) {//$_session["$user"]=true;header ("Location:chat.php?name=". UrlEncode ($user));} else {echo "";}}}}
Here is the registration page regester.php
<title>Welcom to login page</title>
6) {echo ""; exit ();} if (Preg_match ("/^[\x7f-\xff]+$/", $user) ==0) {echo ""; exit ();} if (Isset ($_post[' pwd ')) {$pwd =trim ($_post[' pwd "), if (Preg_match ("/^[a-z0-9\-]+$/i ", $pwd) ==0) {echo" "; exit ();} if ($pwd = = ") {echo" "; exit ();} ElseIf (strlen ($pwd) >=19) {echo ""; exit ();} else {include_once ' sql.php '; $sql = new Sql (); $sql->regerster ($user, $pwd);}}? >
The following is the sql.php file for the operational database
Conn=mysql_connect ("localhost", "like", "admin"), mysql_select_db ("test"), and if (! $this->conn) die ("fail to connect The MySQL database "); Public Function Queryinfor ($receiver) {$arr =array (); $receiver =iconv ("Utf-8", "GBK", $receiver); $query = "SELECT * from" Comm ' where recever= ' $receiver ' ORDER by id DESC '; mysql_query ("Set names GBK"); $result =mysql_query ($query, $this conn) or Die ("Fuck error"), while ($row =mysql_fetch_row ($result)) {$arr []= $row;} if (count ($arr) >5) {//Save more than five messages, delete the excess information for ($i =count ($arr)-1; $i >4; $i-) {mysql_query ("delete from ' comm ' where id={$arr [$i][0]} "or Die (" fail to delete the extra information "), unset ($arr [$i]);} Mysql_free_result ($result); for ($i =0; $i
conn) or Die ("Fuck error"), while ($row =mysql_fetch_row ($result)) {$arr []= $row;} if (count ($arr) >5) {//Save more than five messages, delete the excess information for ($i =count ($arr)-1; $i >4; $i-) {mysql_query ("delete from ' comm ' where id={$arr [$i][0]} "or Die (" fail to delete the extra information "), unset ($arr [$i]);} Mysql_free_result ($result); for ($i =0; $i
conn); $query = "INSERT INTO ' comm ' (sendor,time,recever,content) VALUES (' $sender ', ' $timestamp ', ' $receiver ', ' $ Content ') "; mysql_query ($query, $this->conn) or Die (" Insert Error ");} Public Function Getsenderall ($name) {$arr = array (); $name =iconv ("Utf-8", "GBK", $name); $query = "Select User from ' Rege ' where 1 ORDER by id DESC "; mysql_query (" Set names GBK ", $this->conn); $result =mysql_query ($query, $this->conn) or die ("No Data in Database"), while ($row =mysql_fetch_row ($result)) {$arr []= $row [0];} for ($i =0; $i
conn) or Die ("Logoff failed");} Public Function Checkpassword ($name, $password =null) {$name =iconv ("Utf-8", "GBK", $name); $queryP = "select * from ' Rege ' where user= ' $name ' and password= ' $password ' limit 1 '; $queryQ = "select * from ' Rege ' where user= ' $name ' limit 1"; mysql_query ("Set Names GBK", $this->conn), if ($password!=null) {mysql_query ($queryP, $this->conn) or Die ("error"); if ( Mysql_affected_rows ($this->conn) >0) {$result =mysql_query ("Select status from ' Rege ' where user= ' $name '"); $row = Mysql_fetch_row ($result), if ($row [0]) {$this->infor= "the user is already logged in"; return false;} $query = "Update ' Rege ' set status=true where user= ' $name '"; mysql_query ($query, $this->conn); return true;} else{$this->infor= "user name or password error"; return false;}} else {mysql_query ($queryQ, $this->conn) or Die ("Sorry error"); if (Mysql_affected_rows ($this->conn) >0) {$ This->infor= "The user name has been registered"; return false;} return true;}} Public Function Regerster ($name, $pwd) {$name =iconv ("Utf-8", "GBK", $name); $query = "INSERT into ' rege ' (' User ', ' password ') VAlues (' $name ', ' $pwd ') "; mysql_query (" Set names GBK ", $this->conn), mysql_query ($query, $this->conn) or Die (" registration failed "); $this->infor=" registered successfully ";} Public function Close () {mysql_close ($this->conn);}}
The following is the user login after the multiplayer chat interface chat.php, said to be a lot of people chatting, is actually very rough interface, is a UL tag listed all the latest five messages to you, the excess information will be automatically deleted, and then you can choose any one person to write down what he/she said, and then refresh the display. In fact, the principle is quite simple.
Logout ($sess _name); $_session["$sess _name"]=false;header ("Content-type=text/html;charset=utf-8"); echo "Logout success"; echo "Return to login Page"; exit ();}} if (isset ($_get[' name ')) {$user =urldecode ($_get[' name '); $user =$_get[' name '];} Else{header ("location:login.php");} if (isset ($_post[' content ')) {$tmp _content=$_post[' content '); $tmp _user=$_post[' user ']; $tmp _receive=$_post[' Selec ']; $tmp =new Sql (); $tmp->setdatabase ($tmp _user, $tmp _receive, $tmp _content); $tmp = $tmp _content= $tmp _user=$ Tmp_receive=null;}? >
<title>Welcom to chat page</title>"> Logout
queryinfor ($user); for ($i =count ($arr)-1; $i >=0; $i-) {echo "
- ". $arr [$i][1]." In the ". $arr [$i][2]." Said to you:
"; Echo"
- ". $arr [$i][4]."
";} $arr = $sql->queryquery ($user); for ($i =count ($arr)-1; $i >=0; $i-) {echo "
- "." You are in the ". $arr [$i][2]." To ". $arr [$i][3]." Said:
"; Echo"
- ". $arr [$i][4]."
";}? >
Chat screen diagram: