PHP 網頁程式設計課程目錄

使用 PHP 程式取得網頁的 URL

這章課程討論如何取得網頁的 URL。

PHP 提供一些很快捷的方法摘取網頁的 URL。 但是, 每個不同的 PHP 版本可能會有些不同的結果, 這點必須留意。

第一步: 加入以下 PHP 語言於 HTML 文件中

<html>
<body>
<p>PHP Open and Read text File</p>

<?php

// Example URL
// http://php-learning-simple.mygreatname.com/php-learning/php-get-url-of-web-page-1.php

// HTTP_HOST will dispaly www.domain name
// www.php-learning-simple.co.cc
echo "HTTP_HOST will display: " . $_SERVER['HTTP_HOST'];
echo "<br /><br />";

// HTTP_HOST will onlydispaly the domain name
// php-learning-simple.co.cc
echo "SERVER_NAME will display: " . $_SERVER["SERVER_NAME"];
echo "<br /><br />";

// PHP_SELF will display the file path WITHOUT the domain name
// /php-learning/php-get-url-of-web-page-1.php
echo "PHP_SELF will display: " .$_SERVER['PHP_SELF'];
echo "<br /><br />";

// REQUEST_URI will also display the file path WITHOUT the domain name
// /php-learning/php-get-url-of-web-page-1.php
echo "REQUEST_URI will display: " . $_SERVER['REQUEST_URI'];
echo "<br /><br />";

// Nothing will be displayed
// It depends on the version of PHP
echo "QUERY_STRING will display: " . $_SERVER['QUERY_STRING'];
echo "<br /><br />";

?>


</body>
</html>

儲存及測試 php-get-url-of-web-page-1.php 是否可以正常執行.

PHP 實例:

View PHP Example

瀏 灠 器 應 出 現 :

HTTP_HOST will display: www.php-learning-simple.co.cc

SERVER_NAME will display: php-learning-simple.co.cc

PHP_SELF will display: /php-learning/php-get-url-of-web-page-1.php

REQUEST_URI will display: /php-learning/php-get-url-of-web-page-1.php

QUERY_STRING will display: