10 Advanced Tips for PHP (top)

Source: Internet
Author: User
Tags array install php mysql variables php code version variable access
Advanced | Tips the administrators of more than 3 million Internet sites worldwide are using PHP, making it one of the most popular server-side scripting languages. It is characterized by fast operation, stable and reliable, cross-platform, and open source software. Depending on the level you use, PHP can be simple or complex, and you can use it to send HTML form elements and integrate Java and XML into your PHP application.

If you have a certain understanding of PHP or read some preliminary teaching materials, these techniques can expand your knowledge of PHP, so that you have some common and advanced PHP features.

First, the PHP installed as Apache DSO
PHP on the Linux/unix platform often with the use of Apache, in the installation of PHP, there are three ways to install the choice: static mode, dynamic mode (DSO), CGI binary mode.

Because it is easy to maintain and upgrade, I strongly recommend the DSO to install PHP. For example, if the installation of PHP in the initial installation supports only the database, then you want to install the module that supports encryption, just run "make clean", add a new configuration option, and then run "make" and "Make Install", A new PHP module will be installed in the appropriate location in Apache and then restart Apache without recompiling Apache.

The following steps will install a new Apache and install PHP as a DSO:

1, from the Apache Software Foundation to get the latest version of the Apache source code;
2, put the source code into the/usr/local/or/opt/directory, or you specify any directory;
3, the Operation Gunzip to the file for decompression, get suffix of. tar files;
4. Run the following command to install the file in the Apache_[version] directory:

TAR-XVF Apache_[version].tar

5, into the/usr/local/apache_[version] directory (or in step 4 to install the directory of compressed files);
6, type the following command for compiling Apache preparation, with your own path to replace the [path], for example,/usr/local/apache[version], now has set the new value of MOD_SO, it will allow Apache to use the DSO module;
7. When you return to the prompt state, type make and wait to return to the prompt again;
8, execute "make install" command.

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

1, the PHP homepage in the download area to find the latest version of the link;
2. Download the file to an appropriate directory, such as/usr/local/or/opt/or any directory you specify;
3, the Operation Gunzip to the file for decompression, get suffix of. tar files;
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, has been done to the DSO way to install PHP preparation work, the only need to modify the configuration option is With-apxs (this is the Apache Bin directory of a file). To achieve higher performance, I did not install a support module for MySQL.

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

6, go back to the prompt state to execute make command, waiting to return to the prompt state;
7, execute make install command.

At this point, the system to DSO way in the Apache module directory installed PHP, and the Apache httpd.conf file to make appropriate changes to return to the prompt state. When you return to the prompt, you will also need to make some changes to the Apache httpd.conf file.

1, find a line containing serveradmin, add your e-mail address, as follows:

ServerAdmin you@yourdomain.com

2, find the line beginning with servername, and change it to a real value, for example:

ServerName localhost

3, find the content below the section:

# and for PHP 4.x, use:
#
#AddType application/x-httpd-php. php
#AddType Application/x-httpd-php-source. Phps

Modify the contents of these lines so that PHP 4.0 's AddType is no longer a comment, and add the file suffix name that you want to use in PHP, which changes to the content shown below:

# and for PHP 4.x, use:
#
AddType application/x-httpd-php. php. phtml
AddType Application/x-httpd-php-source. Phps

Save the file, go back to the previous directory, and restart Apache by executing the following command:

./bin/apachectl Start

If you do not receive an error message at startup, you can test the installed Apache, PHP by creating a file named phpinfo.php that has only one row below:

? Phpinfo ()?>

Save this file to the Apache document root (Htdocs), and then open the browser and type the http://localhost/phpinfo.php address, where a number of variables and their values appear on the screen.

If you want to reconfigure PHP, you need to run the make Clean command again, and then execute the./configure command with a set of options, and then execute the make and make install commands, and a new module appears in the Apache catalog module, Just restart Apache to load this new module and it's all OK.

Second, the Use of PHP itself dialog

The most anticipated feature in PHP 4.0 should be support for dialogs, and PHP 3.0 users must use third party software otherwise they cannot use dialogs, and not supporting dialogs is one of the biggest flaws in PHP.

As long as users are browsing your site, you can use dialogs to maintain variables related to specific users without having to create multiple cookies, use hidden table fields, or store information in a database.

Starting a conversation on a Web page will let the PHP engine know that you want to start a conversation (if it hasn't started) or continue with the current conversation:

Session_Start ();

Starting a conversation will send the user an identifying string (such as 940F8B05A40D5119C030C9C7745AEAD9) through a cookie, and on the server side, a temporary file that matches the recognized string, such as Sess_ 940F8B05A40D5119C030C9C7745AEAD9, this file contains the registered dialog variables and their values.

The most common example used to show the role of a dialog is to access the counter. Start the PHP module to make sure that the PHP code is the first line of the file and do not have spaces, HTML code, and other code before the PHP code. Because the conversation sends a header, you get an error message if you have spaces and HTML code before session_start ().

?
If a user is not yet present, start a conversation:
Session_Start ();
Then register a variable with the name count:
Session_register (' count ');

After registering a dialog variable, a variable named count is present as long as the dialog is present. Now, the count variable has not been assigned, and if you add 1 to it, its value becomes 1.

$count + +;

Combine the above, and if you haven't started a conversation, you start a conversation; If there is no dialog ID, give the user a good one, register a variable with the name $count, and add 1 to $count to indicate that the user has visited the page for the first time.

To know how many times a user accesses this page in the current conversation, just display the value of the $count variable:

echo "<p>you ' ve been here $count times.</p>";

The full access counter code looks like this:

?
Session_Start ();
Session_register (' count ');
$count + +;
echo "<p>you ' ve been here $count times.</p>";
?>

If you reload the script file above, you'll find that the value of the variable count has increased by 1, which is cool.
You can also register an array variable in a dialog, assuming we have a variable named $faves:

$faves = Array (' chocolate ', ' coffee ', ' beer ', ' Linux ');
You can register an array variable as if you were registering a simple variable:
Session_register (' faves ');

Referencing an array variable with a reference to a simple variable no, if a user points out a hobby in life, he can register his hobby with an array variable called $faves, and then easily display these hobbies on another page:

?
Session_Start ();
echo "My user likes:
<ul> ";
while (the list (, $v) = each ($faves)) {
echo "<li> $v"; }
echo "</ul>";
?>

Then you get a list of user interests.

The dialog variable cannot be overwritten by the query string, which means we cannot enter http:///www.yourdomain.com/yourscript.php?count=56 to specify a new value for the register variable $count. This is important for security: You can only delete a dialog variable that is not registered in the server-side script.

If you want to completely remove a dialog variable, you first need to unregister it from the system:

Session_unregister (' count ');

The script to completely remove a dialog variable is very simple, as follows:

Session_destroy ();

Using dialog variables can reduce the frequency of access to the database, make the code clearer, and reduce the number of cookies sent to users, which is the easiest way.

Related Article

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.