国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

Home php教程 php手冊 一個PHP入門源程序代碼

一個PHP入門源程序代碼

Apr 18, 2017 pm 03:31 PM

一個簡單的PHP入門源程序,需要的朋友們可以看一下。

引用“星空浪子”php中文文檔的例子:

聊天室,是 Web 站上打發(fā)無聊人士的秘密武器。同時,站長或其它人員也可以在這兒殺時間。甚至發(fā)生一段轟轟烈烈的網(wǎng)路戀情呢,就算沒有,起碼可以增加打字的速度。?
聊天室,其實就是多人共同使用的 CGI 程式。程式將每個人輸入的字串,依系統(tǒng)接收完成的時間整理過后,再送給各個使用者。而 Web 聊天室和 BBS 的聊天室不同的地方是 BBS 聊天室可以每收到一句話,就馬上分送給每位在聊天室的網(wǎng)路使用者;Web 由于 CGI 程式不能像 BBS 的 telnet 一直連線,Web CGI 必須以最快的速度將資訊送出,然后結(jié)束連線。會形成這種情形,就是因為 Web 聊天室還是使用 HTTP 傳輸協(xié)定,在 HTTP 實作的版本,無論是 0.9、1.0 或是 1.1 版都不能長期占據(jù)網(wǎng)路連線的 Port。

為了解決資料無法馬上傳輸?shù)膯栴},及更新訊息的問題,Netscape 在 3.0 版瀏覽器之后使用了新的技術(shù),而 Internet Explorer 也實作了這些 Netscape 研發(fā)出來的技術(shù)。Netscape 將它分成 Server Push 及 Client Pull 二種技術(shù)。Server Push 由 Web 伺服器將資料以多重 MIME 編碼,送給使用者端,目前較少網(wǎng)站使用這種方式;而 Client Pull 則利用了 HTML 的 meta 標簽,并利用 http-equiv="Refresh" 的屬性,表示資料要重新載入,至于載入時間,則利用 content 屬性來達成。

標簽通常都放在 .. 的區(qū)段中,以便讓瀏覽器可以僅早準備更新使用者端的網(wǎng)頁。下面為 meta 和 PHP 合用的例子,設(shè)定為每十五秒重新載入一次。

如果不用 Server Push 或是 Client Pull 來做聊天室,是否有其它的方法,讓 Web 的瀏覽器能聊天呢?答案是肯定的。可以使用 Java 或是 ActiveX (限 IE4、5) 來做甚至自行開發(fā)專屬的 Browser Plug-in 程式 (如奇摩的聊天室),不過這就和 PHP 沒有關(guān)系了,不是我們要的重點。

除此之外,由于定期更新所有網(wǎng)友的留言,為了怕寫了一半因為 refresh 而被清掉尚未寫好的字串,因此將聊天室以 frame 的頁框技術(shù)來做是有必要的。下例就是聊天室的主程式。

<html> <head> <title>聊天室</title> </head> <frameset rows="*,40" border=1> <frame src="list.php" name=list scrolling=auto> <frame src="post.php" name=post scrolling=no> <noframes> <body> 本聊天室需使用頁框,您的瀏覽器無法使用 </body> </noframes> </frameset> </html>


程式中以 frame 帶出二支 PHP 程式,建議將它們放在同一目錄之中,例如 /chatroom,以便日后維護。另外,為了 list.php 及 post.php 可以使用相同的變數(shù),下例將共通的變路路徑放在 env.inc 中,可以將它放在 /chatroom 或是 Web 伺服器 (如 Apache) 的 PHP include 設(shè)定路徑中。

<php // 檔名: env.inc $tempdir="/tmp/"; $chatfile="/tmp/abc"; >

聊天室的后端可以設(shè)計的很簡單,單純的使用檔案來做,也可以弄個資料庫,將聊天的內(nèi)容丟入,若是真的很在意系統(tǒng)效率,或許可以考慮使用 UNIX 的行程通訊 IPC 了。

本節(jié)即將使用者留言的內(nèi)容放入檔案中,在這兒的例子大部份都使用 UNIX/Linux 的外部指令。若系統(tǒng)無該指令 (或稱程式),請自行安裝相關(guān)程式。

實際上將資料丟入檔案中會比使用資料庫還快,若還很在乎速度,可以在 UNIX 機器中裝上 RAM Disk,再將檔案的存取路徑都設(shè)在該 RAM Disk 上,保證存取速度能滿足嚴苛的要求。在有些以高速度搜尋引擎為號召的網(wǎng)站,甚至將整個資料庫資料都放到 RAM Disk 中,馬上讓系統(tǒng)速度提高十倍百倍,而且 RAM 的價格和其它解決方案相比的話還算很便宜。若使用 Windows NT,那就沒辦法了,看微軟什么時候提供,或者用 Third Party 的產(chǎn)品了。

有些使用者可能對 UNIX 還不是很熟,在這兒先簡介會用到的指令:

touch: 建立新檔案,或修改舊檔的最后更新日期。

echo 加上二個大于符號: 將字串顯示轉(zhuǎn)向到指定的地方。

tail: 顯示檔案最后數(shù)行的資料,內(nèi)定值為十行,可使用減號加數(shù)字,修改欲顯示的行數(shù)。

下面為送出及處理留言字串的程式,程式用到 env.inc 的檔案。

<php 
// 檔名: post.php 
require("env.inc"); 
if (($chatuser!="") and ($chattext!="")) {
  $chatstr="<font color=8080ff>".date("h:i:s")."</font>-<font color=ff8080>".$chatuser."</font>: ".$chattext; 
  $cmdstr="echo "".$chatstr."" >> ".$chatfile; 
  if (!file_exists($chatfile)) passthru("touch ".$chatfile); 
  passthru($cmdstr); 
} 
>
<html> 
<body bgcolor=ffffff leftmargin=0 topmargin=0 marginheight=0 marginwidth=0> 
<form action=< echo $PHP_SELF; > method=post> 
<table border=0 width=100%><tr> 
<td align=right>匿稱:</td> 
<td><input type=text name=chatuser size=8 value="< echo $chatuser; >"></td> 
<td align=right>發(fā)言:</td> 
<td><input type=text name=chattext size=30 maxlength=500></td> 
<td><div align=right><input type=submit value="送出"></td> 
</tr></table> 
</form> 
</body> 
</html>


程式先檢查是否有輸入字串,若無匿名及發(fā)言內(nèi)容字串則顯示發(fā)言的表單 (Form),若有資料則將字串及當時時間存入檔案中 (利用 UNIX 的轉(zhuǎn)向指令)。當然,為了防止錯誤,先檢查是否有檔案可存檔,若沒有則先 touch 該檔,例中的檔案就是 /tmp/abc。

<html> <meta http-equiv="Refresh" content="5; url=< echo $PHP_SELF; >"> <meta content="text/html; charset=gb2312" http-equiv=Content-Type> <body bgcolor=ffffff leftmargin=0 topmargin=0 marginheight=0 marginwidth=0> < // 檔名: list.php require("env.inc");
if (!file_exists($chatfile)) {  echo "尚未開張</body></html>";   exit; }
$uniqfile=$tempdir.uniqid(rand()); $shellcmd="/usr/bin/tail -50 ".$chatfile. " > ".$uniqfile; passthru($shellcmd); $chatfilearray=file($uniqfile); $j=count($chatfilearray); for ($i=1; $i<=$j; $i++) {  echo $chatfilearray[$j-$i]."<br> "; } unlink($uniqfile); > </body> </html>

上面的程式就是使用 Client Pull 的技術(shù),每五秒就重新更新一次。同樣地,它也 require 共用的 env.inc 檔,要改變其中的變數(shù)時,馬上就可以讓所有的程式用到,這對開發(fā)網(wǎng)站來說,是蠻重要的方法,可以將網(wǎng)頁程式中都會出現(xiàn)的地方。例如 Copyright (C) 1996-2000 的字串,放在一個檔案上,到了新的一年,只要改一個檔,整個站都改了。

if (!file_exists($chatfile)) {  echo "尚未開張</body></html>";   exit; }
$uniqfile=$tempdir.uniqid(rand()); $shellcmd="/usr/bin/tail -50 ".$chatfile. " > ".$uniqfile; passthru($shellcmd);

程式先檢查有沒有使用者發(fā)送聊天內(nèi)容的檔案 /tmp/abc,若沒有就顯示尚未開張,等使用者送聊天內(nèi)容。若已有聊天資料,就抓出最后五十筆,在在另外的檔案中準備顯示。

$chatfilearray=file($uniqfile); 
$j=count($chatfilearray); 
for ($i=1; $i<=$j; $i++) {
  echo $chatfilearray[$j-$i]."<br> "; 
} 
unlink($uniqfile);

將檔案讀入陣列變數(shù) $chatfilearray 中,并以最后的資料最先顯示的方式送給瀏覽器端,當然可以使用對陣列排序的方法,但確定一定時最后存入的資料在最后面,將它排序?qū)嵲诤芾速M CPU 時間,因此就從最后 echo 到最前面的資料。使用完成還要用 unlink() 指令,將臨時檔殺掉。?

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undress AI Tool

Undress AI Tool

Undress images for free

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Why We Comment: A PHP Guide Why We Comment: A PHP Guide Jul 15, 2025 am 02:48 AM

PHPhasthreecommentstyles://,#forsingle-lineand/.../formulti-line.Usecommentstoexplainwhycodeexists,notwhatitdoes.MarkTODO/FIXMEitemsanddisablecodetemporarilyduringdebugging.Avoidover-commentingsimplelogic.Writeconcise,grammaticallycorrectcommentsandu

How to Install PHP on Windows How to Install PHP on Windows Jul 15, 2025 am 02:46 AM

The key steps to install PHP on Windows include: 1. Download the appropriate PHP version and decompress it. It is recommended to use ThreadSafe version with Apache or NonThreadSafe version with Nginx; 2. Configure the php.ini file and rename php.ini-development or php.ini-production to php.ini; 3. Add the PHP path to the system environment variable Path for command line use; 4. Test whether PHP is installed successfully, execute php-v through the command line and run the built-in server to test the parsing capabilities; 5. If you use Apache, you need to configure P in httpd.conf

PHP Syntax: The Basics PHP Syntax: The Basics Jul 15, 2025 am 02:46 AM

The basic syntax of PHP includes four key points: 1. The PHP tag must be ended, and the use of complete tags is recommended; 2. Echo and print are commonly used for output content, among which echo supports multiple parameters and is more efficient; 3. The annotation methods include //, # and //, to improve code readability; 4. Each statement must end with a semicolon, and spaces and line breaks do not affect execution but affect readability. Mastering these basic rules can help write clear and stable PHP code.

PHP 8 Installation Guide PHP 8 Installation Guide Jul 16, 2025 am 03:41 AM

The steps to install PHP8 on Ubuntu are: 1. Update the software package list; 2. Install PHP8 and basic components; 3. Check the version to confirm that the installation is successful; 4. Install additional modules as needed. Windows users can download and decompress the ZIP package, then modify the configuration file, enable extensions, and add the path to environment variables. macOS users recommend using Homebrew to install, and perform steps such as adding tap, installing PHP8, setting the default version and verifying the version. Although the installation methods are different under different systems, the process is clear, so you can choose the right method according to the purpose.

What is PHP and What is it Used For? What is PHP and What is it Used For? Jul 16, 2025 am 03:45 AM

PHPisaserver-sidescriptinglanguageusedforwebdevelopment,especiallyfordynamicwebsitesandCMSplatformslikeWordPress.Itrunsontheserver,processesdata,interactswithdatabases,andsendsHTMLtobrowsers.Commonusesincludeuserauthentication,e-commerceplatforms,for

Your First PHP Script: A Practical Introduction Your First PHP Script: A Practical Introduction Jul 16, 2025 am 03:42 AM

How to start writing your first PHP script? First, set up the local development environment, install XAMPP/MAMP/LAMP, and use a text editor to understand the server's running principle. Secondly, create a file called hello.php, enter the basic code and run the test. Third, learn to use PHP and HTML to achieve dynamic content output. Finally, pay attention to common errors such as missing semicolons, citation issues, and file extension errors, and enable error reports for debugging.

How Do You Handle File Operations (Reading/Writing) in PHP? How Do You Handle File Operations (Reading/Writing) in PHP? Jul 16, 2025 am 03:48 AM

TohandlefileoperationsinPHP,useappropriatefunctionsandmodes.1.Toreadafile,usefile_get_contents()forsmallfilesorfgets()inaloopforline-by-lineprocessing.2.Towritetoafile,usefile_put_contents()forsimplewritesorappendingwiththeFILE_APPENDflag,orfwrite()w

python if else example python if else example Jul 15, 2025 am 02:55 AM

The key to writing Python's ifelse statements is to understand the logical structure and details. 1. The infrastructure is to execute a piece of code if conditions are established, otherwise the else part is executed, else is optional; 2. Multi-condition judgment is implemented with elif, and it is executed sequentially and stopped once it is met; 3. Nested if is used for further subdivision judgment, it is recommended not to exceed two layers; 4. A ternary expression can be used to replace simple ifelse in a simple scenario. Only by paying attention to indentation, conditional order and logical integrity can we write clear and stable judgment codes.

See all articles