PHP3 Chinese document (turn)

Source: Internet
Author: User
Tags ereg expression file size file upload odbc mysql sybase microsoft iis
Chinese 1th Chapter PHP3 Introduction

What is PHP3?
The PHP3.0 version is a server-side html-embedded Script Description language.

What can PHP3 do?
Perhaps the most powerful and important feature of PHP3 is his database integration layer, which is incredibly simple to use to complete a Web page that contains database functionality. The databases listed below are currently supported.

Oracle
Adabas D
Sybase
Filepro
mSQL
Velocis
Mysql
Informix
Solid
DBase
Odbc
Unix dbm
PostgreSQL
 

A Brief History of PHP
PHP was conceived from the autumn of 1994, and his founder was Rasmus Lerdorf. The early unpublished version was used by him on his own web page to track who had visited his online resume. The first version that was used by others was released in 1995, when it was called the personal home Page Tools. He contains a very simple parsing engine that can only understand some of the specified macros and some of the common features of the home page background, such as message books, counters, and some other material. In the middle of the 1995, this parsing engine was rewritten and named Php/fi version 2.0. Fi comes from another package written by Rasmus that can accept HTML form data. He assembled the personal home Page Tools script and form interpreter, and added support for mSQL, so Php/fi 2.0 was born. Php/fi developed at an alarming rate, and others began to refine and modify his source code.

It is difficult to give any precise statistics, but it is estimated that at the end of 1996 at least 15,000 Web sites in the use of PHP/FI 2.0, in 1997 years, this number has grown to 50,000, 1997 years of PHP development has been some changes, He has become a more organised group project from Rasmus's pet project. The parsing engine is also rewritten by Zeev Suraski and Andi Gutmans, which form the basis of the PHP3. Most of the common code in PHP/FI is rewritten and introduced into the PHP3.

Today (1998), there are many commercial products such as C2 ' s stronghold Web server and Red Hat Linux all beginning to support PHP3 or PHP/FI, based on conservative inferences from the numbers provided by Netcraft, now around 150 in the world, 000 Web sites are using PHP or PHP/FI. In the foreground, these sites on the Internet are far more numerous than those running Netscape ' s flagship Enterprise server.

Using PHP3 for HTTP authentication
HTTP authentication is only available when PHP is running in the Apache module mode. In the Apache module PHP script, you can use the header () function to send a "authentication Required" message to the client in the browser, allowing the browser to eject a username/password (username/password) input window, When the user enters the username and password, the URL containing the PHP script will be called again, using the $php_auth_user, which represents the username, password, and confirmation method, $PHP _AUTH_PW, $PHP _auth_type variable. Only "BASIC" confirmation is now supported.

Examples of code snippets that force users to authenticate on a page are as follows:

 

Example 2-1. HTTP Authentication Examples:

<?php

if (!isset ($PHP _auth_user)) {

Header ("Www-authenticate:basic realm=\" "My Realm\");

Header ("http/1.0 401 Unauthorized");

echo "Text to send if user hits Cancel button\n";

Exit

}

else {

echo "Hello $PHP _auth_user.<p>";

echo "You entered $PHP _AUTH_PW as your password.<p>";

}

?>

In addition to the simple output $php_auth_user and $PHP _AUTH_PW variable values, you can also check the legality of the username and password, perhaps querying the database, perhaps searching for the user in the dbm file.

Beware of bug stacks of Internet Explorer browsers, he is very picky about the order of hearders. Therefore, it is a good solution to send Www-authenticate header request before sending out http/1.0 401 header request.

To prevent some people from writing scripts to display a password for a page that has been validated by a traditional external mechanism, the following method is used: If this page uses an external validation mechanism, the Php_auth variable will not be generated. In this way, $REMOTE _user variables can be used to represent users that have been validated by external mechanisms.

Note that the above method does not prevent someone from using an unauthenticated URL to steal the password of an authenticated URL on the same server.

Whether Netscape or IE, after receiving 401 replies from the server, the authentication cache for the local browser window is emptied. This approach can effectively use the user login to exit, forcing them to re-enter their username and password. Some people use this method to implement "timeout" registration, or to provide a button to log out.

This method is not required for standard HTTP Basic authentication, so you may never rely on it. Testing with Lynx does not identify the 401 server response, so if you use the forward or backward feature, you will open the source file (as long as your credit requirements have not been changed).

Although it has been noted that the language does not work on a Microsoft IIS server, the PHP language CGI version will be limited by IIS.

Create GIF using PHP
PHP is not only used to build HTML output. It can also be used to make GIF graphics files, and even to make a simpler GIF image stream. To do this, you will need to use the GD function library to compile the PHP source file.

 

Example 2-2. Use PHP to create GIF files

<?php

Header ("Content-type:image/gif");

$string =implode ($ARGV, "");

$im = Imagecreatefromgif ("Images/button1.gif");

$orange = Imagecolorallocate ($im, 220, 210, 60);

$PX = (Imagesx ($im) -7.5*strlen ($string))/2;

Imagestring ($im, 3, $px, 9, $string, $orange);

Imagegif ($im);

Imagedestroy ($im);

?>

This example will be used by a page similar to: method to Invoke. The script on BUTTON.PHP3 will overlay this piece of text on the basic graph (here is "images/button1.gif") and output the resulting graph. This is an easy way to prevent the user from drawing a button image again whenever they want to change the text of the key. Using this method, they will be used dynamically.

File Upload Support
PHP has the ability to accept file uploads for any RFC-1867 adaptation browser (including Netscape Navigator 3.0 or later, Microsoft Internet Explorer 3.0 (with patches) or later versions). This feature allows the user to upload text and binary files. Using PHP's authentication and file-handling functions, you have full control over who can upload files and what files can be uploaded.

You can build a file upload screen using a form-setting method similar to the one below.

 

Example 2-3. File Upload Form

<form enctype= "Multipart/form-data" action= "_url_" method=post>

<input type= "hidden" name= "max_file_size" value= "1000" >

Send This file: <input name= "UserFile" type= "File" >

<input type= "Submit" value= "Send File" >

</FORM>

where "_url_" points to a php HTML file. The max_file_size hidden domain must precede the file input field, and its value is the maximum file size that can be accepted. The unit of this value is bytes. In the destination file, the following variables are used to define whether the file was successfully uploaded:

$userfile: The name of the temporary file on the server where the uploaded file will be stored.

$userfile _name: The initial file name in the sender system.

$userfile _size: The size of the uploaded file, measured in bytes.

$userfile _type: Multipurpose Internet Mail Extension Protocol type of file, if the browser provides this information. One example is "Image/gif".

Note: Here the "userfile" section can be changed, and it can take any name as long as the type in the upload form is still "type=file". In the above example, we choose its name as "UserFile".

The file will be stored by default in the default temporary directory of the server. In the context of PHP running, you can change the environment variable TMPDIR to change this setting. When setting, use a putenv () function when your PHP script is not running.

The PHP script language that accepts uploaded files will implement what logical structure is necessary to decide what to do with uploaded files. For example, a user can use the "$file _size" variable to send any or too small or too large file to try the result. You can use the $file _type variable to send any files that do not meet the criteria for determining the type. Regardless of the logical structure, you need to delete the file in the temp directory, or remove it.

If the name is not changed or moved, the file will be deleted from the temporary folder at the end of the request.

Note that after obtaining the MIME connection type from the client, the "CERN httpd" software appears to be stripped of any content and appears as the original Whiteboard form. On this occasion, the "CERN httpd" software will not support the feature of uploading files.

HTTP Cookie Support
PHP is clearly supported by HTTP cookies. A cookie is a structure that stores data and tracks or identifies the returned user on a remote browser. You can use the function Setcookie () to set cookies. Cookies are part of the HTTP file header, so the Setcookie () function must be invoked before any output is sent to the browser. This condition is similar to the limit of the function header ().

Any cookies sent from the client computer to you will automatically be converted into a PHP variable similar to the Get and post method data. If you want to assign multiple values to a cookie, simply add "[]" after the cookie's name. See the Setcookie () function for more detailed questions.

Database support
PHP supports a large number of different types of databases, such as local mode and full ODBC, including:

Adabas D

Mysql

DBase

Oracle

Empress

PostgreSQL

Filepro

Solid

Informix

Sybase

InterBase

Velocis

mSQL

Unix dbm

Regular expression format
Regular expressions are used in PHP to handle complex text strings. Functions that support regular expressions are:

Ereg ()

Ereg replace ()

Eregi replace ()

Split ()

These functions use regular expressions as their first argument. PHP uses POSIX extended rule expressions (using POSIX 1003.2). To find all the descriptions of POSIX extended rule expressions, see the Regex man page included in the PHP release.

 

Example 2-4. Regular expression Examples

Ereg ("abc", $string);

/* Returns true if "abc"

is found anywhere in $string. */

 

Ereg ("^abc", $string);

/* Returns true if "abc"

is found at the beginning of $string. */

 

Ereg ("abc$", $string);

/* Returns true if "abc"

is found in the $string. */

 

Eregi ("(ozilla.[ 23]| MSIE.3) ", $HTTP _user_agent);

/* Returns true if client browser

Is Netscape 2, 3 or MSIE 3. */

 

Ereg ([: alnum:]]+) ([[: alnum:]]+) ([[: alnum:]]+)],

$string, $regs);

/* Places three space separated words

into $regs [1], $regs [2] and $regs [3]. */

 

Ereg_replace ("^", "<BR>", $string);

* Put a <BR> tag at the beginning of $string. */

Ereg_replace ("$", "<BR>", $string);

/* Put a <BR> tag on the end of $string. */

 

Ereg_replace ("\ n", "", $string);

/* Get rid of any carriage return

Characters in $string. */

Error action
There are four kinds of errors and warnings in PHP. They are:

Normal function Error: 1
Usually warning: 2
Parse Error: 4
Note (warns the user that this information can be ignored, but this problem may give your code some errors): 8
The four digits following the information are the representation values of the information type, which are added as the level of the error report. The report level of the province is 7 (that is, 1+2+4), or other combinations of "notes". This level can be changed by changing the method of error reporting indicated in the Php3.ini file. It can also be changed in the user's httpd.conf file by changing the method of PHP3 error reporting, or by using the scripting language function error Reporting () at run time.

All PHP expressions can be invoked using the prefix "@", which will turn off error reporting for this particular expression. If an error occurs while running such an expression, and the "track Error" attribute is made available, the user can find the error message in the global variable $php_errormsg.





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: info-contact@alibabacloud.com 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.