10 Advanced tips for PHP _php

Source: Internet
Author: User
Keywords tips advanced one PHP dialogs variables Apache installation
PHP is used by administrators of more than 3 million Internet sites worldwide, making it one of the most popular server-side scripting languages. It is characterized by fast running, stable and reliable, cross-platform, and open source software. Depending on the level you use, PHP can be very simple or complex, you can use it to send HTML table elements only, and you can integrate Java and XML into your PHP application.

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

first, the installation of PHP as Apache DSO
PHP is often used with Apache on the Linux/unix platform, with three installation options available when installing PHP: static mode, dynamic mode (DSO), CGI binary mode.

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

The following steps will install a completely new Apache and install it in the DSO mode:

1, from the Apache Software Foundation to get the latest version of the Apache source code;

2. Put the obtained source code into the/usr/local/or/opt/directory, or any directory you specify;

3. Run Gunzip to unzip the file and get the file suffix. tar;

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

TAR-XVF Apache_[version].tar

5. Enter the/usr/local/apache_[version] directory (or the directory where the compressed files are installed in step 4);

6. Type the following command to prepare for compiling Apache, replace the [path] with your own path, for example,/usr/local/apache[version], and now set the new value of Mod_so, which will allow Apache to use the DSO module;

7. After returning to the prompt state, type make and wait to return to the prompt again;

8. Execute the "make install" command.

Now that Apache is installed, the system will return to the prompt state. Next we start installing PHP:

1. Find the latest version of the link 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 specify;

3. Run Gunzip to unzip the file and get the file 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, the preparation for installing PHP in DSO is ready, and the only configuration option that needs to be modified is WITH-APXS (this is a file in Apache's Bin directory). In order to get a higher performance, I did not install the support module for MySQL.

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

6, return to the prompt state after the make command, waiting to return to the prompt state;

7. Execute the Make install command.

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

1. Find a line containing serveradmin and add your email 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 following subsections:

# 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 no longer becomes a comment, and add the file suffix name that you want to use in PHP, and 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

To save the file, go back to the top level directory and execute the following command to restart Apache:

./bin/apachectl Start

If you do not see an error message at startup, you can test the installed Apache and PHP by creating a file named Phpinfo.php that contains only one line, as shown below:

  

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

If you want to reconfigure PHP, you need to run the make Clean command again, then execute the./configure command with a series of options, and then the make and make install commands, a new module appears in the Apache directory module. Just restart Apache to load this new module and everything is OK.

second, the use of PHP itself dialogue
The most anticipated feature in PHP 4.0 should be support for conversations, and PHP 3.0 users must use third-party software or they won't be able to use conversations, and not support conversations has always been one of PHP's biggest flaws.

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

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

Session_Start ();

Starting a dialog will send a recognition string (for example, 940F8B05A40D5119C030C9C7745AEAD9) to the user via a cookie, and on the server side, a temporary file that matches the recognition string, such as Sess_, will be created. 940F8B05A40D5119C030C9C7745AEAD9, this file contains the registered dialog variables and their values.

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

  
If a user does not already exist for a user, a dialog is started:

Session_Start ();

Then register a variable with the name count:

Session_register (' count ');

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

$count + +;

Combine the above, and if you haven't started a conversation, you'll start a conversation; If there is no dialog ID, specify a good for the user, register a variable named $count, and perform a plus 1 operation on $count to indicate that the user has visited the page for the first time.

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

echo "

You ' ve been here $count times.

";

The full access counter code 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 value of the variable count is increased by 1, which is cool.

You can also register an array variable in the dialog, assuming we have registered a variable named $faves:

$faves = Array (' chocolate ', ' coffee ', ' beer ', ' Linux ');

You can register an array variable as you would register a simple variable:

Session_register (' faves ');

Reference array variables and reference simple variables there is nothing two, if a user on the page point out in the life of the hobby, you can register his hobby in a known as the $faves array variable, and then in another page can easily show these hobbies:

  
Session_Start ();

echo "My user likes:

  
      ";

      while (list (, $v) = each ($faves)) {

      echo "
    • $v "; }

      echo "
";

?>

Then you get a list of users ' hobbies.

The dialog variable cannot be overwritten by the query string, which means we cannot enter http:///www.yourdomain.com/yourscript.php?count=56 to assign a new value to the registered 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 delete a dialog variable, you first need to unregister it from the system:

Session_unregister (' count ');

The script to completely delete 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 the user, which is the simplest method.
  • 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.