PHP Design Chat room step through

Source: Internet
Author: User
Tags array count current time reset split strlen window
Chat room | Design chat room can be used in a completely free way, you can enter it, say, no password, do not save your chat status, the advantage is: free, very suitable for tourists! Another way is to register a chat room, every person entering the chat room to enter their own username and password to enter! Advantages: Fully embodies the personality, very suitable for old friends, their claim will not be malicious occupation of use. My chat room uses the registration Method!

Registration typically takes 2 different ways:

1, register and then enter the chat
  
2, Automatic registration

And then in the inside to modify their own information! I use the 2nd Method!! The username of each newly entered chat friend will be automatically saved to the database, the next login must enter the exact password to enter!

Here is the Judgment section! This program uses a text database!

$useronline name of the data file for the online person
$useronlinelock lock sign for an online person
$register the name of the data file that has been registered
$registerlock the lock flag for the registration file
$split as Separator

Login parameter Enter
if ($action = = "Enter")
{
The number of seconds in the current time
$timecurrent = Date ("U");

Lock online number files to prevent simultaneous modification of the same file
while (File_exists ($useronlinelock))
{
if (!file_exists ($useronlinelock))
{
Break
}
}

Create temporary files
Fclose (fopen ($useronlinelock, "w"));

Read information about online users and registered users: password, nickname, update time
$useronline = file ($useronline);
$register = file ($register);

A sign to determine whether a landing is successful
$namesign = 0;

The user name, password error, user name is not allowed to be empty, not allowed more than 10 characters, password does not allow more than 20 characters
if (($name = = "") | | (Strlen ($name) > 10) | | (Strlen ($pass) > 20))
{
Print ("No nickname or password too long");
Landing failed
$namesign = 1;
Delete temporary files
Unlink ($useronlinelock);
}
Else
{
Find out if someone has registered or a bad password
$foundsign = 0;
for ($i =0; $i <count ($register); $i + +)
{
Segmentation
$tempregister = Split ($split, $register [$i],99);
Locate the user name that is already registered
if ($name = = $tempregister [0])
{
The flag has been found
$foundsign = 1;
Is the password correct?
if ($pass!= $tempregister [1])
Print ("Wrong password!");
Landing failed
$namesign = 1;
Unlink ($useronlinelock);
Break
}
Else
{
Successful landing of the old user
$namesign = 0;
Break
}
}

}

If this user name is not found, it is automatically registered
if (! $foundsign)
{
Save user name and password
$handle = fopen ($register, "a");
Fputs ($handle, "$name $split$pass$splitrn");
Fclose ($handle);
New User Login Successful
$namesign = 0;
}
}
}
if (! $namesign)
{
Update List of people online
$useronlinehandle = fopen ($useronline, "w");

Judge whether it's already inside, just refresh the page
$updatesign = 0;
for ($i =0; $i <count ($useronline); $i + +)
{
$usertemp =split ($split, Chop ($useronline [$i]), 99);
if ($name = = $usertemp [0])
{
Update flags
$updatesign = 1;
Fputs ($useronlinehandle, $useronline [$i]);
}
Else
{
Fputs ($useronlinehandle, $useronline [$i]);
}
}
If it's not inside, add it to the inside.
if (! $updatesign)
Fputs ($useronlinehandle, "$name $split$level$split$pass$split$timecurren
TRN ");
Fclose ($useronlinehandle);

Remove the Indentation
Unlink ($useronlinelock);

Landing success
}

Here, the user's verification has been completed, chat friends have been legally entered the chat room, carrier and password


Login

1, the basic elements of landing page

You can see the landing form in my bamboo leaves, here provides the most basic landing form single

(1) Landing form

<form method=post name=chatform action=chat/login.php?action=enter onsubmit= "B1_submit (); return true;" Target= " Howtodo ">

(a) The name of the chat form is chatform, I use Action=enter as the entrance to the chat room, if this parameter is not available, the landing page is displayed.

(b) to call B1_submit () to establish a chat window when the form is submitted

(c) The target window for the chat is the Howtodo window for B1_submit ()

(2) Form items

Nickname: <input type=text name=name size=15 maxlength= "10"
Password: <input type=password name=pass size=15 maxlength= "10"
<input type=submit name=submit value= landing style= "width:100"
<input Type=reset Name=reset value= re-add style= "width:50"


(a) Each form item must be set to the maximum allowable length maxlength

(3) Set up the chat window JS

<script language= "JavaScript" >
function B1_submit () {
Chat=window.open (', ' Howtodo ', ' status=no,scrollbars=no,resizable=no ');

Chat.moveto (0,0);
Chat.resizeto (Screen.availwidth,screen.availheight);
Chat.outerwidth=screen.availwidth;
Chat.outerheight=screen.availheight;
}

This code first opens a Howtodo window with no status bar, scroll bar, adjustable size! Then move to the upper-left corner of the screen and zoom to the allowed screen size.

Online number

I based on the online number of NetEase chat room to display the current number of online, the code explained as follows:
1. Set up an online list of people at the time of landing, put it behind the body

?
Lock online number Files
while (File_exists ($useronlinelock)) {$PPPP + +;}
Fclose (fopen ($useronlinelock, "w"));

Read into the list of people online
$useronline = file ($useronline);
Unlink ($useronlinelock);

Set up array list
Print ("Document.writeln" ("List=new Array (");
$k =count ($useronline);
if ($k >1)
{
for ($i =0; $i ($k-1); $i + +)
{
$usercurrent = Split ($split, $useronline [$i],99);
Name +,
Print ("' $usercurrent [0] ',");
}
$i = $k-1;
Process Last Name
$usercurrent = Split ($split, $useronline [$i],99);
Print ("' $usercurrent [0] ');
}
Array end
Print (")"); n ");
? >

2, display online number of JS

Document.writeln (' [Online number <font color=red> ' +count+ ' </font>] <br> ');
Document.writeln ("[<a Href=" Javascript:parent.cs (' everyone ') "> all </
a>] <br> ");
Document.writeln ("<font class= ' p9 ')";
var J,name,club;
for (Var i=0;i<list.length;i=i+1)
{
if (list[i]!=null) {

Show the name of each person on the line
Document.writeln ("<a Href=" Javascript:parent.cs (' "+list[i]+") "Titl
E= ' "+list[i]+" ' > "+list[i]+" </a> <br> ");
}
}
This.r.document.writeln (' </font>
3, change the chat object

Function cs (name)
{
if (this.d.document==null) return;
if (name== ' everyone ')
{
This.d.add (' everyone ');
This.d.document.inputform.talkto.value= ' everyone ';

Change focus
This.d.document.inputform.msg.focus ();
Return
}
for (Var i=0;i<list.length;i=i+1)
{
if (list[i]==name)
{

Change the Conversation object sent
This.d.document.inputform.talkto.value=list[i];
This.d.document.inputform.msg.focus ();
Return
}
}

Error
Alert (' This user is offline or has changed its nickname. ');
}

4. Delete a user

Function del (str)
{
for (Var i=0;i<list.length;i=i+1)
if (LIST[I]==STR)
{
Delete List[i];
count--;
}
}

5, add a user

function Add (STR1,STR2)
{
var l=list.length;
for (Var i=0;i<list.length;i=i+1)

If it is already inside the array, return
if (LIST[I]==STR1)
Return

Add a user
LIST[L]=STR1;
count++;
}

6, update the number of chat methods, the use of timers

var timerid=null;
var Timerrunning=false;

function Stop ()
{
Stop it
if (timerrunning) cleartimeout (Timerid);
Timerrunning=false;
}
function Start ()
{
Stop ();
Call to update the number of online programs
Write1 ();
}

function Write1 ()
{
... ... ... ...
Set the update time,
Timerid=settimeout ("Start ()", 30000);
Timerrunning=true;
}

This method is relatively simple to achieve the number of online display, of course, can also use to read the online person files to show the number of people online, but it will be more cumbersome to change the chat object.

Show

Now the WWW chat room basically all adopt frame way, can use frame also can use IFRAME to see a person like, my traditional way of using frame

Print ("<frameset rows=" *,110,0,0,0 "Border=0>n");
Print ("<frameset cols=" 660,118 "rows=" * ">n");

Main display screen, responsible for displaying chat content
Print ("<frame name=u src=about:blank frameborder=" NO "Noresize>n");


Online number screen
Print ("<frame name=r src=" About:blank "frameborder=" NO "");
Print ("</frameset> n");

The screen that sends the message, the Information command center, all instructions are issued here.
Print ("<frame name=d src=send.php?name= $name &&pass= $pass scrolling= ' No"
' frameborder= ' NO "noresize>n");

Passively update the screen to handle the information sent
Print ("<frame src=" About:blank "name=" BL ">n");

/Active update screen, show yourself and other chat friends information
Print ("<frame src=" About:blank "name=" Flush ">n");

Detect whether the screen is online, for abnormal departure, such as the crash, drop line and so on processing
Print ("<frame src=" About:blank "name=" Check ">n");
Print ("</frameset> n");

Because the program between the various pages are connected, so the display order is very important, you can see, I only send the page is not About:blank, the other pages of the display must first through the call to send the page to start.


command center

This is the command center of the chat room, and all instructions are to be sent here.
1, below is the basic send form code

<form name=inputform action= ' messagesend.php ' target= ' bl ' width=6
0 height=60 border=0> </font> ";

Picture Build Success Flag
$specialsign = 1;
}

}

The realization of the secret conversation

Each chat room offers a secret-talk function to facilitate the conversations that need to be made in private, and here's how my chat room can be privately discussed as follows:

1 Processing of sending information

Each private conversation should be displayed on the screen of your own and your private conversation, while others will not be able to see it. Here first the speech content is processed, and then saved to the speech file!

$split is a separator symbol

messagesend.php

?
Judge whether to speak privately
if ($secret = = "on")
{
In front of the statement add the mark!--+ Private chat Mark + object + Speaker +--> which + represents separator

The first mark for secret for private chat, followed by the chat object and their own name, to ensure that these two people can see
$message = "!--$splitsecret $split$talkto$split$name$split--><font C
olor=ff0000> Secret talks </font> $message ";
}
Else
{

If the first mark is open, the back is the same as the object and own name
$message = "!--$splitopen $split$talkto$split$name$split--> $message"
;
}

And then save it to a file.
? >

2 ways to show private chat

After each new speech read into the first processing judgment, in the active refresh program.

messageflush.php

?
Split speech content
$tempmessage = Split ($split, $message [0],99);

If speaking is not a secret conversation, or private chat object is oneself, or send this private chat is yourself, then show this sentence, otherwise do not show
if ($tempmessage [1]!= "secret") | | ($tempmessage [2] = = $name) | | (
$tempmessage [3] = = $name))
{

Show Private Chat
Print ("Parent.u.document.writeln" ("$message [0]"); RN ");
}
? >

In this simple way, we realize the private conversation,

Attention:

Before the preservation of the document processing, the statement of the judgment before and after the words "!--and--> so that when the screen display can not be special to deal with!"



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.