Home  >  Article  >  Backend Development  >  Quickly build a simple chat room with a hundred lines of PHP code_PHP tutorial

Quickly build a simple chat room with a hundred lines of PHP code_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 17:31:55949browse

When I was bored, I saw that I could build a chat room with less code. I wrote a full 100 lines to complete the chat room at first. Later, I felt that many functions were missing, so I revised it again and added a lot of code. In fact, without using particularly complex technologies, such as templates, databases, etc., you can still make a relatively good chat room, suitable for personal use.

Basic functions: can log in, chat, record the number of people online and IP events, can control the font color of the chat, automatically convert the URL in the chat into a link address, can customize the chat room title, advertising information, etc. Use text as the storage medium. If you are interested, you can refer to the code and expand it.

In fact, php (as the current mainstream development language) is great as a scripting language for rapid development!

===Code===

(as the current mainstream development language)
/**
* Simple chat room for passers-by at night
* 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, I wish everyone a Happy Valentine's Day!!");

//Get value
if (isset($_GET[action]) && !empty($_GET[action])) {
$action = $_GET[action];
}

//If already Log in and jump directly to the chat interface
if (!isset($_GET[action]) && isset($_SESSION[username])) {
header("location:".SCRIPT."?action=chat" );
}

//Login prompt
if (!isset($_GET[action]))
{
if (!session_is_registered(username))
{
echo "

[ ".CHAT_NAME." ] © 2005


form action=".SCRIPT."?action=login method=post>
Name:


";
exit;
}
}

//Verify login

if ($ action==login)
{
if (isset($_POST[login_user]) && !empty($_POST[login_user])) {
$username = $_POST[login_user];
} else {
$username = "Guest";
}
session_register(username);
save_online($username, get_client_ip());
header("location:".SCRIPT." ?action=chat");
}

//Start chat www.acnow.net

if ($action=="chat")
{
$online_sum = get_online_sum ();
echo "[ ".CHAT_NAME." ]




".AD_MSG."    [ Currently online: $online_sum]