Chat room based on PHP (ii)

Source: Internet
Author: User
Tags exit header php and regex expression
Chat Room < php

Open the file and truncate the file length to 0
$open _file = fopen ("messages.html", "w");

Write header information for a file
Fputs ($open _file, $header);

A new line
(Use stripslashes because we don't want all the escape characters to appear in the message file)
Fputs ($open _file, stripslashes ($new _message));

The old line
Fputs ($open _file, $old _messages);

Foot Mark
Fputs ($open _file, $footer);

Close File
Fclose ($open _file);

? >


Now we have files that need to be cross-referenced through the $REMOTE_ADDR variable, so that we can tell if the user who wants to post has been flattened or not flattened. Very simple:

< PHP

For ($counter =0 $counter < sizeof ($banned _array); $counter + +) {
if ($banned _array[$counter] = = $REMOTE _addr) {
Print ("< font color=" red "face=" Arial "align=" Center ">".
"You have been banned from-this chat</font >");
Exit
}
}

? >

The exit command stops the execution of the script immediately. Insert a check of the flattened user before starting to perform processing on the passed variables so that the flat user cannot use the chat room.

A better solution to the problem of dynamic IP address in some cases is to check the scope of the IP address block. A simple function can easily implement it.

< PHP

function Makemask ($IP) {
Remember to escape the. So PHP doesn ' t do it ' s a concatenation
$ip _array = Explode (".", $ip);
$ip _mask = "$ip _array[0]. $ip _array[1]. $ip _array[2]";
return $IP _mask;
}

? >

Then we replace the IF in the loop with the

< PHP

For ($counter =0 $counter < sizeof ($banned _array); $counter + +) {
if (Makemask ($REMOTE _addr) = = Makemask ($banned _array[$counter)) {
Print ("< font color=" red "face=" Arial "align=" Center ">".
"You have been banned from-this chat</font >");
Exit
}
}

? >

We have protective measures for dynamic IP addresses.
Finally we need a way to get the first annoying IP. My implementation is to log $name and $remote_addr to a
Iplist.html in the file. For a separate, secret URL, I can monitor the IP address while browsing the message. This can add some windfall benefits, that is, the ability to detect counterfeiters-the most common crime in these places.

Iplist.html and messages.html are basically the same way of creating. First take the current value out of the iplist.html, we peel back the turn information, pin and old IP records, and then create a new record, new header information, and a new PIN. To make the layout clearer, I used the table.

< PHP

$header = "< HTML >< body bgcolor=" #000000 "text=" #ffffff ">< table border=" 0 ">";
$footer = "</table ></body >$new _ip = "< tr >< td > $name </TD >< td > $REMOTE _addr</td ></tr >";

$ip _array = File ("iplist.html");
for ($counter = 1; $counter < $counter + +)
$old _ips.= $ip _array[$counter];

? >

Simply writing the content to disk is the same as the message file, so we have a web chat room. Better cross-platform compatibility than Java, and no need for anything but a Web browser--I've heard that even Dreamcast works like this!

There are some things you might want to try to do yourself, including merging some common code snippets into functions, writing a script that automatically increases the number of troublesome people to a flattened list, and writes a regex expression that scans the message body for URLs and e-mails. and automatically convert them to links (as Outlook Express and ICQ do).

Try, experience fun, and get some experience. That's how I started in PHP and now I'm working on it.
I wish to chat happy!

< finished full >


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.