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

Source: Internet
Author: User
Basic functions: logging in, chatting, recording online user IP events, controlling the chat font color, automatically converting the chat URL into a link address, and customizing the chat room title and advertisement information. If you are interested in using text as a storage media, you can refer to the code and scale it out.

In fact, PHP is a great scripting language for rapid development!

=== Code ===

<? Php
/**
* Simple chat room for night travelers
* Author: heiyeluren
* Creation:
* Modification:
*/
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. I wish you a happy Valentine's Day !! ");

// Obtain the value
If (isset ($ _ GET ['action']) &! Empty ($ _ GET ['action']) {
$ Action = $ _ GET ['action'];
}

// If you have logged on, jump to the chat page.
If (! Isset ($ _ GET ['action']) & isset ($ _ SESSION ['username']) {
Header ("location:". SCRIPT ."? Action = chat ");
}

// Logon prompt
If (! Isset ($ _ GET ['action']) {
If (! Session_is_registered ('username ')){
Echo "

<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> ";
Exit;
}
}

// Verify 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
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>
<Marquee width = 70% scrollamount = 2> ". AD_MSG." </marquee> [currently online: $ online_sum]
<Iframe src = '". SCRIPT ."? Action = say 'name = say_win width = 800 height = 60 scrolling = no frameborder = 0> ";
}

// Speaking Interface
If ($ action = "say "){
Echo "<Form action = ". SCRIPT ."? Action = save method = post name = chat onSubmit = 'Return check () '>
[". $ _ SESSION ['username']. "] Description: <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: # ff000000' value = '# ff000000'> Red enthusiasm </option>
<Option style = 'color: # 0000ff 'value =' # 0000FF '> blue and 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 </option>
<Option style = 'color: #990000 'value =' #990000 '> dark night excitement </option>
<Option style = 'color: #000099 'value =' #000099 '> deep blue and melancholy </option>
<Option style = 'color: #999900 'value =' #999900 '> card the uniform </option>
<Option style = 'color: # ff9900 'value = '# FF9900'> golden years </option>
<Option style = 'color: # 0099ff 'value =' # 0099FF '> Lake ripple </option>
<Option style = 'color: # 9900ff 'value =' # 9900FF '> shiny blue-violet </option>
<Option style = 'color: # ff0099 'value =' # FF0099 '> suggestion of love </option>
<Option style = 'color: #006600 'value =' #006600 '> dark green </option>
<Option style = 'color: #333333 'value =' #333333 '> gray track </option>
<Option style = 'color: #999999 'value =' #999999 '> London Fog </option>
</Select>
<Input type = submit value = 'talking' style = 'background-color: # ffff'> <a href = ". SCRIPT ."? Action = logoff title = exit chat room target = _ top onclick = 'Return confirm (\ "are you sure you want to exit the chat room? \ ") '> Exit </a>
</Form>
<Script> function check () {if (document. chat. chatmsg. value = '') {; alert ('enter the chat information! '); Return false;} return true;} </script> ";
}

// Save the speaker
If ($ action = "save "){
If ($ _ POST ['chatmsg ']! = ""){
Save_chat ($ _ POST ['chatmsg '], $ _ SESSION ['username'], $ _ POST ['usercolor']);
}
Header ("location:". SCRIPT ."? Action = say ");
}

// Display chat records
If ($ action = "show "){
Echo "<body style = 'font-size: 12px 'onload = 'scrolloads () '> ";
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 is talking ";
}
}

// Exit the chat room
If ($ action = "logoff "){
Unset ($ _ SESSION ['username']);
Session_destroy ();
Header ("location:". SCRIPT );
}

/* Basic functions */
// Function for saving chat records
Function save_chat ($ msg, $ user, $ color ){
If (! $ Fp = fopen (CHAT_NOTE, "a + ")){
Die ('failed to create the chat record file. Please check if you have the permission .');
}
$ Msg = htmlspecialchars ($ msg );
$ Msg = preg_replace ('/([httpftp: \ //]) * ([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. "]: <font color = '". $ color. "'> ". $ msg. "</font>
\ R \ n ";
If (! Fwrite ($ fp, $ msg )){
Die ('failed to write the chat record .');
}
Fclose ($ fp );
}
// Write in the informant information
Function save_online ($ user, $ ip ){
If (! $ Fp = fopen (ONLINE_LIST, "a + ")){
Die ("An error occurred while creating the online list file. Check whether you have the permission .");
}
$ User = str_replace ("", "", $ user );
$ Line = $ user. "". $ ip. "". time (). "\ r \ n ";
If (! Fwrite ($ fp, $ line )){
Die ("failed to write online list .");
}
Fclose ($ fp );
}
// Obtain the number of online users
Function get_online_sum (){
If (file_exists (ONLINE_LIST )){
$ Online_msg = file (ONLINE_LIST );
Return count ($ online_msg );
} Else {
Return 0;
}
}
// Obtain the IP address of the current Login User
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.