PHP Design Chat room step through _php Foundation

Source: Internet
Author: User
Tags chop current time manual

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: Full embodiment of individuality, Very suitable for old friends, what they say 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 {
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 {
$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

(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: Password:


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

(3) Set up the chat window JS

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 ' +count+ ' ]
');
Document.writeln ("[ all a>]
");
Document.writeln ("var J,name,club;
for (Var i=0;i {
if (list[i]!=null) {

Show the name of each person on the line
Document.writeln ("E= ' "+list[i]+" ' > "+list[i]+"
");
}
}
This.r.document.writeln ('

');

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 {
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 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

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 ("n");
Print ("n");

Main display screen, responsible for displaying chat content
Print ("n");


Online number screen
Print ("Print (" n");

The screen that sends the message, the Information command center, all instructions are issued here.
Print ("

Passively update the screen to handle the information sent
Print ("n");

/Active update screen, show yourself and other chat friends information
Print ("n");

Detect whether the screen is online, for abnormal departure, such as the crash, drop line and so on processing
Print ("n");
Print (" 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

?
The following 2 parameters are used to verify the correctness of the information
Print ("n");
Print ("n");
? >

Chat object, note plus ReadOnly property

What was sent last chat

Form text Box sent

2 to check the content of the JS

var dx = ';
function Checksay ()
{

Not allowed to send empty speeches
if (document.inputform.msg.value== ')
{
Document.inputform.msg.focus ();
return false;
}

Do not allow repeated speeches, same content, same object
if ((Document.inputform.msg.value==document.inputform.message.value)
&& (DOCUMENT.INPUTFORM.TALKTO.VALUE==DX))
{
Alert (' statements cannot be repeated ');
Document.inputform.msg.focus ();
return false;
}

Two statements can not be less than 1 seconds interval, or the number of words greater than the interval *3
T2= (New Date ()). GetTime ()/1000;
if ((T2-T1) <1) | | ((T2-T1) *3 {
Document.inputform.msg.focus ();
return false;
}

Update Time
T1=t2;

document.inputform.showsign.value=1;

Save Last statement
Document.inputform.message.value =document.inputform.msg.value;

Empty the contents of the speech
Document.inputform.msg.value = ';

Save Speech objects
Dx=document.inputform.talkto.value;

Positioning focus
Document.inputform.msg.focus ();

Return
return (true);
}

3, call the message to send the program, post the chat has entered the information

Speech by the messagesend.php processing completed, note that the output object is BL, that is, to handle the frame name of the statement, so as to ensure that the content of the page frame of the full

Expressions and movements

expression and action greatly enriched the fun of chatting, the general chat room mainly through 2 ways to send

(1) The method of the button menu

By selecting it in a fixed pull-down menu, find your own satisfied expression, then select it and press the Send button to issue

(2) by manual input code

For example, NetEase to manually input//hello representative of a welcome action, to//bye representative of the expression of a gesture of goodbye
Here we introduce the specific implementation of the menu, manual input does not need to introduce it! Unless you can't remember those ...

The realization of the choice of expression action in the 1 pull-down menu

In order to expand the convenience, we have made the expression of the data file, so it will be very convenient to expand later.

The expression action file is formatted as follows ($split represents the delimiter):

1234$split "1234567, where is my friend 1234!!!!! ”
? $split very puzzled to look at the object ...
//?? $split grasping, scratching and scratching the scalp, and did not come up with an idea.
//??? $split What's going on? What the hell is going on here?

The front//1234 represents the expression action code, and the expression behind the delimiter indicates that the object two words will be replaced with the name of the speaker in the display.

This code is used to write the corresponding expression action code of the menu selection into the Send bar.

This code generates a dynamic down menu

action ?$emotemsg = file ($emotefilename);for ($i =0; $i {$msg = Split ($split, $emotemsg [$i],99);Print (" $msg [1] ");}? >

This completes the process of selecting the action expression from the menu.

2 The process of the expression action in the speech processing program

messagesend.php

?

Read the expression action file
$emote 3 = file ($emotefilename);
$emote 3number = count ($emote 3);
for ($kk =0; $kk $emote 3number; $KK + +)
{

Split each expression action
$emote =split ($split, chop ($emote 3[$KK]), 99);

If the speech is equal to the expression action
if ($message = = $emote [0])
{

Replace the object in the expression action with the actual name of the chat object
$emote [1]=ereg_replace ("Object", " $talkto ", $emote [1]);

Change the content of the speech to the action expression
$message = " $name ". $emote [1];
Break
}
}
? >

In this way we realize the expression and action, if you do an automatic add action expression of the program function, will increase the entertainment chat!

Post picture

If you can add some beautiful pictures in the chat text ...
Image code generation and expression action, you can choose the menu can also be manually entered, here only give the format and code, no longer explain, please see the expression in the action part

1 file format

$picturefilename

1$split Brown Suit $splitxw20151.jpg$split
2$split Black Tuxedo $splitxw201534.jpg$split

Number + name + picture name +

2 program

My chat room uses the///gift+ number method to display the picture, notices is three backslashes, differs from the expression movement

?
Judge the contents of the speech the first 7 characters are///gift
if (substr ($message, 0,7) = = "///gift")
{

Gets the number of the picture, 2 characters starting from the 7th character (note that the character is numbered starting from 0)
$id = substr ($message, 7,2)-1;

Read into picture files
$giftmsg = file ($picturefilename);

Judge whether the number is valid
if ($id >= 0) && ($id count ($giftmsg))
{
Split Picture Row
$gift =split ($split, $giftmsg [$id],99);

Generate a picture of the speech content
$message = "or= $namecolor $name is sent to O ') target=d> $talkto a $gift[1]0 height=60 border=0> ";

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-->olor=ff0000> Secret talks $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.