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, PHP can also be used by administrators of more than 3 million internet websites around the world in PH, 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.
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 promising feature of PHP 4.0 should be the support for dialogs. users of PHP 3.0 must use third-party software. Otherwise, they cannot use dialogs. not supporting dialogs 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. for example:
Include ('/home/me/myfile ');
Any code in the include file is executed within the scope of the code that appears in include (). you can use include () and fopen () together to include static files on your own server, include the target file on another server.
Include_once () has the same function as include (). The difference between them is that it checks whether the code in a file is included in an existing script. if the code already exists, it will not be included again.
The require () function replaces itself with the content of a given file, which occurs during PHP engine code compilation rather than during execution, unlike include () the calculation is performed first. The require () function is more used in static elements, while the include () function is more used in dynamic elements. Similar to include_once (), require_once () first checks whether the given code has been inserted. if the code already exists, it is no longer inserted.
To learn more about the content, I prefer to use the require function in copyright information, static text, and other elements that do not contain variables, or those that depend on other scripts being executed. For example:
Something
[A lot of content]
// Insert copyright
Require ('/home/me/mycopyright ');
?>
On the other hand, I often use include () to control many functions at the beginning of a file:
// Obtain the function library
Include ('/home/me/myfunctions ');
// Do PHP things with my functions?>
Something
[A lot of content]
The next question should be "where are the include and require files ?", A simple answer to this question is, "anywhere in the system ." If your code contains a database connection with a user name and password, you will not put them in the root directory of the document to be open to everyone.
Stored Ded or required files can be stored anywhere on the system, as long as users on the system where PHP runs can access these files, you can make these files have any suffix, or do not use a suffix.
Using include () and require () to externalize the elements on the website is a common phenomenon, and it brings great convenience to you when you need to upgrade the website.
IV. PHP and file system maintenance
PHP has many functions related to the file system. these functions can not only open files, but also display contents in directories, move files, and other functions, many people even use PHP to develop Internet-based file resource managers.
File path explanation: in Windows, you can use the/and symbol in the path, but in other operating systems, you can only use the/symbol. For the sake of consistency, we use the/symbol in a unified manner.
The following script example shows a directory list with annotations included in the code:
$ Dir_name = "/home/me /";
/* Create a handle whose value is the result of opening a given directory */
$ Dir = opendir ($ dir_name );
/* Create a text block to place the list element (file name )*/
$ File_list ="
";
/* Use a while statement to read all the elements in the opened Directory. if the file name is not ".." or "..", the name in the list is displayed */
While ($ file_name = readdir ($ dir )){
If ($ file_name! = ".") & ($ File_name! = "..")){
$ File_list. ="
- $ File_name ";
}
}
$ File_list. ="
";
/* Close The OpenEd directory and end the PHP module */
Closedir ($ dir );
?>
Directory Listing
Files in:
Now we have a directory list. Note that to read a file (which will be explained later) or directory, users on the PHP running system must have at least the permission to read the file.
The following is an example of how to copy an object: