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

Source: Internet
Author: User
Tags define exit empty header interface log
Chat room

Boring case, see can use less code to build chat room, a start to write a full 100 lines to complete the chat room, then feel very multi-functional did not, and then revise, add a lot of code. In fact, do not use particularly complex technology, such as templates, databases, etc., can do a better chat room, suitable for personal use.

Basic functions: Can log in, chat, record the number of online IP events, can control the font color chat, automatic conversion chat URL for the link address, can be customized chat room title, advertising information and so on. Use text as a storage medium, and interest can refer to the code and expand.

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

= = = Code = =

<?php
/**
* 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 passers-by chat room");
Define ("Ad_msg", "Today is Chinese Valentine's Day, wish everybody Happy Valentine's Day!!");

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

If you've logged in, skip to the chat interface.
if (!isset ($_get[' action ')) && isset ($_session[' username ')) {
Header ("Location:"). SCRIPT. "? Action=chat ");
}

Landing Tips
if (!isset ($_get[' action '))
{
if (!session_is_registered (' username '))
{
echo "<p>

<p align=center>
<form action= ". SCRIPT. "? Action=login method=post>
: <input type=text size=25 maxlength=30 name=login_user>
<input type=submit value= Chat >
</form></p>
";
Exit
}
}

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

Start chatting
if ($action = = "Chat")
{
$online _sum = Get_online_sum ();
echo "<div style= ' border:1px solid #999966; width:802px;height:450 ' ><iframe src= '. SCRIPT. "? Action=show ' Name=show_win width=800 height=450 scrolling=auto frameborder=0></iframe></div><br >
<marquee width=70% scrollamount=2> ". Ad_msg. "</marquee> [current online: $online _sum]
<iframe src= ' ". SCRIPT. "? Action=say ' Name=say_win width=800 height=60 scrolling=no frameborder=0>
";
}

Speaker interface
if ($action = = "Say")
{
echo "<form action= ". SCRIPT. "? Action=save Method=post name=chat >
[". $_session[' username ']." said: <input type=text size=80 maxlength=500 name=chatmsg style= ' background-color: #99CC99; width:550px; height:22px; border:1px Solid: #000000 ' >
<select name=usercolor>
<option selected style= ' color: #000000 ' value= ' 000000 ' > Default color </OPTION>
<option style= ' COLOR: #000000 ' value= ' #000000 ' > Black silence </OPTION>
<option style= ' COLOR: #ff0000 ' value= ' #FF0000 ' > Red Passion </option>
<option style= ' COLOR: #0000ff ' value= ' #0000FF ' > Blue cheerful </option>
<option style= ' COLOR: #ff00ff ' value= ' #FF00FF ' > Peach romance </option>
<option style= ' COLOR: #009900 ' value= ' #009900 ' > Green Youth </option>
<option style= ' COLOR: #009999 ' value= ' #009999 ' > Cyan refreshing </option>
<option style= ' COLOR: #990099 ' value= ' #990099 ' > Purple prim </option>
<option style= ' COLOR: #990000 ' value= ' #990000 ' > Night excitement </option>
<option style= ' COLOR: #000099 ' value= ' #000099 ' > Blue Blues </option>
<option style= ' COLOR: #999900 ' value= ' #999900 ' > Khaki uniform </option>
<option style= ' COLOR: #ff9900 ' value= ' #FF9900 ' > Gilded years </option>
<option style= ' COLOR: #0099ff ' value= ' #0099FF ' > Lake Ripples </option>
<option style= ' COLOR: #9900ff ' value= ' #9900FF ' > shiny blue Violet </option>
<option style= ' COLOR: #ff0099 ' value= ' #FF0099 ' > ' Hint of love </option>
<option style= ' COLOR: #006600 ' value= ' #006600 ' > dark green deep </option>
<option style= ' COLOR: #333333 ' value= ' #333333 ' > Grey trajectory </option>
<option style= ' COLOR: #999999 ' value= ' #999999 ' > London Grey Mist </option>
</select>
<input type=submit value= ' speak ' style= ' Background-color: #ffffff ' > <a href= '. SCRIPT. "? Action=logoff title= exit chat room target=_top onclick= ' return confirm (\ "Are you sure you want to quit the chat room? \") ' > Exit </a>
</form>
<script>function Check () {if (document.chat.chatmsg.value== ') {; alert (' Please enter chat info! '); return false;} return true;} </script>
";
}

Save the 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 "<body style= ' font-size:12px ' >";
echo "<meta Http-equiv=refresh 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 Log function
function Save_chat ($msg, $user, $color)
{
if (! $fp = fopen (Chat_note, "A +")) {
Die (' Create chat log file failed, please check for permissions. ');
}
$msg = Htmlspecialchars ($msg);
$msg = Preg_replace ('/([http|ftp:\/\/]) * ([a-za-]) +\. ( [a-za-z0-9_-]) +\. ([a-za-z0-9_-]) + (a-za-z0-9_) */', ' <a href=\\0 target=_blank>\\0</a> ', $msg);
$msg = Preg_replace ('/([a-za-z0-9_\.]) +@ ([a-za-z0-9-]) +\. ([a-za-z0-9-]{2,4}) +/', ' <a href=mailto:\\0>\\0</a> ', $msg);
$msg = Date (' h:i:s '). " [". $user."] said: <font color= '. $color. "' > ". $msg." </font><br>\r\n ";
if (!fwrite ($FP, $msg)) {
Die (' Write chat log failed. ')
}
Fclose ($FP);
}
Write Online person information
function Save_online ($user, $ip)
{
if (! $fp = fopen (Online_list, "A +")) {
Die ("Create online list file failed, 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 online numbers
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;
}
?>



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.