•  本聊天室需使用頁(yè)框,您的瀏覽器無法使用 <\/body> <\/noframes> <\/frameset> <\/html><\/pre>

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

    <\/pre>

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

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

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

    有些使用者可能對(duì) UNIX 還不是很熟,在這兒先簡(jiǎn)介會(huì)用到的指令:<\/p>

    touch: 建立新檔案,或修改舊檔的最后更新日期。<\/p>

    echo 加上二個(gè)大于符號(hào): 將字串顯示轉(zhuǎn)向到指定的地方。<\/p>

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

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

    \".date(\"h:i:s\").\"<\/font>-\".$chatuser.\"<\/font>: \".$chattext; \r\n  $cmdstr=\"echo \"\".$chatstr.\"\" >> \".$chatfile; \r\n  if (!file_exists($chatfile)) passthru(\"touch \".$chatfile); \r\n  passthru($cmdstr); \r\n} \r\n><\/pre>
     \r\n
    

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

     \r\n method=post> \r\n \r\n匿稱:<\/td> \r\n\"><\/td> \r\n發(fā)言:<\/td> \r\n<\/td> \r\n<\/td> \r\n<\/tr><\/table> \r\n<\/form> \r\n<\/body> \r\n<\/html><\/pre>

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

     \">   < \/\/ 檔名: list.php require(\"env.inc\");\r\nif (!file_exists($chatfile)) {  echo \"尚未開張<\/body><\/html>\";   exit; }\r\n$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].\"
     \"; } unlink($uniqfile); > <\/body> <\/html><\/pre>

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

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

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

    $chatfilearray=file($uniqfile); \r\n$j=count($chatfilearray); \r\nfor ($i=1; $i<=$j; $i++) {\r\n  echo $chatfilearray[$j-$i].\"
     \"; \r\n} \r\nunlink($uniqfile);<\/pre>

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

    Home php教程 php手冊(cè) 一個(gè)PHP入門源程序代碼

    一個(gè)PHP入門源程序代碼

    Apr 18, 2017 pm 03:31 PM

    一個(gè)簡(jiǎn)單的PHP入門源程序,需要的朋友們可以看一下。

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

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

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

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

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

    除此之外,由于定期更新所有網(wǎng)友的留言,為了怕寫了一半因?yàn)?refresh 而被清掉尚未寫好的字串,因此將聊天室以 frame 的頁(yè)框技術(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> 本聊天室需使用頁(yè)框,您的瀏覽器無法使用 </body> </noframes> </frameset> </html>


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

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

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

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

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

    有些使用者可能對(duì) UNIX 還不是很熟,在這兒先簡(jiǎn)介會(huì)用到的指令:

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

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

    tail: 顯示檔案最后數(shù)行的資料,內(nèi)定值為十行,可使用減號(hào)加數(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),若有資料則將字串及當(dāng)時(shí)時(shí)間存入檔案中 (利用 UNIX 的轉(zhuǎn)向指令)。當(dāng)然,為了防止錯(cuò)誤,先檢查是否有檔案可存檔,若沒有則先 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ù)時(shí),馬上就可以讓所有的程式用到,這對(duì)開發(fā)網(wǎng)站來說,是蠻重要的方法,可以將網(wǎng)頁(yè)程式中都會(huì)出現(xiàn)的地方。例如 Copyright (C) 1996-2000 的字串,放在一個(gè)檔案上,到了新的一年,只要改一個(gè)檔,整個(gè)站都改了。

    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)容。若已有聊天資料,就抓出最后五十筆,在在另外的檔案中準(zhǔn)備顯示。

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

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

    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)

    How to get the current session ID in PHP? How to get the current session ID in PHP? Jul 13, 2025 am 03:02 AM

    The method to get the current session ID in PHP is to use the session_id() function, but you must call session_start() to successfully obtain it. 1. Call session_start() to start the session; 2. Use session_id() to read the session ID and output a string similar to abc123def456ghi789; 3. If the return is empty, check whether session_start() is missing, whether the user accesses for the first time, or whether the session is destroyed; 4. The session ID can be used for logging, security verification and cross-request communication, but security needs to be paid attention to. Make sure that the session is correctly enabled and the ID can be obtained successfully.

    PHP get substring from a string PHP get substring from a string Jul 13, 2025 am 02:59 AM

    To extract substrings from PHP strings, you can use the substr() function, which is syntax substr(string$string,int$start,?int$length=null), and if the length is not specified, it will be intercepted to the end; when processing multi-byte characters such as Chinese, you should use the mb_substr() function to avoid garbled code; if you need to intercept the string according to a specific separator, you can use exploit() or combine strpos() and substr() to implement it, such as extracting file name extensions or domain names.

    How do you perform unit testing for php code? How do you perform unit testing for php code? Jul 13, 2025 am 02:54 AM

    UnittestinginPHPinvolvesverifyingindividualcodeunitslikefunctionsormethodstocatchbugsearlyandensurereliablerefactoring.1)SetupPHPUnitviaComposer,createatestdirectory,andconfigureautoloadandphpunit.xml.2)Writetestcasesfollowingthearrange-act-assertpat

    How to split a string into an array in PHP How to split a string into an array in PHP Jul 13, 2025 am 02:59 AM

    In PHP, the most common method is to split the string into an array using the exploit() function. This function divides the string into multiple parts through the specified delimiter and returns an array. The syntax is exploit(separator, string, limit), where separator is the separator, string is the original string, and limit is an optional parameter to control the maximum number of segments. For example $str="apple,banana,orange";$arr=explode(",",$str); The result is ["apple","bana

    JavaScript Data Types: Primitive vs Reference JavaScript Data Types: Primitive vs Reference Jul 13, 2025 am 02:43 AM

    JavaScript data types are divided into primitive types and reference types. Primitive types include string, number, boolean, null, undefined, and symbol. The values are immutable and copies are copied when assigning values, so they do not affect each other; reference types such as objects, arrays and functions store memory addresses, and variables pointing to the same object will affect each other. Typeof and instanceof can be used to determine types, but pay attention to the historical issues of typeofnull. Understanding these two types of differences can help write more stable and reliable code.

    Using std::chrono in C Using std::chrono in C Jul 15, 2025 am 01:30 AM

    std::chrono is used in C to process time, including obtaining the current time, measuring execution time, operation time point and duration, and formatting analysis time. 1. Use std::chrono::system_clock::now() to obtain the current time, which can be converted into a readable string, but the system clock may not be monotonous; 2. Use std::chrono::steady_clock to measure the execution time to ensure monotony, and convert it into milliseconds, seconds and other units through duration_cast; 3. Time point (time_point) and duration (duration) can be interoperable, but attention should be paid to unit compatibility and clock epoch (epoch)

    How to pass a session variable to another page in PHP? How to pass a session variable to another page in PHP? Jul 13, 2025 am 02:39 AM

    In PHP, to pass a session variable to another page, the key is to start the session correctly and use the same $_SESSION key name. 1. Before using session variables for each page, it must be called session_start() and placed in the front of the script; 2. Set session variables such as $_SESSION['username']='JohnDoe' on the first page; 3. After calling session_start() on another page, access the variables through the same key name; 4. Make sure that session_start() is called on each page, avoid outputting content in advance, and check that the session storage path on the server is writable; 5. Use ses

    How does PHP handle Environment Variables? How does PHP handle Environment Variables? Jul 14, 2025 am 03:01 AM

    ToaccessenvironmentvariablesinPHP,usegetenv()orthe$_ENVsuperglobal.1.getenv('VAR_NAME')retrievesaspecificvariable.2.$_ENV['VAR_NAME']accessesvariablesifvariables_orderinphp.iniincludes"E".SetvariablesviaCLIwithVAR=valuephpscript.php,inApach

    See all articles