Some skills of PHP

xiaoxiao2021-03-06  87

Prevents the page from being constantly refreshed: $ refreshLimitTime = 60; $ timeStamp = time (); $ cookieTime = $ timeStamp 3153600; if ($ timeStamp - $ _ COOKIE [ 'lastvisit_fresh'] <$ refreshLimitTime) {die ( 'Please do not Frequently refresh, thank you for cooperation. Brush new time '. $ Refreshlimittime.' second ');} setcookie (' Lastvisit_fresh ', $ TIMESTAMP, $ cookietime); ------- -------------------------------------------------- -------------------------- Pack the session into a simple shopping car

/ ***********************

Secate the session into a simple shopping car, please refer to the corresponding article of the PHP learning record.

Each product requires four properties of ID, name, price, count, if you want to add new properties, modify the appropriate section.

************************ /

Class tcart {

Var $ sortcount; // Product Category Var $ TOTALCOST; // Total Value VAR $ ID; // The ID (array) of each type of item (array) VAR $ name; // Name (array) VAR $ price; // Price of each type of product (array) Var $ count; / / The number of pieces of each type of item (array) var $ cost; // Value of each type of item (array)

// ****** Constructor function tcart () {$ this-> sortcount = 0; session_start (); // Initialize a session session_register ('sID'); session_register ('sname'); session_register ('Sprice '); Session_register (' Scount '); $ this-> Update (); $ this-> Calculate ();} // ******** Private, according to the SESSION value update class corresponding data Function Update () {Global $ SID, $ SNAME, $ SPRICE, $ SCOUNT; if (! Isset ($ SID) OR! Isset ($ SNAME) OR! Isset ($ SPRICE)) RETURN; $ this-> id = $ sID; $ this-> name = $ SNAME; $ this-> price = $ sprice; $ this-> count = $ scount; $ this-> sortcount = count ($ sID);} // ******** Private, calculated the value of each type of product in accordance with new data, total price of all products, total price ($ I = 0; $ 1-> sortcount; $ i ) {$ this-> cost [$ I] = $ this-> price [$ I] * $ this-> count [$ I]; $ this-> totalcost = $ this-> cost [$ I];}} // ****** ******** The following is the interface function // *** Add a item // Decision whether there is already blue, if there is, plus count, otherwise add a new product // first is the value of session And then call Update () and Calculate () to update member variables Function Addone ($ ID, $ NA, $ PR) {Global $ SID, $ SNA ME, $ SPRICE, $ SCOUNT; $ k = count ($ sID); for ($ I = 0; $ i <$ k; $ i ) {// First find this product IF ($ SID) [$ I] == $ ID) {$ SCOUNT [$ I] ; Break;}} IF ($ I> = $ K) {// No added a new product species $ sID [] = ID; $ SNAME [] = $ NA; $ SPRICE [] = $ PR; $ SCOUNT [] = 1;} $ this-> update (); // Update a member data $ this-> Calculate (); / Load a commodity Function Removeone ($ ID) {Global $ SID, $ SNAME, $ SPRICE, $ Scount; $ K = Count ($ SID); for ($ I = 0; $ i <$ K; $ I )

) {IF ($ SID [$ I] == $ ID) {$ SCOUNT [$ I] -; Break;}} $ this-> Update (); $ this-> Calculate ();} // Change Products Number of Function Modifycount ($ I, $ NCOUNT) {Global $ Scount; $ SCOUNT [$ I] = $ ncount; $ this-> Update (); $ this-> Calculate ();} // Empty a commodity Function Emptyone ($ I) {Global $ Scount; $ SCOUNT [$ I] = 0; $ this-> Update (); $ this-> Calculate ();} / ************ ************** Clear all items because the session_destroy () function is not supported in the win, so this emptying function is not perfect, but the number of each item is 0. If it is under Linux, you can use session_destroy () directly.

*********************************************** $ SID, $ SNAME, $ SPRICE, $ SCOUNT; $ K = Count ($ SID); for ($ I = 0; $ i <$ k; $ i ) {$ SCOUNT [$ I] = 0;} $ this-> Update (); $ this-> Calculate (); Session_unregister ('sID'); session_unregister ('sname'); session_unregister ('sprice'); session_unregister ('scount'); $ this-> sortcount = 0;} // Is there a product has been in the blue, parameter is The ID Function Incart ($ ID) {for ($ I = 0; $ I-> Sortcount; $ I ) {IF ($ this-> ID [$ I] == $ ID) Return True; } Return False;} // Some items in the Blue Position Function IndexOf ($ ID) {for ($ I = 0; $ I <-> Sortcount; $ I ) {ified ($ this-> id [ $ I] == $ ID) RETURN $ I;} Return 0;} // Take information about a commodity, the main work function // Return an associated array, the subscript is corresponding to ID, Name, Price, Count, COST Function Getone ($ y) {$ result [$ I]; $ result [name] = $ this-> name [$ I]; $ result [price] = $ this-> price [$ I]; $ result [count] = $ this-> count [$ I]; $ results [COST] = $ this-> cost [$ i]; return $ result; } // Take the total number of goods function getSortcount () {return $ this-> sortcount;} // Take a total value Function GetTotalcost () {Return $ this-> Totalcost;} // End class}?>

-------------------------------------------------- --------------- date string to a function by highlighting php code: 2003-06-0719 author: cw source: / by iwind.org coldwind / iwind / month / ccterran // This function is analyzed in the [Code] [/ code] tab, and outputs //, for example: // [/ code] ssssssssss'; // highlight ($ string); //?> // Reserved an incomplete label, such as [Code], no [/ code], output [code] Function Highlight ($ String) {$ Arr = Explode ("[Code]", $ String); $ TOTAL = SizeOf ($ ARR); for ($ I = 0; $ I <$ Total; $ I ) {IF (EREG "(. ) [/ code], $ arr [$ one]) = split (" [/ code] ", $ arr [$ i]," 2 "); HIGHLIGHT_STRING ($ Astr); Echo "$ BSTR";} else {if ($ I! = "0") {echo "[code]";} echo "$ arr [$ I]";}}} --- -------------------------------------------------- ------------ How to use a regular expression to represent Chinese? Since Chinese ASCII code has a certain range. So you can represented Chinese with the following regular expressions. / ^ [CHR (0xA1) -Chr (0xFF)] $ /

Here is an example of use:

$ Str = "Beyond PHP"; if (preg_match ("/ ^ [". "-". chr (0xff). "] $ /"] / ", $ str) {echo" This is a pure Chinese string ";} else {echo" This is not a pure Chinese string ";} ------------------------------ -------------------------------------------------- ------------------ use the PHP as a shell scripting language

- English original: Darrell Brogdon, Posted on http://www.phpbuilder.com/columns/darrell20000319.php3) Many people have thought about writing some timed-time letter from PHP, but there is no way to perform PHP When I went to PHPBUILDER, I found this article, so I would like to translate it (I have made some modifications), I hope to be useful to everyone. For the first translation article, please don't forgive me. -------------------------------------------------- -------------------------------- We all know that PHP is a very good dynamic web development language (speed speed The development cycle is short ...). But only a small number of people realized that PHP can also be very good as a language written shell script. When PHP is written as a language written shell script, he doesn't have Perl or Bash so powerful, but he has a good advantage. Especially those who are familiar with PHP but not very familiar with Perl. To use PHP as a shell scripting language, you must compile PHP as binary CGI, not Apache mode; compiled PHPs with binary CGI mode have some security issues, and see the PHP manual for solving (http: / / www.php.net). At the beginning, you may feel uncomfortable to write shell scripts, but will slowly: Writing PHP as a general dynamic web page and as a shell scripting language is that a shell script needs to explain in the first line of life The program path of this script: #! / Usr / local / bin / php -q We added parameters "-1" after the PHP executive file, so that PHP will not output httpheader (if still need to be a web page, Then you need to use the Header function to output httpheader. Of course, in the shell script, you still need to use PHP's start and end tags: Now let's take an example so that you can better understand the use of PHP as a shell scripting language: #! / Usr / local / bin / php -q The above program will simply output "Hello, World!" to the display.

First, pass the shell script running parameter to PHP: as a shell script, often add some parameters when running the program, PHP has an embedded array "$ argv" when the PHP is used as a shell script, which can be very convenient to use the "$ argv" array. Read the parameter of the shell script time ("$ argv [1]" corresponds to the first parameter, "$ argv [2]" corresponds to the second parameter, and so on. For example, the program below: #! / Usr / local / bin / php -q The above code requires two parameters when running, respectively, named, such as this run: [dbrogdon @ artemis dbrogdon] $ Scriptname.ph Darrell The brogdon shell script will output on the display: Hello, Darrell Brogdon! How are you today? [Dbrogdon @ artemis dbrogdon] $ In PHP as a dynamic webpage writing language, it also contains "$ argv" arrays, but there are some differences here. : When PHP is called as a shell scripting language, "$ argv [0]" corresponds to the file name of the script, and when the dynamic web page is written, "$ argv [1]" corresponds to the first parameter of querystring. .

Second, write a interactive shell script: If a shell script is just running itself, there is nothing to do so much. How to read the information input when the PHP is written for the shell script? Unfortunately, PHP does not read the function or method of the user input information, but we can follow the other language to write a function "read" "read" that read the user input information: Need to note that the above function can only be used for UNIX systems (other The system needs to make a corresponding change). The above function opens a file pointer and then reads a row that does not exceed 255 bytes (that is, the role of FGETS), then turn off the file pointer, returns the read information. Now we can use the function "read" to modify the programs you wrote before, making him more "interactive": #! / Usr / local / bin / php -q The program is saved, run, you may see an expected thing: the last line of input becomes three lines! This is because the information returned by the "read" function also includes the end of the user's end of the user, retains the last name and name, to remove the end of the end, you need to modify the "read" function: 3, the shell script containing PHP in other languages: Sometimes we need to include PHL scripts written in other languages. In fact, it is very simple. Here is a simple example: #! / Bin / bash echo this is the bash section of the code.

/ usr / local / bin / php -q << EOF EOF is actually calling PHP to resolve the following code, then output; then, Try the following code: #! / Bin / bash echo this is the bash section of the code.

/ usr / local / bin / php -Q << EOF EOF can see the code twice The only difference is the second time I use a variable "$ myvar". Try running, PHP actually gives an error message: "Parse Error: Parse Error In - On Line 2"! This is because the variables in Bash are also "$ myvar", and the Bash parser will replace the variables first, to solve this problem, you need to add "/" escape in front of each PHP variable, then The code just changed as follows: #! / Bin / bash echo this is the bash section of the code./usr/local/bin/php -q << EOF eof

Ok, now you can write your own shell script with PHP, I hope you all go well. If you have any questions, you can go to http://www.phpbuilder.com or http://www.zphp.com. -------------------------------------------------- ---------------------------------------- Weather forecast PHP code is actually from Sina

The currently supported city has Beijing Tianjin Hohhot Taiyuan Shijiazhuang Jinan Qingdao Hefei Shanghai Nanjing Hangzhou Fuzhou Nanning Haikou Guangzhou Xiamen Shantou Shenzhen Zhengzhou Wuhan Changsha Nanchang Harbin Shenyang Changchun Dalian Urumqi Yinchuan Xi'an Xining Lanzhou Chongqing Chengdu Guiyang Hong Kong Macau Taipei / ****** ********************************************************************************** / $ city = "Xi'an"; // Set urban data here

// Remotely open Sina weather forecast page $ filename = "http://weather.sina.com.cn/index.html"; $ fp = fopen ($ FileName, "R"); $ www = fread ($ fp); $ www = fread , 100000);

// Get the first time $ riqi = strstr ($ WWW, ""); $ Num = STRPOS ($ RIQI, "") - Strlen (" "); $ RIQI = Substr ($ RIQI, Strlen (" "), $ NUM);

// Get the second time $ RIQI2 = strstr ($ WWW, "colspan = 3>"); $ riqi2 = strstr ($ RIQI2, ""); $ Num = Strpos ($ RIQI2, " ") - Strlen (" "); $ RIQI2 = Substr ($ RIQI2, Strlen (" ), $ Num);

// Data acquired $ city = "". $ City. ""; $ www = strstr ($ www, $ city); $ ender = ""; $ num = STRPOS ($ WWW, $ ENDER); $ www = substr ($ www, 0, $ num); // ========= Processing forecast data start ======== $ www = Substr ($ WWW, 4); $ www = evtern ("", $ WWW);

// Define the processing function function sub ($ x) {$ Num = Strpos ($ x, ""); $ real = substr ($ x, 0, $ num); Return $ real;}

// Handling data $ www [1] = SUB ($ www [1]); // Time a weather condition $ WWW [2] = SUB ($ www [2]); // Time A Wind Type Wind $ WWW [3 ] = SUB ($ WWW [3]); // Time 1 Minimum Temperature $ WWW [4] = SUB ($ WWW [4]); // Time Two Weather Conditions $ WWW [5] = SUB ($ WWW [5 ]); // Time two wind to wind $ WWW [6] = SUB ($ WWW [6]); // Time two highest temperature

/ / ========= Processing forecast data is completed ========

/ / Format output, understand the brothers of HTML can be modified directly 嘿嘿. Echo ""; echo $ city. "City Weather Forecast:"; Echo $ RIQI; // Time One Echo "& NBSP "; Echo $ www [1]; echo" & nbsp wind direction: & nbsp "; Echo $ www [2]; ECHO" & NBSP lowest temperature: & nbsp "; Echo $ www [3]; echo" & nbsp & nbsp "; echo $ riqi2; // Time 2 Echo "& nbsp"; Echo $ WWW [4]; Echo "& NBSP Wind Treatment: & NBSP"; Echo $ WWW [5]; Echo "& NBSP Top Temperature: & NBSP"; Echo $ WWW [6]; Echo "

?> --------------------------------------------------------------------------------------------- ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 10.10>

ServerAdmin Webmaster@hostmachine.com

DocumentRoot / www/virtual.com/htdocs

Servername www.virtual.com

Userdir /www/virtual.com/htdocs

Scriptalias / cgi-bin / /www/virtual.com/cgi-bin/

ErrorLog / WWW/VIRTUAL.COM/logs/error_log

Customlog / www/virtual.com/logs/access_log common

Directory setting

Set whether to use PHP for this virtual host

PHP_ENGINE ON

# Set the debug level of PHP error message

PHP_ERROR_REPORTING 1

# Set whether to record the PHP error log

PHP_LOG_ERRORS ON

# Set the error log file for PHP

PHP_ERROR_LOG /WWWW/VIRTUAL.COM/logs/php_error_log

# Set the longest survival time of a PHP thread

PHP_MAX_EXECUTION_TIME 180

# Set the user temporary upload directory

PHP_UPLOAD_TMP_DIR /WWW/Virtual.com/htdocs/TMP

# Set the included header file

PHP_INCLUDE_PATH /WWW/VIRTUAL.COM/HTDOCS/InClude

Executable directory settings

Options Indexes FollowSymlinks

ALLOWOVERRIDE ALL

ORDER ALOW, DENY

ALLOW FROM ALL

---------------------------------------------- ----------------------------- Function FileExtName ($ fileEname) {$ RetVal = "" $ PT = Strrpos ($ filename, "."); if ($ Pt) $ RETVAL = SUBSTR ($ FileName, $ Pt 1, Strlen ($ FileName) - $ PT); Return ($ RETVAL);} -------------------------------------------------- ----------------------- Dynamic page to static output

Include "detail.php"; // execute the program

IF (isset ($ _ get ['AID')) {$ buffer = ob_get_contents (); // Remove output cache $ filename = $ _GET ['AID']. ". htm"; $ fp = fopen ($ filename, "w"); FWRITE ($ fp, $ buffer); // Write file fclose ($ fp); unlink ($ filename); // Remove file} OB_END_FLUSH (); // Output and turn off the output cache?> -------------------------------------------------- ------------------------ get the operating system and browser $ ip = $ _server ["remote_addr"];

// --------------------------------------- Function Osinfo () {OS = ""; $ Agent = $ _server ["http_user_agent"]; if (EREGI ('WIN', $ Agent) && Strpos ($ Agent, '95')) {$ OS = "Windows 95";} elseif (EREGI ('WIN 9X', $ Agent && Strpos ($ Agent, '4.90')) {$ OS = "Windows Me";} Elseif (EREGI ('Win', $ Agent) && EREG ('98 ', $ Agent )) {$ os = "windows 98";} elseif (EREGI ('WIN', $ Agent) && EREGI ('NT 5', $ Agent) {$ OS = "Windows 2000";} elseif (EREGI (' Win ', $ agent) && EREGI (' NT ', $ Agent) {$ OS = "Windows NT";} elseif (EREGI (' Win ', $ Agent) && EREG (' XP ', $ Agent) { $ os = "windows XP";} elseif (EREGI ('WIN', $ Agent) && EREG ('32 ', $ Agent) {$ OS = "Windows 32";} elseif (EREGI (' Linux ", $ Agent)) {$ OS = "Linux";} elseif (EREGI ('UNIX', $ Agent) {$ OS = "Unix";} elseif (EREGI ('Sun', $ Agent) && EREGI ('OS' , $ Agent)) {$ OS = "sunos";} elseif (EREGI (EREGI ('IBM', $ Agent) && EREGI ('OS', $ Agent) {$ OS = "IBM OS / 2";} elseif EREGI ('Mac', $ Agent) && EREGI ('PC', $ Agent) {$ OS = "Macintosh";} elseif (EREGI (EREGI ('PowerPC', $ Agent) {$ OS = "PowerPC"; Elseif (EREGI ('AIX', $ Agent) {$ OS = "AIX";} elseif (EREGi ('HP Ux ', $ agent) {$ os = "hpux";} elseif (EREGI (' NetBSD ', $ Agent) {$ OS = "NetBSD";} elseif (EREGI (' BSD ', $ Agent) { $ OS = "BSD";} elseif (EREG ('OSF1', $ Agent) {$ OS = "OSF1";} elseif (EREG ('Irix', $ Agent) {$ OS =

"Irix";} elseif

// ------------------------------------- Function Browseinfo () {$ Browser = ""; $ browserver = "; $ browsers = array (" Lynx "," Mosaic "," AOL "," Opera "," Java "," MacWeb "," WebExplorer "," OmniWeb "); $ Agent = $ _SERVER ["http_user_agent"]; for ($ I = 0; $ I <<($ i ) {IF (STRPOS ($ Agent, $ Browsers [$ I]) {$ Browser = $ Browsers [$ I ]; $ browserver = "";}}}} & (EREG ("Mozilla", $ Agent &&! EREG ("Msie", $ Agent) {$ TEMP = EXPLODE ("(", $ Agent); $ part = $ TEMP [0]; $ TEMP = EXPLODE ("/", $ part); $ browserver = $ TEMP [1]; $ TEMP = EXPLODE ("", $ browserver); $ browserver = $ temp [0]; $ Browserver = preg_replace ("/ ([/ d /.] )/","/ 1", $ browserver); $ browserver = "$ browserver"; $ browser = "netscape navigator";} if (EREG) Mozilla ", $ agent) && EREG (" Opera ", $ Agent) {$ TEMP = EXPLODE (" (", $ agent); $ part = $ temp [1]; $ temp = evLode (") ", $ Part); $ browserver = $ TEMP [1]; $ TEMP = EXPLODE ("", $ browserver); $ browserver = $ temp [2]; $ browserver = preg_replace ("/ ([/ d /.] )/ "," // 1 ", $ browserver; $ browserver =" $ browser Ver "; $ browser =" Opera ";} IF (EREG (" Mozilla ", $ Agent && EREG (" Msie ", $ Agent) {$ TEMP = EXPLODE (" (", $ AGENT); $ part = $ TEMP [1]; $ TEMP = EXPLODE (";", $ part); $ part = $ TEMP [1]; $ TEMP = EXPLODE ("", $ part); $ browserver = $ temp [2]; $ Browserver = preg_replace ("/ ([/ d /.] )/","// 1"

转载请注明原文地址:https://www.9cbs.com/read-106658.html

New Post(0)