Ten advanced skills in PHP

zhaozj2021-02-16  91

Administrators in more than 3 million Internet sites worldwide are using PHP, making it the most popular server-side scripting language. It is characterized by fast running speed, stable and reliable, cross-platform, and is open source software. Unlike the level you use, PHP can be very simple, can also be complicated, you can use it to send HTML table elements, you can also integrate Java and XML in the PHP application.

If you have a certain understanding of PHP or have seen some preliminary textbooks, these techniques can expand your understanding of PHP and make you master some common and advanced PHP functions.

First, install PHP to apache's DSO

PHP is often used with Apache on the Linux / UNIX platform. When installing PHP, there are three installation methods to choose from: static mode, dynamic mode (DSO), CGI binary mode.

Because it is easy to maintain and upgrade, I strongly recommend installing PHP in DSO. For example, if the installed PHP only supports the database, then you want to install the encrypted module, just run "make clean", add a new configuration option, then run "make" and "make install", one The new PHP module will be installed in the appropriate location in Apache, then restart Apache without recompiling Apache.

The following steps will install a brand new Apache and install php in DSO:

1. Get the latest version of Apache source code from the Apache Software Foundation;

2, put the resulting source code in / usr / local / or / opt / directory, or in any of your specified directory;

3. Running Gunzip to decompress the file to get the file that suffix .tar;

4. Run the following command to install the file to the Apache_ [Version] directory:

Tar-xvf apache_ [version] .tar

5. Go to the / usr / local / apache_ [version] directory (or the directory of the compressed file in step 4);

6. Type the following command to prepare to compile Apache, replace [PATH] with your own path, for example, / usr / local / apache [version], now set up mod_so's new value, it will allow Apache to use DSO module;

7. When you return to the prompt state, type Make and wait back to the prompt status again;

8. Execute the "make install" command.

At this point, Apache has been installed and the system will return to the prompt state. Let's start installing PHP:

1. Find the latest version of the link in the download area of ​​the PHP home page;

2. Download the file to an appropriate directory, such as / usr / local / or / opt / or any directory you specify;

3. Running Gunzip to decompress the file to get the file that suffix .tar;

4. Execute the following command to install the file in the PHP- [Version] directory:

TAR-XVF PHP- [Version]

5, enter the / usr / local / php- [version] directory or the directory specified in step 4;

At this point, it has been prepared to install PHP in DSO, and the only configuration option that needs to be modified is with-apxs (this is a file in the bin directory of Apache). In order to get a high performance, I didn't install the support module for MySQL.

./configure --with-mysql = / [path to mysql] --with-apxs = / [path to apxs]

6. If you return to the prompt state, execute the make command, wait for the re-return to the prompt state; 7, execute the make install command.

At this point, the system is installed in the DSO mode in the Apache's module directory and returns to the prompt state after proper modification of Apache's httpd.conf file. After returning to the prompt state, you also need to modify the apache's httpd.conf file.

1. Find a line containing ServerAdmin, add your email address, as shown below:

ServerAdmin you@yourdomain.com

2. Find the line starting with ServerName and change it to a true value, for example:

ServerName Localhost

3. Find the section as shown below:

# And for php 4.x, use:

#

#Addtype application / x-httpd-php .php

#Addtype application / x-httpd-php-source .phps

Modify the contents of these rows, making the addtype of PHP 4.0 no longer a comment, and add the file suffix name that I want to use in PHP, the above content becomes as follows:

# And for php 4.x, use:

#

AddType Application / X-httpd-php .php .phtml

AddType Application / X-httpd-php-source .phps

Save the file, return to the previous directory, perform the following command to restart Apache:

./bin/apachectl start

If there is no error message when starting, you can test the installed Apache, PHP by creating a file named PHPINFO.PHP:

Save this file to the Apache 's Document Root (HTDOCS), then turn on the browser, type http: //localhost/phpinfo.php address, and there will be a lot of variables and their values.

If you want to reconfigure the PHP, you need to run the make clean command again, then perform the ./configure command with a series of options, then execute the make and make install commands, a new module appears in the Apache directory module, just re-re- Start Apache loading this new module, everything is OK.

Second, use the PHP itself dialogue

The most expectation of PHP 4.0 should be support for dialogue. Users of PHP 3.0 must use third-party software. Otherwise, they cannot use dialogue, and do not support dialogging has always been one of PHP's largest shortcomings.

As long as users are browsing your website, you can use dialogue maintenance with variables related to a specific user without having to create multiple cookies, using hidden table fields or store information in the database.

Start a conversation on a web page, you will know that the PHP engine knows that you want to start a conversation (if you have not started) or continue the current conversation:

session_start ();

Starting a dialog will send a identifier string to the user (for example, 940f8B05A 40D5119C030C9C9C7745AEAD9), at the server side, created a temporary file that matches the identifier string, such as SESS_940F8B05A40D5119C030C9C7745AEAD9, which contains registered dialog variables and their values.

The most common example used to display the role of the conversation is to access the counter. Start the PHP module, make sure the PHP code is the first line of the file, do not have spaces, HTML code, and other code before the PHP code. Because the conversation will send a head, if there is space and HTML code before session_start (), an error message will be obtained.

// If there is still no user who targets a user, start a conversation:

session_start ();

Then sign up for a variable name for count:

Session_register (count);

After registering a dialog variable, as long as the conversation exists, the variable of the name is COUNT also exists. Now, the count variable has not been assigned. If you perform an additional action to it, its value changes to 1.

$ count ;

Comprehensively synthesize the above content, if you haven't started a conversation, you will start a conversation; if there is no dialogue ID, you specify a good time, register a variable of $ count, do adding $ count 1 Operation indicates that the user has accessed the page for the first time.

To know the number of times the user accesses this page in the current dialog, as long as the value of $ count variable:

Echo "

youve been here count Times. ";

All access counter code as follows:

session_start ();

Session_register (count);

$ count ;

Echo "

youve been here count Times. ";

?>

If the above script file is reloaded, it will find that the value of the variable count has increased by 1, cool.

You can also register a array variable in the dialogue, assume that we registered a variable name $ FAVES:

$ FAVES = Array (Chocolate, Coffee, Beer, Linux);

You can register an array variable like a simple variable:

Session_register (failes);

Quote the array variables and reference simple variables have no differential. If a user points out the hobby in the webpage, you can register his hobbies into a group variable called $ FAVES, and then you can be on another web page. It is convenient to display these hobbies:

session_start ();

echo "My User Likes:

    While (List (, $ V) = Each ($ FAVES)) {

    Echo "

  • $ v";

    ECHO "";

    ?>

    Then you get a list about the user's hobbies.

    The dialog variable cannot be covered by the string, that is, we can't enter http://www.yourdomain.com/yourscript.php? Count = 56 Specify a new value to the registration variable $ count, this is important for security: You can only delete a registered dialog variable in the script of the server.

    If you want to completely delete a dialog variable, first you need to log out from the system:

    Session_unregister (count);

    The script to completely delete a dialog variable is very simple, as shown below:

    session_destroy ();

    Using dialog variables can reduce the frequency of accessing the database, making the code clearer, and can reduce the number of cookies sent by the user, which is the easiest way.

    Third, the document is our friend, regardless of the size of the website you develop, you should realize the importance of code reuse, whether these code is HTML, or PHP code. For example, you must change a footer that contains copyright information at least every year. If your website contains 1000 pages, it is also an annoying thing to modify each year.

    In PHP, at least there are at least a few functions can help you implement the purpose of the code reuse, the function used depend on the code you reuse, the main functions are:

    * incrude () and include_once ()

    * Require () and repeire_once ()

    The incrude () function contains and calculates a given file, for example:

    INCLUDE (/ home / me / myfile);

    Any code in the include file is executed within the code range of include (), you can use include () and fopen () in your own server, contain the target file on another server.

    The functionality of include_once () is the same as include (). The difference between the two is whether it checks if the code in a file is already included in an existing script. If the code already exists, it will not be included again.

    Require () function replaces itself with the content of a given file itself, this replacement process occurs during the PHP engine compile code, not during execution, it is not like include () will be calculated first. The Require () function is more used in static elements, and include () is more used in dynamic elements. Similar to include_once (), request_once () will first check if the given code is inserted, and if the code already exists, it is no longer inserted.

    In order to understand its content, in the elements of copyright information, static text, and other elements that do not contain variables or elements that rely on other ongoing scripts, I tend to use the Require function. E.g:

    Something </ title> </ hEAD></p> <p><Body></p> <p>[a Lot of Content]</p> <p><?</p> <p>// INSERT COPYRIGHT</p> <p>Require (/ home / me / mycopyright);</p> <p>?></p> <p></ Body></p> <p></ Html></p> <p>On the other hand, I often use include () to control many functions in the beginning of the file:</p> <p><?</p> <p>// Get a library</p> <p>INCLUDE (/ home / me / myfunctions);</p> <p>// Do PHP Things with my functions?></p> <p><Html></p> <p><Head> <title> Something </ title> </ hEAD></p> <p><Body></p> <p>[a Lot of Content]</p> <p></ Body></p> <p></ Html></p> <p>The next question is "Where is the include and the require file?", A simple answer to this problem is "" If you include a database connection with the username and password, you are sure They will not be placed in the document root directory to all people.</p> <p>Included or Required files can be available anywhere on the system, as long as the user on the system running in PHP can access these files, you can make these files any suffix, or do not use the suffix.</p> <p>Use include () and Require () to make a generalization of elements in the website, and bring you great convenience when you need to upgrade your website.</p> <p>Fourth, the maintenance of PHP and file system</p> <p>There are many functions related to the file system in PHP. These functions can not only open files, but also display content, mobile files, and other functions in the directory, and many people have even developed the Internet-based file resource manager with PHP. About the interpretation of the filepread: In Windows, you can use / and / symbols in the path, while only / symbols can be used in other operating systems. For consistency, we use / symbols.</p> <p>The following script sample can display a list of directory, the comment is already included in the code:</p> <p><? / * Stores the full path name of the directory you want to read into a variable of $ dir_name. * /</p> <p>$ DIR_NAME = "/ home / me /";</p> <p>/ * Create a handle that is the result of opening a given directory * /</p> <p>$ dir = OpenDir ($ DIR_NAME);</p> <p>/ * Create a text block to place list elements (file name) * /</p> <p>$ FILE_LIST = "<UL>";</p> <p>/ * Using a While statement, read all the elements in the open directory, if the name of the file is not "." Or "..", the name in the list * /</p> <p>While ($ file_name = readdir ($ dir)) {</p> <p>IF (($ file_name! = ") && ($ file_name! =". ")) {</p> <p>$ FILE_LIST. = "<li> $ file_name";</p> <p>}</p> <p>}</p> <p>$ FILE_LIST. = "</ ul>";</p> <p>/ * Close the open directory, end the PHP module * /</p> <p>CloseDir ($ DIR);</p> <p>?></p> <p><! - start your html -></p> <p><Html></p> <p><HEAD></p> <p><Title> Directory Listing </ Title></p> <p></ HEAD></p> <p><Body></p> <p><! - use php to print the name of the directory you read -></p> <p><P> files in: <? Echo "$ dir_name";?> </ P></p> <p><! - USE PHP to Print The Directory Listing -></p> <p><? echo "$ file_list";?></p> <p></ Body></p> <p></ Html></p> <p>Ok, we have got a list of directories. It should be noted that the contents of the system on the system that should be read by reading a file (later we will explain later), the user on the system of PHP must have at least read files.</p> <p>Here is an example of how to copy files:</p> <p><? / * Assign the full path of the original file you want to copy to a variable of the name $ Original, give a full path to the copy file to a variable of $ Copied * /</p> <p>$ othern = "/ home / me / mydatabaseDump";</p> <p>$ COPIED = "/ Archive / mydatabaseDumo_1010";</p> <p>/ * Use a copy () function to copy the original file, if the copy is not completed, an error message is displayed * /</p> <p>@Copy ($ Original, $ Copied) or Die ("COULDNT COPY FILE.");</p> <p>?></p> <p>This example is the prototype of a file backup system. When this script is run, it copies the file to a different location for saving. Slightly modify the daemon, you can execute it at the time you specified during the day without the need for user intervention. Assume that you install Lynx on the system, you can create a daemon entry to access this file, access this file, run this script and create a copy file, the following example will run this script at 5 o'clock in the morning, then close Lynx:</p> <p>0 5 * * * [username] lynx -dump http://localhost/copyfile.php 1> / dev / null 2> & 1</p> <p>If you run a CGI version of PHP, you can skip the Lynx section, and call the binary directly:</p> <p>0 5 * * * [username] php /Path/to/copyfile.php 1> / dev / null 2> & 1</p> <p>Five, rich array functions</p> <p>Add 30 functions related to the number of groups in PHP 4.0, some common functions can determine if an array contains an element, counts elements in an array, add or delete elements in an array or alignment groups The elements in the middle are sorted.</p> <p>If there is a big array, you need to find out if you contain a specific element, you can use IN_ARRAY (). The following example will display "Not Found In this Array", because in an array of $ Nameserray, you don't have such an element in the $ NameserRay array.</p> <p><? $ Nameserray = Array ("Joe", "Jane", "Bob", "Mary", "Paul", "EDDIE", "John");</p> <p>$ lookingFOR = "albert";</p> <p>IF (In_Array ($ LOOKINGFOR, $ NameserRay) {</p> <p>Echo "Youve Found IT!";</p> <p>} else {</p> <p>Echo "NOT Found in this Array!";</p> <p>}</p> <p>?></p> <p>If you change the value of $ lookingfor to Mary, you will get information about "Youve Found IT!" Because Mary is an element in the NameserRay array.</p> <p>If you want to count the number of elements in an array, just simply use the count () function:</p> <p><? $ Nameserray = Array ("Joe", "Jane", "Bob", "Mary", "Paul", "EDDIE", "John");</p> <p>$ count = count ($ nameserray);?>></p> <p>The value returned $ count is 7.</p> <p>You can add an element at the beginning or end of an array, and use array_merge () to create a new array of elements in two or more groups, when combined, the order of the elements is arranged in the specified order, if the original The array is sorted by sequence, and it needs to be reordered it after the merger.</p> <p>We can first add an element with array_push () at the end of the array:</p> <p><? / * Establish an array * /</p> <p>$ fruitArray = array ("Apple", "Orange", "Banana", "Kiwi", "PEAR");</p> <p>/ * Add an element to an array * /</p> <p>Array_push ($ FRUITARRAY, "GRAPE", "PineApple", "Tomato"); / * Shows each element and its serial number * /</p> <p>While (List ($ key, $ value) = Each ($ fruitaRray) {</p> <p>Echo "$ Key: $ Value <br>";</p> <p>}</p> <p>?></p> <p>Run the above program will get the following results:</p> <p>0: apple</p> <p>1: Orange</p> <p>2: Banana</p> <p>3: Kiwi</p> <p>4: Pear</p> <p>5: grape</p> <p>6: PineApple</p> <p>7: Tomato</p> <p>If you need to add an element at the beginning of the array, the code is similar to the above code, and the only difference is to replace Array_UNSH () in place_push ().</p> <p><?</p> <p>/ * Establish an array * /</p> <p>$ fruitArray = array ("Apple", "Orange", "Banana", "Kiwi", "PEAR");</p> <p>/ * Add an element to an array * /</p> <p>Array_unshift ($ FRUITARRAY, "Grape", "PineApple", "Tomato");</p> <p>/ * Show each element and its serial number * /</p> <p>While (List ($ key, $ value) = Each ($ fruitaRray) {</p> <p>Echo "$ Key: $ Value <br>";</p> <p>}</p> <p>?></p> <p>Run the above program will get the following results:</p> <p>0: grape</p> <p>1: PineApple</p> <p>2: Tomato</p> <p>3: apple</p> <p>4: Orange</p> <p>5: Banana</p> <p>6: Kiwi</p> <p>7: Pear</p> <p>Array_merge () function can combine two or more arrays into an array.</p> <p><? / * Establish the first array * /</p> <p>$ fruitArray = array ("Apple", "Orange", "Banana", "Kiwi", "PEAR");</p> <p>/ * / Establish a second array * /</p> <p>$ VEGARRAY = Array ("Carrot", "Green Beans", "Asparagus", "Artichoke", "Corn");</p> <p>/ * Combination of these two numbers and a array * /</p> <p>$ GoodFoodArray = Array_Merge ($ FRUITARRAY, $ VEGARRAY);</p> <p>/ * Show each element and its serial number * /</p> <p>While (list ($ key, $ value) = Each ($ GoodFoodArray) {</p> <p>Echo "$ Key: $ Value <br>";</p> <p>}</p> <p>?></p> <p>Run the above script will get the following result:</p> <p>0: apple</p> <p>1: Orange</p> <p>2: Banana</p> <p>3: Kiwi</p> <p>4: Pear</p> <p>5: Carrot</p> <p>6: Green Beans</p> <p>7: Asparagus</p> <p>8: Artichoke</p> <p>9: Corn</p> <p>Now we have mastered how to add an element and merge array, let's take a look at how to delete an element from an array. Delete an element from an array can use the Array_POP () function, using the array_shift () function to delete an element from an array. Although an element is removed from array_pop () or array_shift (), you can use this element as a variable.</p> <p>Use array_pop () to delete an element from the end of an array: <?</p> <p>/ * Establish an array * /</p> <p>$ fruitArray = array ("Apple", "Orange", "Banana", "Kiwi", "PEAR");</p> <p>/ * Delete an element from the end of the array * /</p> <p>$ popped = array_pop ($ fruitarray);</p> <p>/ * Show the contents of the deleted number of groups and your deleted elements * /</p> <p>While (List ($ key, $ value) = Each ($ fruitaRray) {</p> <p>Echo "$ Key: $ Value <br>";</p> <p>}</p> <p>echo "<br> And Finally, in $ Popped: $ POPPED";</p> <p>?></p> <p>Run the above script get the following results:</p> <p>0: apple</p> <p>1: Orange</p> <p>2: Banana</p> <p>3: Kiwi</p> <p>And finally, in $ Popped: PEAR</p> <p>Let's discuss an example of deleting an element from an array:</p> <p><?</p> <p>/ * Establish an array * /</p> <p>$ fruitArray = array ("Apple", "Orange", "Banana", "Kiwi", "PEAR");</p> <p>/ * Delete an element from one array * /</p> <p>$ shifted = array_shift ($ fruitaRray);</p> <p>/ * Show the contents of the deleted number of groups and your deleted elements * /</p> <p>While (List ($ key, $ value) = Each ($ fruitaRray) {</p> <p>Echo "$ Key: $ Value <br>";</p> <p>}</p> <p>echo "<br> And Finally, IN $ Shifted: $ Shifted";</p> <p>?></p> <p>Run the above script will get the following display result:</p> <p>0: Orange</p> <p>1: Banana</p> <p>2: Kiwi</p> <p>3: Pear</p> <p>And Finally, In $ Shifted: Apple</p> <p>There are also several functions to sort elements in arrays, but we will only briefly introduce basic sort functions, explaining the process of sorting:</p> <p><? / * Establish an array * /</p> <p>$ fruitArray = array ("Apple", "Orange", "Banana", "Kiwi", "PEAR");</p> <p>/ * Sort array * /</p> <p>Sort ($ fruitaray);</p> <p>/ * Show each element and its serial number * /</p> <p>While (List ($ key, $ value) = Each ($ fruitaRray) {</p> <p>Echo "$ Key: $ Value <br>";</p> <p>}</p> <p>?></p> <p>Run the above script gets the following display results:</p> <p>0: apple</p> <p>1: Banana</p> <p>2: Kiwi</p> <p>3: Orange</p> <p>4: Pear</p> <p>6. Creation of moving images</p> <p>Just install some third-party library files and have certain geometries, you can use PHP to create and process images. In fact, this doesn't need too much geometries, because our university does not graduate, still can use PHP to create images.</p> <p>You need to install the GD library file before using the basic image creation function. If you want to create a function related to JPEG, you will need to install JPEG-6B. If you want to use the Type 1 type font in the image, you must install T1LIB.</p> <p>Before establishing an image creation environment, you need some preparations. First, install T1LIB; secondly install JPEG-6B, then install the GD library file. Be sure to install in the order here, because JPEG-6B is used when compiling GDs is used, and if JPEG-6B is installed, it will be wrong when compiling. After installing these three components, you also need to reconfigure PHP, which is one of you who is featured by using DSO way to install PHP. Run Make Clean, then add the following content in the current configuration:</p> <p>--with-gd = [/ patH / to / gd]</p> <p>--with-jpeg-dir = [/ path / to / jpeg-6b]</p> <p>--with-t1lib = [/ patH / to / t1lib]</p> <p>After completing the addition, execute the make command, then execute the make install command. After restarting apache, run PHPINFO () to check if the new settings have taken effect. You can start the image creation work now.</p> <p>Depending on the version of the installed GD library file, you may or cannot create a graphic file in GIF or PNG format. If you install GD-1.6 or previous version, you can use the GIF format file but cannot create a PNG format, if Installing the version after GD-1.6, you can create a PNG file but cannot create a file in GIF format.</p> <p>Creating a simple image also requires many functions, we will explain step by step.</p> <p>In this example, we will create an image file in a PNG format. The following code is a header of the MIME type of image created:</p> <p><? Header ("Content-Type: Image / PNG");</p> <p>Create a variable representing a blank image using ImageCreate (), which requires parameters of image size in pixels, which is imagecreate (x_size, y_size). If you want to create an image of 250x250, you can use the following statement:</p> <p>$ newimg = imagecreate (250, 250);</p> <p>Since the image is still blank, you may want to use some colors to fill it. However, you need to first use the ImageColoralLocate () function to specify a name with its RGB value for this color, the format of this function is imagecolorallocate ([Image], [Red], [Green], [Blue]). If you want to define the sky, you can use the following statement:</p> <p>$ skyblue = imagecolorallocate ($ NEWIMG, 136, 193, 255);</p> <p>Next, you need to use the imagefill () function to populate this image with this color, and the imagefill () function has several versions, such as ImageFillRectangle (), ImageFillPolygon (), and so on. For simplicity, we use imagefill () functions through the following format:</p> <p>Imagefill ([Image], [Start X Point], [Start Y Point], [Color])</p> <p>Imagefill ($ NEWIMG, 0, 0, $ SKYBLUE);</p> <p>Finally, the image handle and the memory occupied after the image is established:</p> <p>ImagePNG ($ newimg);</p> <p>ImageDestroy ($ newimg);?></p> <p>In this way, all code to create images are as follows:</p> <p><? Header ("Content-Type: Image / PNG");</p> <p>$ newimg = imagecreate (250, 250);</p> <p>$ SkyBlue = ImageColoralLocate ($ NEWIMG, 136, 193, 255); ImageFill ($ NewImg, 0,0, $ SkyBlue);</p> <p>ImagePNG ($ newimg);</p> <p>ImageDestroy; $ newimg;</p> <p>?></p> <p>If this script file is saved as SkyBlue.php, you will see an image of a blue 250x250 PNG format with a browser.</p> <p>We can also use image creation functions to process images, such as making a large image into a small image:</p> <p>Suppose you have an image, you want to cut out a 35x35 size image from it. What you need is to create a blank image of a 35x35 size, create an image stream containing the original image, and then put a resized original image in a new blank image.</p> <p>The health function to complete this task is imageCopyResized (), which is required by the format:</p> <p>ImageCopyResized ([New Image Handle],</p> <p>[Original Image Handle],</p> <p>[New image x],</p> <p>[new image y],</p> <p>[Original Image X],</p> <p>[Original Image Y],</p> <p>[New Image X], [New Image Y],</p> <p>[Original Image X],</p> <p>[Original Image Y]).</p> <p><? / * Send a head so that the browser knows the content type included in the file * /</p> <p>Header ("Content-Type: Image / PNG");</p> <p>/ * Establish variables that save new images and width * /</p> <p>$ newWidth = 35;</p> <p>$ newheight = 35;</p> <p>/ * Establish a new blank image of a given height and width * /</p> <p>$ newimg = imagecreate ($ newwidth, $ newheight);</p> <p>/ * Get data from the original image * /</p> <p>$ Origimg = ImageCreateFromPng ("Test.png");</p> <p>/ * Copy the image after adjustment, use imageSx (), iMagesY () get the original image in x, y,</p> <p>ImageCopyResized ($ newimg, $ Origimg, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,/></p> <p>/ * Create an image you want to get, release memory * /</p> <p>ImagePNG ($ newimg);</p> <p>ImageDestroy ($ newimg);?></p> <p>If you save this short script as resized.php, then use the browser to access it, you will see a graphic of a 35x35 size PNG format.</p> <p>Seven, PHP-based user certification</p> <p>If you want to make password protection at each script, you can use the header () statement, $ PHP_AUTH_USER, and $ PHP_AUTH_PW to establish a basic authentication scheme, and the usual server-based question / response order is as follows:</p> <p>1. Users request a file from the server. If this file is protected on the server, a 401 (emptive user) string is returned to the user in the header of the response.</p> <p>2. After the browser receives this response, the dialog box that requires the user to enter the username / password.</p> <p>3. Users Enter a username and password in the dialog, and click the OK button to return the information to the server for authentication.</p> <p>4. If the username and password are valid, the protected file will be open to the user, as long as the user is still using the file, the certification will be effective.</p> <p>A simple PHP script file can simulate the HTTP's question / response system by sending an appropriate HTTP header that can cause the automatic display user name / password dialog box, PHP inputs the user in the User Name / Password dialog box. Information is stored in $ PHP_AUTH_USER and $ PHP_AUTH_PW, using these two variables, you can compare with usernames / password stored in files such as text files, databases. This example uses two hard-coded values ​​for authentication, but no matter where the username and password are placed, the principles are the same.</p> <p><?</p> <p>/ * Check the value in the PHP_AUTH_USER and $ PHP_AUTH_PW * /</p> <p>IF ($ PHP_AUTH_USER) || ($ PHP_AUTH_PW))) {</p> <p>/ * If there is no value, send a header that can initiate the dialog box * /</p> <p>HEADER (WWW-Authenticate: Basic Realm = "My Private Stuff");</p> <p>HEADER (http / 1.0 401 unauthorized);</p> <p>Echo authorization required .;</p> <p>EXIT;</p> <p>Else IF (($ PHP_AUTH_USER) && (isset ($ PHP_AUTH_PW))) {</p> <p>/ * Value in variables, check if they are correct * /</p> <p>IF ($ PHP_AUTH_USER! = "ValidName") || ($ PHP_AUTH_PW! = "Goodpassword")) {</p> <p>/ * If there is an incorrectly incorrect input username and password, send a header * / that can raise the dialog</p> <p>HEADER (WWW-Authenticate: Basic Realm = "My Private Stuff");</p> <p>HEADER (http / 1.0 401 unauthorized);</p> <p>Echo authorization required .;</p> <p>EXIT;</p> <p>Else IF ($ PHP_AUTH_USER == "VALIDNAME") || ($ PHP_AUTH_PW == "Goodpassword")) {</p> <p>/ * If the two values ​​are correct, the message is displayed * /</p> <p>Echo "<p> youre authorized! </ p>";</p> <p>}</p> <p>}</p> <p>?></p> <p>It should be noted that if you use a file-based protection mechanism, it does not guarantee the security of all files in the directory. It can protect most of the files, if you think it can protect all files in a given directory, your understanding needs to be changed.</p> <p>Eight, PHP and COM</p> <p>If you like to take risks, and run the CGI, ISAPI, or Apache module version of the PHP on Windows, you can access the COM function. Ok, explain in detail the work of COM and give Microsoft and many most books. In order to simply understand the COM's function, the following is a common script.</p> <p>This PHP script starts Microsoft word processing Word on the backend, open a new document, enter some text, save the document, and close the word.</p> <p><?</p> <p>// Establish an index pointing to the new COM component</p> <p>$ Word = New Com ("Word.Application" OR DIE ("Cant Start Word!");</p> <p>// Show the version number of the Word currently being used</p> <p>Echo "Loading Word, V. {$ Word-> Version} <br></p> <p>// Set its visibility to 0 (fake) If you want to make it open at the front end, use 1 (true) // to open the application in the forefront, use 1 (true)</p> <p>$ Word-> Visible = 0;</p> <p>// Create a new document in Word</p> <p>$ Word-> Documents-> Add ();</p> <p>/ / Add text in a new document</p> <p>$ Word-> Selection-> Typetext ("Testing 1-2-3 ...");</p> <p>// Save the document in the Windows Temporary Directory</p> <p>$ Word-> Documents [1] -> Saves ("/ Windows / Temp / Comtest.doc");</p> <p>// Turn off the connection between the COM assembly</p> <p>$ Word-> quit ();</p> <p>/ / Display additional information on the screen</p> <p>Echo "Check for the File ...";</p> <p>?></p> <p>If you have an intranet website, the data is stored in SQL Server, the user needs these data Excel formats, you can make the necessary SQL query and format the output, then use COM to open Excel, turn the data to Excel format Data, then save the data on the desktop of the user.</p> <p>Nine, PHP and Java</p> <p>Another interesting feature of PHP is that it can call the method in the existing Java object so that you can integrate PHP in Java-based applications. If you want to promote PHP applications in your work, this feature is very useful. The result you get is that everything here is based on Java. "</p> <p>To take advantage of this feature, you must have JVM (Java virtual machine) on your server. If you installed the JDK from Sun, Kaffe, IBM, or BlackDown, JVM has been installed.</p> <p>When configuring the PHP, you need to add a WITH-JAVA section in the configuration file, then modify part of the php.ini file, the modification of the php.ini file is mainly to add the following:</p> <p>[Java]</p> <p>Java.library.path = / path / to / librage</p> <p>Java.class.path = / classpath /</p> <p>EXTENSION_DIR = / Path / to / Extensions</p> <p>EXTENSION = libphp_java.so</p> <p>It should be noted that the modifications made are related to your installation type, you need to read the readme file in the EXT / JAVA directory, learn how to configure Java features.</p> <p>Below is an example of how to create a PHP script for a new Java object, which will access and display some Java properties on the display. It is also interesting with the example of COM, and you should give us some inspiration.</p> <p><?</p> <p>$ system = new java ("java.lang.system");</p> <p>Echo "P> Java Version =". $ system-> getproperty ("java.version"). "<br>";</p> <p>Echo "java vendor =". $ system-> getproperty ("java.vendor"). "</ p>";</p> <p>?></p> <p>If you have Java knowledge, it will help development work, this integrated ability is the relationship between PHP acceptance and growth in the future.</p> <p>Ten, PHP and XML</p> <p>The PHP contains an optional XML extension that supports EXPAT resolution, using a function related to XML in PHP, can create an analyst to handle valid XML documents. If you are using the 1.3.7 or higher version of Apache software, you don't need to install additional library files, you need to do just configure the with-xml in PHP. Like Java and COM, the support of XML in PHP is also very interesting, and the growth is also very fast. If you understand Expat or libXML, please join this aspect.</p></div><div class="text-center mt-3 text-grey"> 转载请注明原文地址:https://www.9cbs.com/read-13711.html</div><div class="plugin d-flex justify-content-center mt-3"></div><hr><div class="row"><div class="col-lg-12 text-muted mt-2"><i class="icon-tags mr-2"></i><span class="badge border border-secondary mr-2"><h2 class="h6 mb-0 small"><a class="text-secondary" href="tag-2.html">9cbs</a></h2></span></div></div></div></div><div class="card card-postlist border-white shadow"><div class="card-body"><div class="card-title"><div class="d-flex justify-content-between"><div><b>New Post</b>(<span class="posts">0</span>) </div><div></div></div></div><ul class="postlist list-unstyled"> </ul></div></div><div class="d-none threadlist"><input type="checkbox" name="modtid" value="13711" checked /></div></div></div></div></div><footer class="text-muted small bg-dark py-4 mt-3" id="footer"><div class="container"><div class="row"><div class="col">CopyRight © 2020 All Rights Reserved </div><div class="col text-right">Processed: <b>0.034</b>, SQL: <b>9</b></div></div></div></footer><script src="./lang/en-us/lang.js?2.2.0"></script><script src="view/js/jquery.min.js?2.2.0"></script><script src="view/js/popper.min.js?2.2.0"></script><script src="view/js/bootstrap.min.js?2.2.0"></script><script src="view/js/xiuno.js?2.2.0"></script><script src="view/js/bootstrap-plugin.js?2.2.0"></script><script src="view/js/async.min.js?2.2.0"></script><script src="view/js/form.js?2.2.0"></script><script> var debug = DEBUG = 0; var url_rewrite_on = 1; var url_path = './'; var forumarr = {"1":"Tech"}; var fid = 1; var uid = 0; var gid = 0; xn.options.water_image_url = 'view/img/water-small.png'; </script><script src="view/js/wellcms.js?2.2.0"></script><a class="scroll-to-top rounded" href="javascript:void(0);"><i class="icon-angle-up"></i></a><a class="scroll-to-bottom rounded" href="javascript:void(0);" style="display: inline;"><i class="icon-angle-down"></i></a></body></html><script> var forum_url = 'list-1.html'; var safe_token = 'RbObJrs_2FTj_2BBEJ4lxjM9BepteIukMWhQ7kJe7GO5h_2FFiLI8N_2BnYOrtYE7Hr10IQbGJ7QKrvalm7mtzdIDz9tNQ_3D_3D'; var body = $('body'); body.on('submit', '#form', function() { var jthis = $(this); var jsubmit = jthis.find('#submit'); jthis.reset(); jsubmit.button('loading'); var postdata = jthis.serializeObject(); $.xpost(jthis.attr('action'), postdata, function(code, message) { if(code == 0) { location.reload(); } else { $.alert(message); jsubmit.button('reset'); } }); return false; }); function resize_image() { var jmessagelist = $('div.message'); var first_width = jmessagelist.width(); jmessagelist.each(function() { var jdiv = $(this); var maxwidth = jdiv.attr('isfirst') ? first_width : jdiv.width(); var jmessage_width = Math.min(jdiv.width(), maxwidth); jdiv.find('img, embed, iframe, video').each(function() { var jimg = $(this); var img_width = this.org_width; var img_height = this.org_height; if(!img_width) { var img_width = jimg.attr('width'); var img_height = jimg.attr('height'); this.org_width = img_width; this.org_height = img_height; } if(img_width > jmessage_width) { if(this.tagName == 'IMG') { jimg.width(jmessage_width); jimg.css('height', 'auto'); jimg.css('cursor', 'pointer'); jimg.on('click', function() { }); } else { jimg.width(jmessage_width); var height = (img_height / img_width) * jimg.width(); jimg.height(height); } } }); }); } function resize_table() { $('div.message').each(function() { var jdiv = $(this); jdiv.find('table').addClass('table').wrap('<div class="table-responsive"></div>'); }); } $(function() { resize_image(); resize_table(); $(window).on('resize', resize_image); }); var jmessage = $('#message'); jmessage.on('focus', function() {if(jmessage.t) { clearTimeout(jmessage.t); jmessage.t = null; } jmessage.css('height', '6rem'); }); jmessage.on('blur', function() {jmessage.t = setTimeout(function() { jmessage.css('height', '2.5rem');}, 1000); }); $('#nav li[data-active="fid-1"]').addClass('active'); </script>