亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

Table of Contents
You didn't enter any arguments.
SampleApp Result
Home php教程 php手冊 PHP中通過Web執(zhí)行C/C++應用程序

PHP中通過Web執(zhí)行C/C++應用程序

Jun 21, 2016 am 09:15 AM
lt php

c++|web|程序|執(zhí)行

一、簡介
  
  如果你對Unix/Linux有所了解的話,你應該知道他們大都自帶了C和C++的編譯器,分別是GCC和G++。Unix在程序安裝及Make等許多地方使用到了這些編譯器。利用一些控制臺命令,C++和PHP, 我將向你介紹怎樣生成一個完整的C++程序例子,他可以在用PHP程序來執(zhí)行,并能獲得相應的輸出結果。我將先生成C++程序代碼,并編譯它,談后討論我們將如果通過使用PHP的函數passthru來執(zhí)行這個程序。從某種意義上來說,這邊文章給我們提供一種通過Web頁面來訪問一般程序的方法。
  
  為了能更好的理解這篇文章,你應該有一臺運行著apache和最新版本php的unix/Linux服務器。同時也應該掌握C++, unix控制臺命令,當然一些PHP的編程經驗也是必需的。
  
  二、編寫一個C++程序
  
  例如,我們可以寫一個能夠通過命令行還接收參數的C++ 簡單程序,并命名為Sampleapp.然后我們能夠按照下面的方式給他傳遞三個不同的參數 :
  
  Sampleapp ?參數一 ?參數二 ?參數三
  
  這個程序的功能是能輸出傳遞給他的參數的個數和每個參數的值,然后我們可以用PHP腳本程序來執(zhí)行編譯好的C++程序。
  
  利用你習慣的文本編輯器,新建一個名為Sampleapp.cpp 的文件,再此文件中輸入如下的代碼:
  
  #include
  
  int main(int argc, char* argv[])
  {
  cout     
  cout       
  for(int i = 1; i   cout   
  return 0;
  }
  這個C++程序包含的程序的入口點:main(),main()函數帶了兩個參數:argc(命令行傳入參數的個數)和argv(一個包含了所傳參數實際值的字符型指針數組)。這個兩個參數能被C++編譯器自動捕獲。
  
  cout     這句話的意思是獲得從執(zhí)行命令行傳入的參數的個數。Argv這個字符型指針數組是從0開始檢索的,它至少包含一個實際的值(即本程序的路徑和名稱),這個值由C++編譯器自動地附加上去。條件操作符”?”是用來判斷命令行傳入地參數是否多于一個。例如,如果命令行過傳入兩個參數,我們地程序將輸出如下信息:
  
  You passed 2 arguments.
  
  cout       接下來,我們同樣用條件操作符來輸出另一句話。不過要記住,即使我們不從程序執(zhí)行命令行傳入任何參數,main函數地argv[]參數也包含一個值。同樣地,如果我們從命令行傳入兩個參數給程序,程序將輸出如下地信息:
  
  These arguments are:
  
  for(int i = 1; i   cout   最后,main函數逐一的輸出命令行傳入的每個參數,它用到了一個簡單的for(;;)循環(huán)語句,這個函數能根據參數的個數將參數值一個一個的輸出。假如我們傳給程序兩個參數”first”和second”, for循環(huán)輸出的結果如下:
  
  [1] ?first
  [2] ?second
  
  以上是關于這個C++程序的簡單說明,它的功能十分簡單,就是將命令行傳入的參數用cout函數顯示在輸出屏幕上。
  
  接下來,我們將編譯這個.cpp文件,如果你在windows平臺下,需要telnet到所使用的server上。在這里,我們使用大多Unix機器上都提供的G++編譯器來編譯這個源文件。不過為了確信你的機器安裝了G++,你可以輸入如下命令:which g++。如果G++已經安裝了,Unix shell將顯示出G++所在的全路徑。如果沒有安裝,它將提示你說”command couldn’t be found”. 你可以在這里下載到G++.
  
  在源文件所在的目錄輸入如下G++命令:
  
  g++ -c sampleapp.cpp.
  通過這個命令,我們就將.cpp文件編譯成了包含機器代碼的目標文件。通過 ls ?a命令,你可以發(fā)現在本目錄下出現了一個新文件sampleapp.o,這就是.cpp源文件被編譯成機器碼的結果。不過我們最終想要的是一個可執(zhí)行文件,因為我們還要輸入如下的G++命令:
  
  g++ sampleapp.cpp ?o sampleapp  
  這樣我們就獲得了一個名為sampleapp的可執(zhí)行文件。不過注意的是,Unix下的可執(zhí)行文件跟Windows不一樣,它沒有任何后綴。
  
  下面我們可以來檢驗一下程序執(zhí)行的結果,如果如下命令:
  
  sampleapp one -two /three
  我們可以看到如下的執(zhí)行結果:
  
  You passed 3 arguments.
  These arguments are:
  
  [1] one
  [2] ?two
  [3] /three
  
  現在,可執(zhí)行的C++程序成生完畢,下面我們將生成一個能夠通過 web瀏覽器來訪問這個程序的PHP教本程序。
  
三、生成PHP腳本程序
  
  為了能通過Internet來調用我們的C++程序,我們需要生成一個PHP腳本程序。這個PHP腳本程序將有一個Form表單,以便用戶能輸入可以傳給程序Sampleapp的參數。PHP腳本的代碼太長就不在這里全部列出了,需要的話可以通過下面的地址來下載它。(Php code)
  
  if(@$submit)
  {
  
  }
  else
  {
  } 
  首先,腳本程序檢查看變量$submit是否有值,這個變量$submit的值是程序后面的Form表單提交后傳遞過來的,它缺省為空值。符號@的作用是當變量$submit的值不存在的時忽略相關的錯誤信息。
  
  由于變量$submit缺省為空,所以一開始執(zhí)行else{}中的代碼,它在瀏覽器上簡單的顯示一個Form表單。Form的action屬性設為變量$PHP_SELF,即表單提交后返回本頁。同時Form表單包含了一個文本輸入條,這是用來讓用戶輸入要傳遞給C++程序的命令行參數。Form如下圖所示:
  
  一旦我們輸入執(zhí)行命令并提交表單,變量$submit(即按鈕Go的名字)就獲得一個值,這樣PHP教本將執(zhí)行if{}之間的代碼。
  
  if($args == "")
  echo "

You didn't enter any arguments.

";
  else
  {
  echo "

SampleApp Result

";
  $command = "/htdocs/sampleapp " . escapeshellcmd($args);
  
  passthru($command);
  }  
  變量$args是自動產生的,它的值是Form表單中文本輸入條傳過來的值。如果沒有輸入任何信息,程序將簡單的告訴用戶沒有輸入任何值。
  
  如果用戶輸入任何非空的信息,程序將把text域的值,即變量$args傳給C++程序。下面這段代碼就是執(zhí)行C++的程序的執(zhí)行命令:
  
  $command = "/htdocs/sampleapp " . escapeshellcmd($args);
  函數eacapeshellcmd是用來當做安全檢查工具,以過濾調一些如”,”,””和”\”等的特殊字符。這可以防止一些用戶企圖輸入某些字符來調用系統(tǒng)內部命令。
  
  例如,如果你在Form表單的text域中輸入”1 ?two /three”,那么變量$command的值就為: /htdocs/sampleapp 1 ?two /three
  
  你能發(fā)現我們定義了程序sampleapp的全路徑,在這個例子中,程序文件位于/htdocs目錄下。你可以根據的自己程序所在的目錄做相應的修改。
  
  passthru($command);
  
  最后,我們使用PHP的函數passthru來執(zhí)行變量$command所包含的命令并且將原始的執(zhí)行結果輸出到瀏覽器上。在我的服務器上,返回結果的HTML頁面如下:
  
  w在本文即將結束之前,幾個可能碰到的問題我想說一下。首先,當你執(zhí)行sampleapp.php教本程序的時候,如果你沒有看到程序的任何輸出信息,或許是開了安全模式。如果這樣,系統(tǒng)將不會允許PHP腳本來執(zhí)行系統(tǒng)內部程序。關于如何關閉安全模式,請訪問網頁http://www.php.net/manual/en/features.safe-mode.php,上面有詳細的介紹。其次,在一些Unix系統(tǒng)上,PHP函數passthru不能將內部程序的輸出傳遞給瀏覽頁面,如果發(fā)生這種情況,可以用system函數來代替passthru函數。
  
  四、結論
  
  從本的例子可以看出,Unix操作系統(tǒng)非常強大,并且PHP允許開發(fā)者通過腳本以獨立的線程來執(zhí)行系統(tǒng)內部程序。本文的所給的例子非常的簡單,但是只要再多花一點功夫,你可以寫一個能更新Mysql數據庫的c++程序,運行其他系統(tǒng)命令的程序或者是操作系統(tǒng)文件/目錄結構的程序。但是,不管怎樣,你都應該確保你的系統(tǒng)安全,絕對不能讓任何其他的腳本程序隨意訪問系統(tǒng)內部程序。



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
1488
72
PHP calls AI intelligent voice assistant PHP voice interaction system construction PHP calls AI intelligent voice assistant PHP voice interaction system construction Jul 25, 2025 pm 08:45 PM

User voice input is captured and sent to the PHP backend through the MediaRecorder API of the front-end JavaScript; 2. PHP saves the audio as a temporary file and calls STTAPI (such as Google or Baidu voice recognition) to convert it into text; 3. PHP sends the text to an AI service (such as OpenAIGPT) to obtain intelligent reply; 4. PHP then calls TTSAPI (such as Baidu or Google voice synthesis) to convert the reply to a voice file; 5. PHP streams the voice file back to the front-end to play, completing interaction. The entire process is dominated by PHP to ensure seamless connection between all links.

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

PHP creates a blog comment system to monetize PHP comment review and anti-brush strategy PHP creates a blog comment system to monetize PHP comment review and anti-brush strategy Jul 25, 2025 pm 08:27 PM

1. Maximizing the commercial value of the comment system requires combining native advertising precise delivery, user paid value-added services (such as uploading pictures, top-up comments), influence incentive mechanism based on comment quality, and compliance anonymous data insight monetization; 2. The audit strategy should adopt a combination of pre-audit dynamic keyword filtering and user reporting mechanisms, supplemented by comment quality rating to achieve content hierarchical exposure; 3. Anti-brushing requires the construction of multi-layer defense: reCAPTCHAv3 sensorless verification, Honeypot honeypot field recognition robot, IP and timestamp frequency limit prevents watering, and content pattern recognition marks suspicious comments, and continuously iterate to deal with attacks.

How to use PHP to combine AI to generate image. PHP automatically generates art works How to use PHP to combine AI to generate image. PHP automatically generates art works Jul 25, 2025 pm 07:21 PM

PHP does not directly perform AI image processing, but integrates through APIs, because it is good at web development rather than computing-intensive tasks. API integration can achieve professional division of labor, reduce costs, and improve efficiency; 2. Integrating key technologies include using Guzzle or cURL to send HTTP requests, JSON data encoding and decoding, API key security authentication, asynchronous queue processing time-consuming tasks, robust error handling and retry mechanism, image storage and display; 3. Common challenges include API cost out of control, uncontrollable generation results, poor user experience, security risks and difficult data management. The response strategies are setting user quotas and caches, providing propt guidance and multi-picture selection, asynchronous notifications and progress prompts, key environment variable storage and content audit, and cloud storage.

PHP realizes commodity inventory management and monetization PHP inventory synchronization and alarm mechanism PHP realizes commodity inventory management and monetization PHP inventory synchronization and alarm mechanism Jul 25, 2025 pm 08:30 PM

PHP ensures inventory deduction atomicity through database transactions and FORUPDATE row locks to prevent high concurrent overselling; 2. Multi-platform inventory consistency depends on centralized management and event-driven synchronization, combining API/Webhook notifications and message queues to ensure reliable data transmission; 3. The alarm mechanism should set low inventory, zero/negative inventory, unsalable sales, replenishment cycles and abnormal fluctuations strategies in different scenarios, and select DingTalk, SMS or Email Responsible Persons according to the urgency, and the alarm information must be complete and clear to achieve business adaptation and rapid response.

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

PHP integrated AI speech recognition and translator PHP meeting record automatic generation solution PHP integrated AI speech recognition and translator PHP meeting record automatic generation solution Jul 25, 2025 pm 07:06 PM

Select the appropriate AI voice recognition service and integrate PHPSDK; 2. Use PHP to call ffmpeg to convert recordings into API-required formats (such as wav); 3. Upload files to cloud storage and call API asynchronous recognition; 4. Analyze JSON results and organize text using NLP technology; 5. Generate Word or Markdown documents to complete the automation of meeting records. The entire process needs to ensure data encryption, access control and compliance to ensure privacy and security.

See all articles