Use PHP to enhance Unix/Linux Website Functions

Source: Internet
Author: User
Tags php source code php3 file setcookie
If you are a network developer and maintenance engineer, you should be familiar with ASP. Since Microsoft introduced ASP technology
The powerful functions of dynamic websites have been widely used soon. However, ASP can only be used
Microsoft's own operating system and server system. At present, UNIX-based network systems still occupy the majority, while Linux
As the system is provided free of charge and open source code is very popular, its market share has rapidly increased, and it is said that it has reached about 17%;
Apache servers are always the mainstream server software for network sites. The emergence and gradual improvement of PHP
For Network developers of Unix/Linux systems, it is undoubtedly good news: We don't have to envy ASP any more.
It's easy to use, but unfortunately the complexity of Perl/C!

I. php features
PHP is called personal home page. It was released and used in 1995 and has become a mature software platform by 1998.
Product. The latest version is 4.0, and the commonly used version is 3.0. PHP is compiled in C language and can run on
Unix/Linux and Win9x/NT.
PHP is a cross-platform server-side embedded script technology. A PHP page is a text file,
Embedded PHP script statements in common HTML page files, which are interpreted and translated by the server into common HTML files.
And then transmitted to the client browser, which is the same as ASP.
PHP borrowed a lot of C, Java, and Perl syntax, and added a variety of PHP
Allows web developers to write dynamic pages quickly.
The PHP script statement section uses "<?" Start with "?>" End. "<?" And "?>" Between scripts
The statement is replaced by a common HTML Script after the server software calls the PHP script interpretation engine.
The author believes that the strongest and most representative feature of PHP lies in its database layer, which is built on many
Database Support, without the need for special extensions, makes it very easy to write database-based Web pages. Currently supported Databases
Oracle, adabas D, Sybase, filepro, msql, velocis, MySQL, Informix, solid, dBase,
ODBC, UNIX, DBM, PostgreSQL, and ms SQL Server. Each database has a set of functions for database access and operation.
For example, dbase_create, dbase_open, dbase_close, dbase_pack, dbase_add_record,
Dbase_delete_record, dbase_get_record, dbase_numfields, dbase_numrecords, and other functions. For MySQL
Mysql_close, mysql_connect, mysql_create_db, mysql_data_seek, mysql_dbname, mysql_db_query
And other functions.
PHP also provides a set of image functions to dynamically generate GIF image data streams and output them
In addition, PHP also provides socket, email, PDF file, his (hyperwave Information
Server), IMAP, LDAP (Lightweight Directory Access Protocol), file upload, NIS, and other functions.

2. Build a PHP application system platform
PHP can run on Unix/Linux + Apache server system, Win9x + PWS server system, or WINNT + IIS server system.
The following uses pwin98 + pws4.0 as an example to build a PHP learning and development environment.
(1) download php3 for Win9x/NT
To the, select downloads, and then select download the PHP source code and windows
Distribution: select a download site and select 3.0.11 Win32 binary. If you are in trouble
Use The downloaded file is named,
The size is 1.68 MB.
(2) install php3 for Win9x/NT
Installing php3 for Win9x/NTIS simple. You only need to extract to a directory. In the following
In the discussion, we assume that php3 is installed in the C:/program files/php3 directory.
(3) Configure php3 for Win9x/NT
To configure php3 for Win9x/NT, you must modify the system settings so that pws4.0 can use the php3 script engine to explain the PHP page.
1. copy the file php3-dist.ini in the C:/program files/php3 directory to the Windows 98 installation directory (such as C:/pwin98)
And change it to php3.ini.
2. Edit the php3.ini File
Edit the php3.ini file and change the "extension_dir" under "paths and directories" to "extension_dir ".
= C:/program files/php3; Change "browscap" under "browscap" to "browscap = C:/pwin98/systeminetsrv/browscap. ini "(pws4.0 installation directory file); add a string value under the primary key of HKEY_LOCAL_MACHINE/systemcurrentcontrolset/services/w3svc/parameters/scriptmap in the registry, whose name is
". Php3" (The php3 file extension) with the value "C:/program files/php3/php.exe % S % s ".
In this way, after the PWS server software is restarted, pws4.0 can use the php3 script engine to explain the PHP page.

Introduction to PHP script syntax
In the php3 file, the PHP script statement section uses "<?" Start with "?>" For the end, use "<?" And "?>" The
Is the PHP program segment.
The PHP syntax is similar to the C and Perl syntax and mainly comes from the C language. For example, the control flow statement is almost identical to the C syntax, and
Operators are similar to C, function definitions, and calls are similar to C.
The variable name in PHP must have a $ sign before it, such as $ var1 and $ var2. The variable scope of PHP is also similar to C, that is, the main function is considered by default.
The variable in the number is a global variable, and the variable in the function is a local variable.
In PHP, the echo and printf functions are used to output HTML content to HTML pages. Echo outputs a non-formatted string, while printf outputs
Returns the formatted string. For example, echo ("<br> This PHP page <HR>") and printf ("% s, % d", str1, int1 ).
If you are familiar with the C language syntax, it is not difficult to learn the PHP script syntax.

4. Create your own simple PHP page
The so-called PHP page is to insert some special scripting languages in common HTML page files and save them with the extension PHP
Therefore, you can easily create a simple PHP page.
The following is a simple PHP page file (test01.php3), which should not be hard to understand.
<! -- Test01.php3 -->
<Title> A simple php3 page </title>
<P> <? Echo ("PHP script engine has started to work for you! <HR> ");?>
<H1> the following four statements should appear, with the font size ranging from 4 to 7 <? For ($ I = 4; $ I <= 7; $ I ++) {?>
<Br> <font size = <? Printf ("% d", $ I)?> The font size of this line of statements is
<? Printf ("% d", $ I) ;}?> </Font>
Where, the statement <? Printf ("% d", $ I);?> Is to output the variable I value to the HTML file.
As shown in the preceding figure, you can create a common HTML page file and insert the PHP script language as needed.
The key is to be familiar with PHP script statements.
Although this page is simple, it can help you familiarize yourself with and understand the PHP syntax. You can use a browser to view the running effect and use it to view the source code.
For more information about the code function, see the HTML file after the PHP page is interpreted. Note that you must use HTTP to access the PHP page,
You cannot double-click a file name as an HTML page file to browse the file.
Http: // wangfajun/test01.php3, because the PHP page needs to be explained by server software such as PWS.

4. obtain user request information
To create a dynamic website page, it is essential to obtain user request information. PHP pages can use this information as some applications.
(Such as commercial order information) basis, and feedback on the applicant's application requirements.
On the ASP page, you can use its built-in object request to obtain the visitor's application information. On the PHP page, you can read
Array to obtain the application information of a visitor, such as form field and querystring. Request page
The request information sent in post mode can be obtained in the form of $ http_post_vars ["itemname "].
The request information can be obtained in the form of $ http_get_vars ["itemname"], and the cookie information can be obtained in the form of $ http_cookie_vars [
"Itemname. Generally, the form information is sent as post, which can be in the format of $ http_post_vars ["itemname "]
The query information is sent in get mode and can be obtained in $ http_get_vars ["itemname"] mode.
(1) One HTML or php3 page requests another php3 page for processing
In the following example, the applicant is required to fill in the full name, ID number,
Email address. Notify the reader of successful registration after receiving the application.
Create an application page for reader Membership registration, which can be an HTML page or a PHP page. This should not be a problem.
<! -- Apply.htm -->
<Title> apply for membership </title>
<H2> apply for membership </H2>
<Form method = "Post" Action = "test02.php3">
Full name: <input name = "fullname" size = "20" value = "Full name">
ID number: <input name = "universalID" size = "15" value = "888888888888888">
Email: <input name = "email" value = "">
<P> <input type = submit value = "Submit Application"> <input type = reset value = "Refill">

On the Application page, all the application data is filled in the form element, and the processing is specified in the form action.
The application page is test02.php3. When receiving the application, the data of the corresponding project can be changed from $ http_post_vars ["project name"]
"Project name" is the name of the form element, for example, "Full name" corresponds to "fullname", for example, "ID card number"
It should be "universalID ". Now we can use this information to create a successfully registered notification page test02.php3.
<! -- Test02.php3 -->
<Title> the application for membership is successful. </title>
<P align = "center"> <H2> <font color = "green"> the application for membership is successful.
</Font> </H2>
<P> dear <? Printf ("% s", $ http_post_vars ["fullname"])?> Reader: <br>
We have received your application and accepted your application. You have become an official reader member of our department. <br>
Your Reader number is CN <? Printf ("% s", $ http_post_vars ["universalID"])?>. We will use your email box
<? Printf ("% s", $ http_post_vars ["email"])?> To officially inform you. Since then, we will hold a reader salon event
And the location will also be notified to you through this mailbox.
Open the application page apply.htm with a browser, enter the corresponding data, submit the application, view the feedback, and then use the browser.
To view the source code, you can see the HTML file after the PHP page interpretation.

(2) A php3 page requests the same php3 page for processing
In another example below, login. php3 provides a user logon page that requires the user to enter the Logon account and password,
Then, use the form action attribute to send the table to the user for processing. In login. php3, the table will be sent based on the account and password.
Whether it is blank to determine whether to enter the page or feedback page, and give different prompts. If no account or password is entered, the system prompts "User
Log On, enter the user account and password. Otherwise, the user account and password are displayed.
<! -- Login. php3 -->
<Title> User Logon </title>
<? File: // the account or password is blank, prompting the user to log on and enter the user account and password
If (empty ($ http_post_vars ["customerid"]) & brvbar
Empty ($ http_post_vars ["password"])
<P align = center> <H2> User Logon. Enter the user account and password </H2> </P> <HR>
<Form method = "Post" Action = "login. php3">
User Account: <input name = "customerid" value = "">
User Password: <input name = "password" value = "">
<P> <input type = submit value = "Start Logon"> <input type = reset value = "Log on again">
Else file: // if you have entered your account and password, the user account and password are displayed.
Printf ("<p> <H2> User % s successfully logged on, and the password is % S. </H2> </P> ", $ http_post_vars [" customerid "],
$ Http_post_vars ["password"]);

(3) Obtain query parameter information on the php3 page
When the user uses XXXXX. php3? When sending a query request in paraname = paravalue mode, XXXXX. php3 can
Obtain the paraname information submitted by the user from $ http_get_vars ["paraname"]. You can also
Obtain the number of user request parameters from Count ($ http_get_vars) and process them accordingly.
When the user uses query. php3? When a query request is sent using customerid = 7777, query. php3 can
The value of customerid in $ http_get_vars ["customerid"] to obtain the query parameter is 7777,
Count ($ http_get_vars) is 1.
When the user uses query. php3? When a query request is sent using customerid = 7777 & Password = dogisdog
, Query. php3 can obtain the customerid value of the query parameter from $ http_get_vars ["customerid "].
7777. Obtain the value of the query parameter password from $ http_get_vars ["password"] As dogisdog,
Count ($ http_get_vars) is 2.
The following example (query. php3) is used to obtain the query parameters specified by the user and display them to the user. When
Query. php3? When you send a request in customerid = 1111 & startdate = & enddate =,
The result is as follows:
You have specified three query parameters as follows:
The value of customerid is 1st.
The value of startdate is 2nd.
The value of the enddate parameter 3rd is:
<! -- Query. php3 -->
<Title> query parameter processing </title>
$ Querycount = count ($ http_get_vars); // query the number of parameters
Printf ("<p> <H2> you have specified % d query parameters as follows: </H2>", $ querycount); // display the number of query parameters
File: // lists the query parameters and their values.
For ($ intindex = 0; List ($ key, $ Val) = each ($ http_get_vars); $ intindex ++)
Printf ("<p> <H2> value of the % d parameter % s: % S </H2>", $ intindex + 1, $ key, $ Val );

(4) Obtain users' cookies
Cookies can be transmitted between the server and the client. They record certain information of a specific user.
You can use $ http_cookie_vars ["cookieitemname"] to obtain your cookies. The following statement can be used to display
Show all cookies of a user:
$ Querycount = count ($ http_cookie_vars); // Number of cookies
Printf ("<p> <H2> you have % d cookies: </H2>", $ querycount); // display the number of cookies
File: // list each cookie and Its Value
For ($ intindex = 0; List ($ key, $ Val) = each ($ http_cookie_vars); $ intindex ++)
Printf ("<p> <H2> the value of cookie % d (% s) is % S </H2>", $ intindex + 1, $ key, $ Val );

(5) obtain server environment information
In PHP, the server environment information is given in the form of a group of global variables for PHP script programs to access.
Common server environment variables include:
$ Gateway_interfa
CE: Gateway Interface, such as cgi/1.1
$ Http_accept: List of MIME types acceptable to the client, for example */*
$ Http_accept_language: the language used by the server. "en" indicates English, and "ZH-CN" indicates Simplified Chinese.
$ Http_host: HTTP server name
$ Http_user_agent: the client browser type that sends the request. It is identified based on the user request information and browscap. ini,
For example, Mozilla/4.0 (compatible; MSIE 4.01; Windows 98)
$ Local_addr: name of the local server
$ Path_translated: The full name (including the actual path) of the script file, as shown in
C:/inetpub/wwwroot/php3/query. php3
$ Remote_addr: IP address of the client machine sending the request
$ Remote_host: name of the client machine sending the request
$ Request_method: Method for sending a request (post/get/head)
$ Script_name: the path and name of the called script file, for example,/php3/query. php3
$ SERVER_NAME: server name or IP address
$ Server_port: HTTP service port number, which is generally 80.
$ Http_referer: the URL of the page on which the client sends a CGI program request
$ Server_protocol: the protocol used by the server software, such as HTTP/1.1
$ Server_software: Name and version of the server software, for example, Microsoft-IIS/4.0

5. send messages to users
You can use the functions provided by PHP to flexibly control the content and methods of information sent to the client. For example, you can dynamically set feedback.
Information, set the customer's cookie, page location, and so on.
(1) Use PHP functions such as ECHO and printf to control the sent information.
The ECHO and printf functions can directly output content to HTML files sent to the client browser.
In the following example, selecolr. php3 requires the user to enter a color name. After the user confirms, send the application
Go to selecolr. php3 on the same page and display the color name in the specified color.
<! -- Selecolr. php3!>
<Title> select color </title>
If (empty ($ http_post_vars ["colorname"])
{// No color name is entered, prompting you to enter a color name
Echo ("<H4> you only need to select the color name for me to display </H4> ");
<Form method = "Post" Action = "selecolr. php3">
Color name: <input name = "colorname" value = "red">
<P> <input type = submit value = ""> <input type = reset value = "reselect">
Else // You have entered a color name and used the specified color to display the color name.
Printf ("<H2> the color you choose is <font color = % S> % S </font> </H2> ",
$ Http_post_vars ["colorname"], $ http_post_vars ["colorname"]);

(2) Use the header function to locate the URL page
For business reasons, the domain name or IP address of your website has changed, just as Borland has changed
Like Inprise, old customers can use the header function to direct access to the new
If you are required to access another page before accessing this page, you can also use the header Function
Instead of sending information that the user has no permission to access the page.
It is worth noting that the header function must be used before sending any information to the client, otherwise it will cause an error.
Generally, the first statement on the page directs the user to another URL.
In the following example, if the user calls this page, it is directed to another page, selecolr. php3.
File: // locate selecolr. php3 on the page, and use exit to end the page processing.
Header ("Location: selecolr. php3"); exit;
<! -- Redirect. php3!>
<Title> Redirection </title>

<P> you cannot enter this page. Otherwise, it is my failure.

(3) Use the header function to change the page content Interpretation Method
The following sentence is often seen in the HTML page code.
<Meta http-equiv = "Content-Type" content = "text/html">
It is used to specify how the HTML page is interpreted by the browser. When content = "text/html", the browser
The Code is interpreted as a common HTML page and displayed to the user. When content = "text/plain", the browser does not add the code
Why is it displayed to the user in an explanatory manner? When content = "image/JPEG" is a graphic file, when it is not specified, contenttype
The default value is "text/html". On the PHP page, you can use the header function to change the contenttype type.
The browser interprets HTML text. For example, header ("contenttype: image/GIF") indicates that the page returns a result
A GIF image.

(4) use the setcookie function to Set Client cookies
You can add, modify, or delete client cookies by setting the setcookie function. When the cookie to be set does not exist,
Creates a new cookie with the value to be set. When a cookie exists, it replaces the original value with the new value.
It is worth noting that the cookie should be set before the header of the page is sent.
In the following example, Cookie. php3 is used to set the value of a cookie named wfj.
The value of the cookie (wfj) set last time.
File: // cookie. php3
$ Oldcookievalue = $ http_cookie_vars ["wfj"]; // The value of the cookie (wfj) set last time
If (! (Empty ($ http_post_vars ["cookievalue"])
{// If the value of cookie (wfj) has been entered, the cookie is set to display the message indicating that the setting is successful.
Setcookie ("wfj", $ http_post_vars ["cookievalue"]);
<Title> Cookie set successfully </title>

Printf ("<p> cookie (wfj) has been set by % s
S ", $ oldcookievalue, $ http_post_vars [" cookievalue "]);
Printf ("<p> what you will see next time ");
Else // If the cookie (wfj) value is not entered, the system prompts you to enter the cookie value.
<Title> cookie Settings </title>

Printf ("<p> cookie (wfj) = % s", $ oldcookievalue );
Printf ("<p> <HR> <H4> you only need to set the cookie for me to display </H4> ");
<Form method = "Post" Action = "Cookie. php3">
Cookie settings: <input name = "cookievalue" value = "wangfajun">
<P> <input type = submit value = ""> <input type = reset value = "reselect">
<? }?>

6. Mixed Programming of server scripts and client scripts
When you use <SCRIPT> </SCRIPT> to write a script program, if language = PHP is not specified
The script program is executed on the client. In this case, you can mix the main scripts on the server and client in the program.
In this way, you can flexibly control the execution of client scripts based on client requests, making the feedback to customers more personalized.
In hybrid use, the server script must start with "<??>" To show differences.
The following is an example of Mixed Programming of server-side scripts and client-side scripts (csscript. php3). In this example,
10 records will be output, and different functions will be customized for each record, so that each function can display different
Content, and occurs when the mouse is recorded.
<! -- Csscript. php3 -->
<Title> mixed programming of server-side scripts and client-side scripts </title>
<H1> when you move the cursor over the project name on each record, pay attention to the changes in the status bar content <Table border = "1">
<Tr> <TD> project name </TD> <TD> content of the status bar </TD> </tr>
<? For ($ intindex = 1; $ intindex <= 10; $ intindex ++) {?>
<TD onmouseover = "status <? Echo ($ intindex)?> () ">
<? Echo ($ intindex)?> </TD>
<TD> the mouse is now pointing to the project <? Echo ($ intindex)?> </TD>
<Script language = JavaScript>
<! --
Function status <? Echo ($ intindex)?> ()
Window. Status = "The mouse is now pointing to the project <? Echo ($ intindex)?> ";
7. Add comments
It is a good habit to add comments during programming. php allows you to add comments to the page code.
The language annotation syntax is the same, for example, the double slash "//" can be used as the annotator.

8. Use include to implement modular functions
You can store common function functions in a PHP file. When you use the functions in other PHP pages
The PHP file is included in the PHP file that calls the function. You can use the include function. The specific syntax is:
Include ($ filename );
1. Avoid self-inclusion, that is, file1 contains file1. avoid indirect self-inclusion when multiple files contain statements.
Contains loops. For example, file1 contains file2, file2 contains file3, and file3 contains file1.
2. The included scripting language must be of the PHP language type or the <SCRIPT> </SCRIPT> script statement segment.

The above is an entry-level knowledge for learning and using PHP to create dynamic and interactive sites.
Previous programming experience, hands-on learning and exploration of using PHP to create dynamic and interactive UNIX/Linux-based website Systems
The site's advanced network application system.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.