国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

Home php教程 php手冊(cè) PHP的十個(gè)高級(jí)技巧(上)

PHP的十個(gè)高級(jí)技巧(上)

Jun 21, 2016 am 09:13 AM
apache count make php session

高級(jí)|技巧

全球超過300萬個(gè)互聯(lián)網(wǎng)網(wǎng)站的管理員都在使用PHP,使得它成為最為普及的服務(wù)器端腳本語(yǔ)言之一。其特點(diǎn)是運(yùn)行速度快、穩(wěn)定可靠、跨平臺(tái),而且是開放源代碼軟件。隨你使用的水平不同,PHP可以很簡(jiǎn)單,也可以很復(fù)雜,可以只使用它發(fā)送HTML表格元素,還可以在PHP應(yīng)用程序中集成Java和XML。

  如果你對(duì)PHP有一定的了解或者看過一些初步的教材,這些技巧可以擴(kuò)展你對(duì)PHP的認(rèn)識(shí),使你掌握一些常見的和高級(jí)的PHP功能。

一、把PHP安裝為Apache的DSO
  PHP在Linux/Unix平臺(tái)上經(jīng)常與Apache搭配使用,在安裝PHP時(shí),有三種安裝方式可供選擇:靜態(tài)模式、動(dòng)態(tài)模式(DSO)、CGI二進(jìn)制模式。

  由于易于維護(hù)和升級(jí),我強(qiáng)烈建議以DSO方式安裝PHP。例如,初次安裝時(shí)如果安裝的PHP僅支持?jǐn)?shù)據(jù)庫(kù),隨后希望再安裝支持加密的模塊,只要運(yùn)行“make clean”,添加新的配置選項(xiàng),然后再運(yùn)行“make”和“make install”即可,一個(gè)新的PHP模塊就會(huì)安裝在Apache中適當(dāng)?shù)奈恢蒙希缓笾匦聠?dòng)Apache,而無需重新編譯Apache。

  下面的步驟將安裝一個(gè)全新的Apache,并以DSO方式安裝PHP:

  1、從Apache軟件基金會(huì)得到最新版本的Apache源代碼;
  2、把得到的源代碼放到/usr/local/或者/opt/目錄下,或者你指定的任意目錄中;
  3、運(yùn)行Gunzip對(duì)文件進(jìn)行解壓縮,得到后綴為.tar的文件;
  4、運(yùn)行下面的命令,把文件安裝到apache_[version]目錄中:

   tar -xvf apache_[version].tar

  5、進(jìn)入/usr/local/apache_[version]目錄(或者在步驟4中安裝壓縮文件的目錄);
  6、鍵入下面的命令為編譯Apache作準(zhǔn)備,用你自己的路徑替換其中的[path],例如,/usr/local/apache[version],現(xiàn)在已經(jīng)設(shè)置了mod_so的新值,它將允許Apache使用DSO模塊;
  7、回到提示符狀態(tài)后鍵入make,并等待再次回到提示符狀態(tài);
  8、執(zhí)行“make install”命令。

  至此,Apache已經(jīng)安裝完畢,系統(tǒng)將重回到提示符狀態(tài)。接下來我們開始安裝PHP:

  1、在PHP主頁(yè)的下載區(qū)中找到最新版本的鏈接;
  2、把文件下載到一個(gè)適當(dāng)?shù)哪夸浿?,例?usr/local/或/opt/或者你指定的任意目錄中;
  3、運(yùn)行Gunzip對(duì)文件進(jìn)行解壓縮,得到后綴為.tar的文件;
 4、執(zhí)行下面的命令把文件安裝在php-[version]目錄中:

   tar -xvf php-[version]

  5、進(jìn)入/usr/local/php-[version]目錄或在步驟4中指定的目錄;

  至此,已經(jīng)作好了以DSO方式安裝PHP的準(zhǔn)備工作,唯一需要修改的配置選項(xiàng)是with-apxs(這是Apache的bin目錄中的一個(gè)文件)。為了得到較高的性能,我沒有安裝對(duì)MySQL的支持模塊。

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

  6、回到提示符狀態(tài)后執(zhí)行make命令,等待重新返回到提示符狀態(tài);
  7、執(zhí)行make install命令。

  至此,系統(tǒng)以DSO方式在Apache的模塊目錄中安裝了PHP,并對(duì)Apache的httpd.conf文件作適當(dāng)?shù)男薷暮蠓祷氐教崾痉麪顟B(tài)?;氐教崾痉麪顟B(tài)后,你還需要對(duì)Apache的httpd.conf文件作一些修改。

  1、找到包含有ServerAdmin的一行,添加你的電子郵件地址,如下所示:

   ServerAdmin you@yourdomain.com

  2、找到以ServerName開頭的行,把它改為真正的值,例如:

   ServerName localhost

  3、找到內(nèi)容如下所示的小節(jié):

  # And for PHP 4.x, use:
  #
  #AddType application/x-httpd-php .php
  #AddType application/x-httpd-php-source .phps

  修改這些行的內(nèi)容,使PHP 4.0的AddType不再成為注釋,并添加希望在PHP中使用的文件后綴名,上面的內(nèi)容變?yōu)槿缦滤镜膬?nèi)容:

  # And for PHP 4.x, use:
  #
  AddType application/x-httpd-php .php .phtml
  AddType application/x-httpd-php-source .phps

  保存文件,回到上一級(jí)目錄,執(zhí)行下面的命令重新啟動(dòng)Apache:

  ./bin/apachectl start

  如果在啟動(dòng)時(shí)沒有出現(xiàn)錯(cuò)誤提示信息,就可以通過創(chuàng)建一個(gè)名為phpinfo.php的只有如下所示一行內(nèi)容的文件,對(duì)安裝的Apache、PHP進(jìn)行測(cè)試:

   phpinfo() ?>

  把這個(gè)文件保存到Apache的文檔根目錄(htdocs)中,然后開啟瀏覽器,鍵入http://localhost/phpinfo.php地址,屏幕上就會(huì)出現(xiàn)許多的變量和它們的值。

  如果要重新配置PHP,需要再次運(yùn)行make clean命令,然后執(zhí)行帶有一系列選項(xiàng)的./configure命令,然后再執(zhí)行make和make install命令,Apache的目錄模塊中就會(huì)出現(xiàn)一個(gè)新的模塊,只要再重新啟動(dòng)Apache加載這個(gè)新的模塊,就一切OK了。

二、使用PHP本身的對(duì)話

  PHP 4.0中最令人期待的特性應(yīng)該是對(duì)對(duì)話的支持,PHP 3.0的用戶必須使用第三方的軟件否則就不能使用對(duì)話,不支持對(duì)話一直是PHP最大的缺憾之一。

  只要用戶在瀏覽你的網(wǎng)站,你就可以利用對(duì)話維護(hù)與特定用戶有關(guān)的變量,而無需建立多個(gè)cookie、使用隱藏表格字段或?qū)⑿畔⒋鎯?chǔ)在數(shù)據(jù)庫(kù)中。

  在一個(gè)網(wǎng)頁(yè)上啟動(dòng)一個(gè)對(duì)話,就會(huì)使PHP引擎知道你想啟動(dòng)一個(gè)對(duì)話(如果還沒有啟動(dòng))或者繼續(xù)當(dāng)前的對(duì)話:

session_start();

  啟動(dòng)一個(gè)對(duì)話將通過cookie向用戶發(fā)送一個(gè)識(shí)別字符串(例如940f8b05a40d5119c030c9c7745aead9),在服務(wù)器端,會(huì)創(chuàng)建一個(gè)與識(shí)別字符串匹配的臨時(shí)文件,例如sess_940f8b05a40d5119c030c9c7745aead9,這個(gè)文件中包含注冊(cè)的對(duì)話變量以及它們的值。

  用來顯示對(duì)話的作用的最常見的例子是訪問計(jì)數(shù)器。啟動(dòng)PHP模塊,確保PHP代碼是文件的第一行,在PHP代碼之前不要有空格、HTML代碼和其他的代碼。因?yàn)閷?duì)話會(huì)發(fā)送一個(gè)頭部,因此如果在session_start()之前有空格和HTML代碼,就會(huì)得到一個(gè)出錯(cuò)信息。

  
  // 如果還不存在一個(gè)針對(duì)某用戶的用戶,則啟動(dòng)一個(gè)對(duì)話:
  session_start();
  然后注冊(cè)一個(gè)名字為count的變量:
  session_register('count');

  注冊(cè)一個(gè)對(duì)話變量后,只要對(duì)話存在,名字為count的變量也就存在?,F(xiàn)在,count變量還沒有被賦值,如果對(duì)它執(zhí)行加1操作,它的值就變?yōu)榱?。

  $count++;

  把上述內(nèi)容綜合在一塊兒,如果還沒有啟動(dòng)一個(gè)對(duì)話,就會(huì)啟動(dòng)一個(gè)對(duì)話;如果不存在一個(gè)對(duì)話id,就為用戶指定一個(gè)好了,注冊(cè)一個(gè)名字為$count的變量,對(duì)$count執(zhí)行加1操作表示用戶已經(jīng)首次訪問了該網(wǎng)頁(yè)。

  要知道用戶在當(dāng)前的對(duì)話中訪問本頁(yè)面的次數(shù),只要顯示$count變量的值即可:

  echo "

You've been here $count times.

";

  全部的訪問計(jì)數(shù)器代碼如下所示:

  
  session_start();
  session_register('count');
  $count++;
  echo "

You've been here $count times.

";
  ?>

  如果重新加載上述的腳本文件,就會(huì)發(fā)現(xiàn)變量count的值增加了1,很酷吧。
  還可以在對(duì)話中注冊(cè)一個(gè)數(shù)組變量,假設(shè)我們注冊(cè)了一個(gè)名字為$faves的變量:

  $faves = array ('chocolate','coffee','beer','linux');
  可以象注冊(cè)一個(gè)簡(jiǎn)單變量那樣注冊(cè)一個(gè)數(shù)組變量:
  session_register('faves');

  引用數(shù)組變量與引用簡(jiǎn)單變量沒有什么二樣,如果一個(gè)用戶在網(wǎng)頁(yè)上指出在生活中的愛好時(shí),可以把他的愛好注冊(cè)到一個(gè)被稱作$faves的數(shù)組變量中,然后可以在另一個(gè)網(wǎng)頁(yè)中很方便地把這些愛好顯示出來:

  
  session_start();
  echo "My user likes:
  
    ";
      while (list(,$v) = each ($faves)) {
      echo "
  • $v"; }
      echo "
";
  ?>

  然后你就得到了一個(gè)關(guān)于用戶愛好的清單。

  對(duì)話變量不能被查詢字符串覆蓋,也就是說我們不能輸入http:///www.yourdomain.com/yourscript.php?count=56給注冊(cè)變量$count指定一個(gè)新值,這一點(diǎn)對(duì)于安全很重要:只能在服務(wù)器端的腳本中刪除一個(gè)沒有注冊(cè)的對(duì)話變量。

  如果要完全刪除一個(gè)對(duì)話變量,首先需要從系統(tǒng)中注銷它:

  session_unregister('count');

  要完全刪除一個(gè)對(duì)話變量的腳本是非常簡(jiǎn)單,如下所示:

  session_destroy();

  使用對(duì)話變量可以減少訪問數(shù)據(jù)庫(kù)的頻率,使代碼更加清晰,而且可以減少對(duì)用戶發(fā)送的cookie的數(shù)量,它是最簡(jiǎn)單的方法了。



Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undress AI Tool

Undress AI Tool

Undress images for free

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

PHP Tutorial
1502
276
How to use PHP to build social sharing functions PHP sharing interface integration practice How to use PHP to build social sharing functions PHP sharing interface integration practice Jul 25, 2025 pm 08:51 PM

The core method of building social sharing functions in PHP is to dynamically generate sharing links that meet the requirements of each platform. 1. First get the current page or specified URL and article information; 2. Use urlencode to encode the parameters; 3. Splice and generate sharing links according to the protocols of each platform; 4. Display links on the front end for users to click and share; 5. Dynamically generate OG tags on the page to optimize sharing content display; 6. Be sure to escape user input to prevent XSS attacks. This method does not require complex authentication, has low maintenance costs, and is suitable for most content sharing needs.

How to use PHP combined with AI to achieve text error correction PHP syntax detection and optimization How to use PHP combined with AI to achieve text error correction PHP syntax detection and optimization Jul 25, 2025 pm 08:57 PM

To realize text error correction and syntax optimization with AI, you need to follow the following steps: 1. Select a suitable AI model or API, such as Baidu, Tencent API or open source NLP library; 2. Call the API through PHP's curl or Guzzle and process the return results; 3. Display error correction information in the application and allow users to choose whether to adopt it; 4. Use php-l and PHP_CodeSniffer for syntax detection and code optimization; 5. Continuously collect feedback and update the model or rules to improve the effect. When choosing AIAPI, focus on evaluating accuracy, response speed, price and support for PHP. Code optimization should follow PSR specifications, use cache reasonably, avoid circular queries, review code regularly, and use X

Beyond the LAMP Stack: PHP's Role in Modern Enterprise Architecture Beyond the LAMP Stack: PHP's Role in Modern Enterprise Architecture Jul 27, 2025 am 04:31 AM

PHPisstillrelevantinmodernenterpriseenvironments.1.ModernPHP(7.xand8.x)offersperformancegains,stricttyping,JITcompilation,andmodernsyntax,makingitsuitableforlarge-scaleapplications.2.PHPintegrateseffectivelyinhybridarchitectures,servingasanAPIgateway

Object-Relational Mapping (ORM) Performance Tuning in PHP Object-Relational Mapping (ORM) Performance Tuning in PHP Jul 29, 2025 am 05:00 AM

Avoid N 1 query problems, reduce the number of database queries by loading associated data in advance; 2. Select only the required fields to avoid loading complete entities to save memory and bandwidth; 3. Use cache strategies reasonably, such as Doctrine's secondary cache or Redis cache high-frequency query results; 4. Optimize the entity life cycle and call clear() regularly to free up memory to prevent memory overflow; 5. Ensure that the database index exists and analyze the generated SQL statements to avoid inefficient queries; 6. Disable automatic change tracking in scenarios where changes are not required, and use arrays or lightweight modes to improve performance. Correct use of ORM requires combining SQL monitoring, caching, batch processing and appropriate optimization to ensure application performance while maintaining development efficiency.

Building Resilient Microservices with PHP and RabbitMQ Building Resilient Microservices with PHP and RabbitMQ Jul 27, 2025 am 04:32 AM

To build a flexible PHP microservice, you need to use RabbitMQ to achieve asynchronous communication, 1. Decouple the service through message queues to avoid cascade failures; 2. Configure persistent queues, persistent messages, release confirmation and manual ACK to ensure reliability; 3. Use exponential backoff retry, TTL and dead letter queue security processing failures; 4. Use tools such as supervisord to protect consumer processes and enable heartbeat mechanisms to ensure service health; and ultimately realize the ability of the system to continuously operate in failures.

Creating Production-Ready Docker Environments for PHP Creating Production-Ready Docker Environments for PHP Jul 27, 2025 am 04:32 AM

Using the correct PHP basic image and configuring a secure, performance-optimized Docker environment is the key to achieving production ready. 1. Select php:8.3-fpm-alpine as the basic image to reduce the attack surface and improve performance; 2. Disable dangerous functions through custom php.ini, turn off error display, and enable Opcache and JIT to enhance security and performance; 3. Use Nginx as the reverse proxy to restrict access to sensitive files and correctly forward PHP requests to PHP-FPM; 4. Use multi-stage optimization images to remove development dependencies, and set up non-root users to run containers; 5. Optional Supervisord to manage multiple processes such as cron; 6. Verify that no sensitive information leakage before deployment

python run shell command example python run shell command example Jul 26, 2025 am 07:50 AM

Use subprocess.run() to safely execute shell commands and capture output. It is recommended to pass parameters in lists to avoid injection risks; 2. When shell characteristics are required, you can set shell=True, but beware of command injection; 3. Use subprocess.Popen to realize real-time output processing; 4. Set check=True to throw exceptions when the command fails; 5. You can directly call chains to obtain output in a simple scenario; you should give priority to subprocess.run() in daily life to avoid using os.system() or deprecated modules. The above methods override the core usage of executing shell commands in Python.

VSCode settings.json location VSCode settings.json location Aug 01, 2025 am 06:12 AM

The settings.json file is located in the user-level or workspace-level path and is used to customize VSCode settings. 1. User-level path: Windows is C:\Users\\AppData\Roaming\Code\User\settings.json, macOS is /Users//Library/ApplicationSupport/Code/User/settings.json, Linux is /home//.config/Code/User/settings.json; 2. Workspace-level path: .vscode/settings in the project root directory

See all articles