PHP教程.應用實例16
Jun 21, 2016 am 09:14 AM教程|應用實例
基于PHP的聊天室(一)
這個經(jīng)過精心設計的web聊天室使用了從表單傳遞過來的變量,并且把它們處理為HTML ,然后將其寫入文件。把表單和信息文件放在一個框架中,你可以看到它與一個叫BeSeen的聊天室很象。當然它的好處在于, 我們的聊天室比起它的BeSeen堂兄弟來說要聰明一些。
上面就是基本的用于輸入的表單。你可能想把它弄得更美觀一些,但是不管出于什么目的,這個就是你要處理的。它發(fā)送兩個變量給chat.php3,分別叫做$name和$message。
不過,在處理那些變量之前,我們需要從消息文件中把當前的內(nèi)容取出來,否則在一個時間里我們只能看到一條消息了。幾乎沒有一個方法可以管理對話。只有象我一樣對自已的消息文件的結(jié)構很熟悉,我就知道每一條消息都以一個回車符結(jié)束。這就是說,可以使用file()函數(shù)來將消息文件讀到一個數(shù)組中去。
消息文件有12行。在12行中,第1行為頭信息,第2行到11行為舊的消息,第12行包含了我的腳標。
我最感興趣的是得到一個能夠包含所有那些舊消息的字符串。
// 把文件讀到一個數(shù)組中
$message_array = file("messages.html");
// 編輯字符串
for ($counter = 1; $counter $old_messages .= $message_array[$counter];
}
?>
在處理字符串的時候,我將for循環(huán)的$counter初始化為1而不是0。這是因為我知道$message_array 數(shù)組的第0個元素包含的是我的頭信息,我不需要它。而且,通過設置循環(huán)結(jié)束條件為$counter
現(xiàn)在已經(jīng)有了舊的消息,接著我想生成新的消息。我們已經(jīng)有了兩個變量:$name 和$message,所以寫出一個新的消息就很容易了。
"; ?>
我們就快要寫好消息文件了。剩下需要的就是頭信息和腳標。先加入簡單的頭部信息:
// 除了在字符串末尾有回車符,其它地方不能有回車符,這一點很重要。
// 要把所有的頭信息放在一樣。
$header = " ";
?>
我們想讓消息屏幕能夠自動刷新,那么人們就可以看到新的貼子了。我沒有使用JavaScript,而是采用了META標記進行刷新,主要是因為它可能更容易被客戶端支持。我也不想讓搜索引擎對我的消息文件進行索引。所以重新定義頭信息為:
$header = "".
"".
" ";
?>
在文件的腳標處,我一般是放置一小段版權信息,還有與打開的頭信息對應的結(jié)束標記。
$footer = "
".
"? Mike Hall 2000
?>
將版權信息用包起來表示只有被選中它才會被看見,因為它的顏色與背景色#000000一樣。這樣做的目的只是為了不讓它影響顯示。

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Many users will choose the Huawei brand when choosing smart watches. Among them, Huawei GT3pro and GT4 are very popular choices. Many users are curious about the difference between Huawei GT3pro and GT4. Let’s introduce the two to you. . What are the differences between Huawei GT3pro and GT4? 1. Appearance GT4: 46mm and 41mm, the material is glass mirror + stainless steel body + high-resolution fiber back shell. GT3pro: 46.6mm and 42.9mm, the material is sapphire glass + titanium body/ceramic body + ceramic back shell 2. Healthy GT4: Using the latest Huawei Truseen5.5+ algorithm, the results will be more accurate. GT3pro: Added ECG electrocardiogram and blood vessel and safety

Why Snipping Tool Not Working on Windows 11 Understanding the root cause of the problem can help find the right solution. Here are the top reasons why the Snipping Tool might not be working properly: Focus Assistant is On: This prevents the Snipping Tool from opening. Corrupted application: If the snipping tool crashes on launch, it might be corrupted. Outdated graphics drivers: Incompatible drivers may interfere with the snipping tool. Interference from other applications: Other running applications may conflict with the Snipping Tool. Certificate has expired: An error during the upgrade process may cause this issu simple solution. These are suitable for most users and do not require any special technical knowledge. 1. Update Windows and Microsoft Store apps

vue3 uses element-plus to call the message environment: vue3+typescript+element-plus1. After the global introduction of element, element has added the global method $message in app.config.globalProperties, so you can use mounted(){(thisasany) directly in the optionsAPI. $message.success("this.$message");}2. In the compositionAPI, the setup method passes in two variables props and

Part 1: Initial Troubleshooting Steps Checking Apple’s System Status: Before delving into complex solutions, let’s start with the basics. The problem may not lie with your device; Apple's servers may be down. Visit Apple's System Status page to see if the AppStore is working properly. If there's a problem, all you can do is wait for Apple to fix it. Check your internet connection: Make sure you have a stable internet connection as the "Unable to connect to AppStore" issue can sometimes be attributed to a poor connection. Try switching between Wi-Fi and mobile data or resetting network settings (General > Reset > Reset Network Settings > Settings). Update your iOS version:

php提交表單通過后,彈出的對話框怎樣在當前頁彈出php提交表單通過后,彈出的對話框怎樣在當前頁彈出而不是在空白頁彈出?想實現(xiàn)這樣的效果:而不是空白頁彈出:------解決方案--------------------如果你的驗證用PHP在后端,那么就用Ajax;僅供參考:HTML code

How to Optimize iPad Battery Life with iPadOS 17.4 Extending battery life is key to the mobile device experience, and the iPad is a good example. If you feel like your iPad's battery is draining too quickly, don't worry, there are a number of tricks and tweaks in iPadOS 17.4 that can significantly extend the run time of your device. The goal of this in-depth guide is not just to provide information, but to change the way you use your iPad, enhance your overall battery management, and ensure you can rely on your device for longer without having to charge it. By adopting the practices outlined here, you take a step toward more efficient and mindful use of technology that is tailored to your individual needs and usage patterns. Identify major energy consumers

Watch4pro and gt each have different features and applicable scenarios. If you focus on comprehensive functions, high performance and stylish appearance, and are willing to bear a higher price, then Watch 4 Pro may be more suitable. If you don’t have high functional requirements and pay more attention to battery life and reasonable price, then the GT series may be more suitable. The final choice should be decided based on personal needs, budget and preferences. It is recommended to carefully consider your own needs before purchasing and refer to the reviews and comparisons of various products to make a more informed choice.

Description Project Description Python Interpreter 3.10.6 Counter module In Python's collections module, a very commonly used module is Counter. Counter is a simple counter used to count the number of certain hashable objects. It stores elements and their counts in the form of a dictionary. Counter() class Counter() can count the parameters passed to this class according to certain rules, and return the results in the form of a dictionary by using the counting object and the counting result as key-value pairs. Counter(iterable=None,/,**kwds) gives an example fromcollectionsimport
