The idea of chat room programming based on PHP

Source: Internet
Author: User
Tags array chop count current time interface reset split strlen
Programming | Chat Room 1 page landing basic elements
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= "Ten" >
Password: <input type=password name=pass size=15 maxlength= "Ten" >
<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.

Chat room Programming Idea--gate--Pass

Gate--Pass
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 is usually in 2 ways: 1, register and then enter the chat, 2, automatic registration, and then modify their own information inside! 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$split");
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$timecurrent");
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


Chat room Programming Idea--hall--display interface


Hall--Display interface
September 2000 04
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>");
Print ("<frameset cols=" 660,118 "rows=" * ">");

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

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

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>");

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

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

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

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.


Chat room Programming Idea--hall--Number of people online

Lobby--Number of online

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 array of online people when 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 (")"); ");
?>

2 show online number of JS
Document.writeln (' [Online number <font color=red> ' +count+ ' </font>]<br> ');
Document.writeln ("[<a href=" Javascript:parent.cs (' All Persons ') "> Everyone </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]+") "title=" "+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 How to update the number of chat, 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.


Chat room Programming Idea--command center--Send information

command center--Send information
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 ' >
?
Read the largest number of speakers
$message = file ($filename);
$number = Chop ($message [0]);

From the next statement that was last displayed to the end of the maximum statement, the contents of the statement
for ($i = $last +1; $i <= $number; $i + +)
{
Read the Next Statement
$filename = "Messageonline". ($i%10). " PHP ";
$message = file ($filename);
$tempmessage = Split ($split, $message [0],99);

Show the contents of the statement
Print ("Parent.u.document.writeln" ("$message [0]");
}

Update the number of the last speaker to send the form
Print ("parent.d.document.inputform.last.value= $number;");

Notifies the main program that this update is complete
Print ("parent.flushflag=true;");
?>
</script>


In this way, each sent speech, after being processed by a passive update, is saved into a file and then displayed by a circular active update
Service!!!



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.