Two classic techniques of PHP application

Source: Internet
Author: User
Tags contains execution file system functions include variable variable scope root directory
Tips (Coolman)

PHP is one of the most popular server-side script languages because of its fast, reliable, cross-platform application and open source code. I based on my experience in the work, to introduce you to the use of PHP experience, I hope to help you.

use PHP's include files to maintain your site
No matter how large or small your Web site is, you should realize the importance of reusing code, whether it's a PHP program or HTML source code that you use repeatedly. For example, the copyright announcement at the end of the page is revised at least once a year, what if your site has many pages? It must be a headache to do a change to one of these pages. Through PHP we can reuse code in several different ways. Which functions you want to use depends on what you want to reuse.

These main functions include:

* Include () and include_once ()

* Require () and require_once ()

The 1.include () function reads the specified file into and executes the program inside.
For example: include ('/home/me/myfile ');

The program code in the file being imported is executed, and these programs have the same variable scope (variable scope) as the location of the call to the include () function in the source file at the time of execution. You can import static files from the same server, and you can even use the include () and fopen () functions to import files from other servers.

The functions of the 2.include_once () function and include () are almost identical
The only difference is that the include_once () function first checks whether the file being imported has been imported elsewhere in the program, if you do, you will not be able to import the file again (this feature is sometimes important, for example, to import a file that declares some of your own well-defined functions. , if you import the file repeatedly in the same program, an error message will occur the second time you import it, because PHP does not allow functions of the same name to be repeated for the second time.

The 3.require () function reads the contents of the target file and replaces itself with the read content.
This read-and-replace action occurs when the PHP engine compiles your program code. Instead of what happens when the PHP engine starts executing the compiled program code (the PHP 3.0 engine works by compiling one line of execution, but it changes in PHP 4.0, PHP 4.0 is the first to put the whole Once the program code is compiled, the compiled program code is executed one at a time, and no program code is executed during the compilation. Require () is typically used to import static content, and include () is suitable for importing dynamic program code.

4. As with the include_once () function, the require_once () function will first check that the contents of the target file have been imported before, and if so, the same content will not be imported again.
I am personally accustomed to using the Require () function to import copyright announcements (copyrights), static text or other code that does not contain variables, or that itself relies on other programs that have been executed in order to execute correctly. For example:

<HTML>
<HEAD> <TITLE> page Title </TITLE> </HEAD>
<BODY>
[A pile of content]
?
Import Copyright Announcement Text
Require ('/home/me/mycopyright ');
? >
</BODY> </HTML>
On the other hand, I usually use the include () function at the beginning of the program to import some functions libraries or similar program code:

?
Import my library of functions
Include ('/home/me/myfunctions ');
Perform some functions using the PHP functions defined in the previously imported function library?
<HTML>
<HEAD> <title> page Title </TITLE> </HEAD>
<BODY>
[A pile of content]
</BODY>
</HTML>
Next you might ask the first logical question: "Where are the files to be imported?" "The short answer is:" Anywhere in the server file system. "However, it should be noted that if the file being imported contains some sensitive information in addition to the simple snippets of the code, such as the account number and password used to connect to the database system, it is recommended that you not put these files under the Web server's file root directory, Because that would make it easy for others to steal the information.

You can put these included files in any directory on the system, only if the PHP itself is used to execute the identity (Www,nobody or other identity) must have sufficient permissions to read these files can be. The extensions of these files can also be arbitrarily taken, even without the file name attached.

Use include () and require () to make a reasonable division of the shared content that is often required to change in the site, and it will be much easier to update the content of the site.

use PHP to maintain the file system
PHP provides a number of functions related to the file system, so that we can not only open the file, but also display the contents of the directory, move the location of the file and other functions. Some friends even write a PHP program that can manage the contents of a file through a browser.

Before we begin to introduce PHP's file-system-related features, we need to sort out one thing: in the Windows operating system, file paths can be represented by slashes (/) or backslashes (\), but in other operating systems we use only diagonal lines. In order to maintain uniformity, the file path in the example below is to use a slash.

The following example program I will teach you the basic contents of the content display function, each step is annotated, please read directly.

? /* $dir _name The value of this variable is the full path of the directory you want to read * * *
$dir _name = "/home/me/";
The/* OPENDIR () function opens a directory and returns a reference value (handle) that we can use to refer to the directory in the program.
$dir = Opendir ($dir _name);
/* Starts creating a string that contains the HTML list volume to display the file name in the directory. */
$file _list = "<ul>";
/* Use a while loop narration to read all the files in the previously opened directory. If the file name that you read is not "." Or "..", write the file name into the string mentioned above. */
while ($file _name = Readdir ($dir)) {
if ($file _name!= ".") && ($file _name!= "...")) {
$file _list. = "<li> $file _name";
}
}
/* Add end to HTML list label
$file _list. = "</ul>";
/* Close the previously opened directory and end this PHP program * *
Closedir ($dir);
? >
!--HTML raw code starts here-->
<HTML>
<HEAD>

</HEAD>
<BODY>
!--use a PHP program to display the directory names we read on the page-->
<p> Files in:? echo "$dir _name";? > </p>
!--use a PHP program to display the file name read in this directory on the page-->
? echo "$file _list";
</BODY>
</HTML>
After a few steps, you have successfully displayed the name of the file in a directory on the page. But you have to remember that to read a directory or file (read the contents of the file will be introduced later), PHP itself to perform the identity must have at least the directory or file read permission to do, otherwise the system will display insufficient permissions error messages.

Next example I will teach you how to copy a file:

? /* Variable $orginal stores the full path of the source file, and the variable $copied stores the full path of the new file that copied the past * *
$original = "/home/me/mydatabasedump";
$copied = "/archive/mydatabasedumo_1010";
/* Call the Copy () function to copy the file from the original location to the new location. If it cannot be replicated, the execution of the program is terminated and an error message is displayed. */
@copy ($original, $copied) or Die ("file cannot be copied. ");
? >
The above example program can be used to expand into a file backup system program. When the program executes, it copies the data files of the database to a different directory for backup purposes. As long as you modify the system's scheduling file content (crontab), we can let this program automatically at a fixed time every day, to achieve the system automatic backup, do not need manual manual execution.

If your system has installed Lynx software (Lynx is a text-only Web browser), you can add the following record in the system scheduling file to allow the system to automatically activate the Lynx at a fixed time and call our previously written PHP backup program. When Lynx calls (browses) our PHP program, the program is executed and a backup file is generated. The following example teaches you how to perform our backup program at five o'clock in the morning every day and automatically closes the Lynx program after execution:

0 5 * * * [username] lynx-dump http://localhost/copyfile.php 1>/dev/null 2>&1
If your system is installed with a CGI version of PHP, you can directly call the PHP execution file without having to call our PHP program via Lynx:

0 5 * * * [username] php/path/to/copyfile.php 1>/dev/null 2>&1

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.