PHP program hundred lines of code to quickly build a simple chat room _php Tutorial

Source: Internet
Author: User
Basic functions: Can log in, chat, record the number of online IP events, can control the font color of chat, automatically convert the URL of the chat as a link address, can customize the chat room title, advertising information and so on. Use text as a storage medium, and interest can refer to the code and expand it.

In fact, PHP as a fast-developing scripting language is great!

= = = Code = =

The following is the referenced content:
/**
* Night passers-by simple chat room
* Author: Heiyeluren
* Created: 2005-8-10 22:42
* Modified: 2005-8-11 23:25
*/
Error_reporting (7);
Session_Start ();
Header ("contenttype:text/html;charset=gb2312");
Define ("SCRIPT", $_server[' Script_name ']);
Define ("Chat_note", "./chat.txt");
Define ("Online_list", "./online.txt");
Define ("Ref_time", 5);
Define ("Chat_name", "Night Passerby chat room");
Define ("Ad_msg", "Today is Chinese Valentine's Day, wish everyone Happy Valentine's Day!!");

Get value
if (Isset ($_get[' action ')) &&!empty ($_get[' action ')) {
$action = $_get[' action '];
}

If you have already logged in, jump directly to the chat screen.
if (!isset ($_get[' action ')) && isset ($_session[' username ']) {
Header ("Location:".) SCRIPT. "? Action=chat ");
}

Login Tips
if (!isset ($_get[' action '))
{
if (!session_is_registered (' username '))
{
echo "

[ ". Chat_name. "] ©2005




";
Exit
}
}

Check Login
if ($action = = ' Login ')
{
if (Isset ($_post[' Login_user ')) &&!empty ($_post[' Login_user ']) {
$username = $_post[' Login_user ');
} else {
$username = "Tourist";
}
Session_register (' username ');
Save_online ($username, Get_client_ip ());
Header ("Location:".) SCRIPT. "? Action=chat ");
}

Start Chatting www.knowsky.com
if ($action = = "Chat")
{
$online _sum = Get_online_sum ();
echo " <title>[ ". Chat_name. "]</title>





". Ad_msg. "
[Current online: $online _sum]
&lt;iframe src="&amp;quot;.SCRIPT.&amp;quot;?action=say" name="say_win" width="800&lt;br"&gt;&lt;/iframe&gt;height=60 Scrolling=no frameborder=0>
";
}

Speaking interface
if ($action = = "Say")
{
echo " <title>[ ". Chat_name. "]</title>



";
}

Save talk
if ($action = = "Save")
{
if ($_post[' chatmsg ']!= "") {
Save_chat ($_post[' chatmsg '), $_session[' username '], $_post[' UserColor ']);
}
Header ("Location:".) SCRIPT. "? Action=say ");
}

Show Chat History
if ($action = = "Show")
{
echo "";
echo "
Content= ' ". Ref_time. "; Url= ". SCRIPT. "? Action=show ' > ';
if (file_exists (chat_note)) {
$chat _msg = @file_get_contents (chat_note);
echo $chat _msg;
} else {
echo "No one speaks at present";
}
}

Exit chat Room
if ($action = = "Logoff")
{
unset ($_session[' username ');
Session_destroy ();
Header ("Location:".) SCRIPT);
}

/* Basic function */

Save Chat record function
function Save_chat ($msg, $user, $color)
{
if (! $fp = fopen (Chat_note, "A +")) {
Die (' Create chat log file failed, please check if there is permission. ');
}
$msg = Htmlspecialchars ($msg);
$msg = Preg_replace ('/([http|ftp:\/\/]) * ([a-za-])
+\. ([a-za-z0-9_-]) +\. ([a-za-z0-9_-]) + (a-za-z0-9_) */', '
", $msg);
$msg = Preg_replace ('/([a-za-z0-9_\.]) +@ ([a-za-z0-9-])
+\. ([a-za-z0-9-]{2,4}) +/', ' n ', $msg);
$msg = Date (' h:i:s '). " [". $user."] Said:
". $msg."
\ r \ n ";
if (!fwrite ($FP, $msg)) {
Die (' Write chat record failed. ');
}
Fclose ($FP);
}
Write Online person information
function Save_online ($user, $ip)
{
if (! $fp = fopen (Online_list, "A +")) {
Die ("Failed to create an online list file, please check for permissions.");
}
$user = Str_replace ("|", "", $user);
$line = $user. "|". $ip. "|". Time (). " \ r \ n ";
if (!fwrite ($FP, $line)) {
Die ("Write online list failed.");
}
Fclose ($FP);
}
Get people online
function Get_online_sum ()
{
if (file_exists (online_list)) {
$online _msg = file (online_list);
Return count ($online _msg);
} else {
return 0;
}
}
Get Current Login User IP
function Get_client_ip ()
{
if ($_server[' REMOTE_ADDR ']) {
$cip = $_server[' remote_addr ');
} elseif (getenv ("REMOTE_ADDR")) {
$cip = getenv ("REMOTE_ADDR");
} elseif (getenv ("Http_client_ip")) {
$cip = getenv ("Http_client_ip");
} else {
$cip = "Unknown";
}
return $CIP;
}
?>

http://www.bkjia.com/PHPjc/364008.html www.bkjia.com true http://www.bkjia.com/PHPjc/364008.html techarticle basic functions: Can log in, chat, record the number of online IP events, can control the font color of chat, automatically convert the URL of the chat as a link address, can customize the chat room title, advertising letter ...

  • 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.