Advanced PHP programming skills in Linux (1)

Source: Internet
Author: User
Tags uncompress
PHP advanced programming skills in Linux (1)-Linux general technology-Linux programming and kernel information. The following is a detailed description. Administrators of more than 3 million Internet websites around the world are using PHP, making it one of the most popular server-side scripting languages. It features fast running, stable and reliable, cross-platform, and open source software. Different from the level you use, PHP can be very simple or complex. You can only use it to send HTML table elements, and integrate Java and XML in PHP applications.
  
If you have a certain understanding of PHP or read some preliminary teaching materials, these skills can expand your understanding of PHP, so that you can master some common and advanced PHP functions.
  
This article mainly introduces the following Top Ten PHP Advanced Skills: 1. install PHP as a DSO of Apache; 2. Use PHP conversation; 3. Files are our friends; 4. Maintenance of PHP and file systems; 5. Rich Array functions; 6. Creation of dynamic images; 7. PHP-based user authentication; 8. PHP and COM; 9. PHP and Java; 10. PHP and XML
  
1. install PHP as a DSO of Apache
  
PHP is often used with Apache on Linux/Unix platforms. When installing PHP, there are three installation methods available: static mode, dynamic mode (DSO), and CGI binary mode.
  
Because it is easy to maintain and upgrade, I strongly recommend using DSO to install PHP. For example, if the PHP installed for the first time only supports the database and then you want to install a module that supports encryption, you only need to run "make clean" and add new configuration options, then run "make" and "make install". A new PHP module will be installed in an appropriate location in Apache and then restart Apache without re-compiling Apache.
  
The following steps will install a brand new Apache and install PHP in DSO mode:
  
1. obtain the latest Apache source code from the Apache Software Foundation;
  
2. Put the obtained source code in the/usr/local/or/opt/directory, or any directory you specified;
  
3rd. Run gunzip' To uncompress the file and get the file suffixed with .tar;
  
4. Run the following command to install the file in the apache _ [version] directory:
Tar-xvf apache_{version}.tar
  
5. Go to the/usr/local/apache _ [version] Directory (or install the compressed file directory in step 4 );
  
6. Enter the following command to prepare for compiling Apache. Replace [path] With your own path, for example,/usr/local/apache [version]. now the new value of mod_so has been set, which will allow Apache to use the DSO module;
  
7. Return to the prompt state, type make, and wait for the prompt to return again;
  
8. Run the "make install" command.
  
Now that Apache has been installed, the system will return to the prompt state. Next we will start to install PHP:
  
1. Find the link to the latest version in the Download Area of the PHP homepage;
  
2. download the file to an appropriate directory, such as/usr/local/or/opt/or any directory you specified;
  
3rd. Run gunzip' To uncompress the file and get the file suffixed with .tar;
  
4. Run 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;
  
Now, you have prepared for installing PHP using DSO. The only configuration option to be modified is with-apxs (a file in the Apache bin directory ). To achieve high performance, I have not installed a support module for MySQL.
  
./Configure -- with-mysql =/[path to mysql] -- with-apxs =/[path to apxs]
  
6. Return to the prompt status and run the make command. Wait until the prompt status is returned again;
  
7. Run the make install command.
  
At this point, the system installed PHP In the Module Directory of Apache in DSO mode, and modified the httpd. conf file of Apache, and then returned to the prompt state. After you return to the prompt state, you also need to modify the httpd. conf file of Apache.
  
1. Find a row containing ServerAdmin and add your email address, as shown below:
ServerAdmin you@yourdomain.com
  
2. Find the row starting with ServerName and change it to a real value, for example:
ServerName localhost
  
3. Find the following section:
# And for PHP 4.x, use:
#
# AddType application/x-httpd-php. php
# AddType application/x-httpd-php-source. phps
  
Modify the content of these rows so that the AddType of PHP 4.0 is no longer annotated, and add the extension name of the file to be used in PHP. The content above is changed to the following content:
# 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 upper-level directory, and run the following command to restart Apache:
./Bin/apachectl start
  
If no error message is displayed at startup, you can create a file named phpinfo. php with only one line of content shown below to test installed Apache and PHP:
  
  
Save the file to the Apache document root directory (htdocs), open the browser, and type http: // localhost/phpinfo. php address, many variables and their values appear on the screen.
  
To reconfigure PHP, run the make clean command again, and then execute the command with a series of options. /configure command, and then execute the make and make install commands. A new module will appear in the directory module of Apache. You only need to restart Apache to load the new module, everything is OK.
  
2. Use PHP conversation
  
The most exciting feature of PHP 4.0 should be its support for dialogs. Users of PHP 3.0 must use third-party software, or they will not be able to use dialogs, which has always been one of the biggest shortcomings of PHP.
  
As long as you are browsing your website, you can maintain variables related to specific users using dialogs without creating multiple cookies, hiding table fields, or storing information in the database.
  
Starting a conversation on a web page will make the PHP engine know that you want to start a conversation (if it has not been started) or continue the current conversation:
Session_start ();
  
When a dialog is started, a cookie is used to send an identification string (for example, 940f8b05a40dda-9c030c9c7745aead9) to the user. on the server side, a temporary file that matches the recognition string is created, for example, Token, this file contains the registered dialog variables and their values.
  
The most common example for displaying the role of a dialog is the access counter. Start the PHP module and ensure that the PHP code is the first line of the file. do not include spaces, HTML code, or other code before the PHP code. Because the dialog will send a header, if there is space and HTML code before session_start (), an error message will be obtained.
   // If no user exists for a user, start a dialog:
Session_start ();
  
Then register a variable named count:
Session_register ('Count ');
  
After registering a dialog variable, the variable named count exists as long as the dialog exists. Now, the count variable has not been assigned a value. If you add 1 to it, its value is changed to 1.
  
$ Count ++;
  
Combine the preceding content. If no dialog is started, a dialog is started. If no dialog id exists, a dialog is specified for the user, register a variable named $ count. Adding 1 to $ count indicates that the user has accessed the webpage for the first time.
  
To know the number of times a user accesses this page in the current dialog, just display the value of the $ count variable:
Echo"

You 've been here $ count times.

";
  
The code for all access Counters is as follows:
  
   Session_start ();
Session_register ('Count ');
$ Count ++;
Echo"

You 've been here $ count times.

";
?>
  
If you reload the above script file, you will find that the variable count value has increased by 1, so cool.
  
You can also register an array variable in the dialog. Suppose we have registered a variable named $ faves:
$ Faves = array ('chocolate', 'coffee ', 'beer', 'linux ');
  
You can register an array variable like registering a simple variable:
Session_register ('fafes ');
  
There is no difference between referencing an array variable and referencing a simple variable. If a user points out that he or she is happy on the webpage, you can register his hobbies into an array variable called $ faves, and then easily display these hobbies on another webpage:
   Session_start ();
Echo "My user likes:
  
    ";
    While (list (, $ v) = each ($ faves )){
    Echo"
  • $ V ";}
    Echo"
";
?>
  
Then you get a list of user hobbies.
  
The dialog variable cannot be overwritten by the query string, that is, we cannot enter http: // www.yourdomain.com/yourscript.php? Count = 56 specify a new value for the registration variable $ count, which is important to security: Only one unregistered dialog variable can be deleted from the script on the server.
  
To completely delete a dialog variable, you must first cancel it 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, make the code clearer, and reduce the number of cookies sent to users. This is the simplest method.
  
3. Files are our friends
  
Regardless of the size of the website you are developing, you should be aware of the importance of code reuse, whether the code is HTML or PHP code. For example, you must change the footer containing the copyright information at least once a year. If your website contains 1000 pages, modifying the footer once a year is also annoying.
  
In PHP, there are at least a few functions that can help you reuse code. The functions used depend on the code you reuse. The main functions are:
* Include () and include_once ()
* Require () and require_once ()
  
The include () function contains and computes the given file.

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.