1、PHP 如何獲取當前的域名
<?
//獲取當前的域名:
echo $_SERVER['SERVER_NAME'];
//獲取來源網址,即點擊來到本頁的上頁網址
echo $_SERVER["HTTP_REFERER"];
$_SERVER['REQUEST_URI'];//獲取當前域名的後綴
$_SERVER['HTTP_HOST'];//獲取當前域名
dirname(__FILE__);//獲取當前文件的物理路徑
dirname(__FILE__)."/../";//獲取當前文件的上一級物理路徑
?>
(新頂級域名top域名O(∩_∩)O)
2、php怎麼獲取當前域名的主機名
$_SERVER['SERVER_NAME']可以獲取,用phpinfo()可以查看更多的這樣的有用變數。
3、如何用php 獲取域名對應的IP?
gethostbyname (PHP 3, PHP 4, PHP 5)
gethostbyname -- 獲取指定機器名的IP地址
函數格式說明:
string gethostbyname ( string hostname )
返回 hostname 的IP地址
例 1. A simple gethostbyname() example
<?php
$ip = gethostbyname('www.example.com');
echo $ip;
?>
4、PHP怎麼獲取來源域名
通過$_SERVER['HTTP_REFERER']就能獲取,但是對HTTPS可能就獲取不到
5、php如何獲取當前頁面url路徑
利用PHP實現
http://localhost/PHP/XX.php?id=5
1、//獲取域名或主機地址
echo $_SERVER['HTTP_HOST']; #localhost
2、//獲取網頁地址
echo $_SERVER['PHP_SELF']; #/PHP/XX.php
3、//獲取網址參數
echo $_SERVER["QUERY_STRING"]; #id=5
4、//獲取用戶代理
echo $_SERVER['HTTP_REFERER'];
5、//獲取完整的url
echo 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
echo'http://'.$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'].'?'.$_SERVER['QUERY_STRING'];
#http://localhost/PHP/XX.php?id=5
(5)php獲取域名地址擴展資料
PHP的特性包括:
1. PHP 獨特的語法混合了 C、Java、Perl 以及 PHP 自創新的語法。
2. PHP可以比CGI或者Perl更快速的執行動態網頁——動態頁面方面,與其他的編程語言相比,
PHP是將程序嵌入到HTML文檔中去執行,執行效率比完全生成htmL標記的CGI要高許多;
PHP具有非常強大的功能,所有的CGI的功能PHP都能實現。
3. PHP支持幾乎所有流行的資料庫以及操作系統。
6、PHP怎麼獲取網站域名和地址
$_SERVER['HTTP_HOST'];
$_SERVER['SERVER_NAME'];
7、PHP獲取來路域名
$url = $_SERVER["HTTP_REFERER"]; //獲取完整的來路URL
$str = str_replace("http://","",$url); //去掉http://
$strdomain = explode("/",$str); // 以「/」分開成數組
$domain = $strdomain[0]; //取第一個「/」以前的字元
用上面的方法才准確無誤,如果你用PHP自帶的函數就不對如:
$_SERVER['SERVER_NAME'] 這個函數它獲取的是伺服器域名
8、PHP 獲取域名的幾種方法
獲取當前的域名:
echo $_SERVER['SERVER_NAME'];
//獲取來源網址,即點擊來到本頁的上頁網址
echo $_SERVER["HTTP_REFERER"];
$_SERVER['REQUEST_URI'];//獲取當前域名的後綴
$_SERVER['HTTP_HOST'];//獲取當前域名
dirname(__FILE__);//獲取當前文件的物理路徑
dirname(__FILE__)."/../";//獲取當前文件的上一級物理路徑
?>
9、php怎麼獲取域名之後的url
1,$_SERVER["QUERY_STRING"]
說明:查詢(query)的字元串
2,$_SERVER["REQUEST_URI"]
說明:訪問此頁面所需的URI
3,$_SERVER["SCRIPT_NAME"]
說明:包含當前腳本的路徑
4,$_SERVER["PHP_SELF"]
說明:當前正在執行腳本的文件名
實例:
1,http://www.biuuu.com/ (直接打開主頁)
結果:
$_SERVER["QUERY_STRING"] = ""
$_SERVER["REQUEST_URI"] = "/"
$_SERVER["SCRIPT_NAME"] = "/index.php"
$_SERVER["PHP_SELF"] = "/index.php"
2,http://www.biuuu.com/?p=222 (附帶查詢)
結果:
$_SERVER["QUERY_STRING"] = "p=222"
$_SERVER["REQUEST_URI"] = "/?p=222"
$_SERVER["SCRIPT_NAME"] = "/index.php"
$_SERVER["PHP_SELF"] = "/index.php"
3,http://www.biuuu.com/index.php?p=222&q=biuuu
結果:
$_SERVER["QUERY_STRING"] = "p=222&q=biuuu"
$_SERVER["REQUEST_URI"] = "/index.php?p=222&q=biuuu"
$_SERVER["SCRIPT_NAME"] = "/index.php"
$_SERVER["PHP_SELF"] = "/index.php"
$_SERVER["QUERY_STRING"]獲取查詢語句,實例中可知,獲取的是?後面的值
$_SERVER["REQUEST_URI"] 獲取http://www.biuuu.com後面的值,包括/
$_SERVER["SCRIPT_NAME"] 獲取當前腳本的路徑,如:index.php
$_SERVER["PHP_SELF"] 當前正在執行腳本的文件名
當前url:"http://".$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF']
總結一下,對於QUERY_STRING,REQUEST_URI,SCRIPT_NAME和PHP_SELF,深入了解將有利於我們在$_SERVER函數中正確調用這四個值。通過實例詳解$_SERVER函數中QUERY_STRING,REQUEST_URI,SCRIPT_NAME和PHP_SELF掌握四個變數之間的區別。
$_SERVER["REQUEST_URI"] :獲取當前請求的完整的(除域名的)url。。。
uchome系統中處理技巧:
代碼
//處理REQUEST_URI
if(!isset($_SERVER['REQUEST_URI'])) {
$_SERVER['REQUEST_URI'] = $_SERVER['PHP_SELF'];
if(isset($_SERVER['QUERY_STRING'])) $_SERVER['REQUEST_URI'] .= '?'.$_SERVER['QUERY_STRING'];
}
if($_SERVER['REQUEST_URI']) {
$temp = urldecode($_SERVER['REQUEST_URI']);
if(strexists($temp, '<') || strexists($temp, '"')) {
$_GET = shtmlspecialchars($_GET);//XSS
}
}
代碼如下:
代碼
<?php
echo $_SERVER['DOCUMENT_ROOT']."<br>"; //獲得伺服器文檔根變數
echo $_SERVER['PHP_SELF']."<br>"; //獲得執行該代碼的文件伺服器絕對路徑的變數
echo __FILE__."<br>"; //獲得文件的文件系統絕對路徑的變數
echo dirname(__FILE__); //獲得文件所在的文件夾路徑的函數
?>
//server函數
$_SERVER["HTTP_REFERER"]=http://localhost/lianxi/
$_SERVER["HTTP_ACCEPT_LANGUAGE"]=zh-cn
$_SERVER["HTTP_ACCEPT_ENCODING"]=gzip, deflate
$_SERVER["HTTP_USER_AGENT"]=Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.1.4322; .NET CLR 2.0.50727)
$_SERVER["HTTP_HOST"]=localhost
$_SERVER["HTTP_CONNECTION"]=Keep-Alive
$_SERVER["PATH"]=C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\Program Files\Common Files\Adobe\AGL;C:\Program Files\MySQL\MySQL Server 5.0\bin;C:\php;C:\php\ext
$_SERVER["SystemRoot"]=C:\WINDOWS
$_SERVER["COMSPEC"]=C:\WINDOWS\system32\cmd.exe
$_SERVER["PATHEXT"]=.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH
$_SERVER["WINDIR"]=C:\WINDOWS
$_SERVER["SERVER_SIGNATURE"]=
Apache/2.0.55 (Win32) PHP/5.1.1 Server at localhost Port 80 \\使用的何伺服器
$_SERVER["SERVER_SOFTWARE"]=Apache/2.0.55 (Win32) PHP/5.1.1
$_SERVER["SERVER_NAME"]=localhost \\伺服器名稱
$_SERVER["SERVER_ADDR"]=127.0.0.1
$_SERVER["SERVER_PORT"]=80 \\伺服器埠
$_SERVER["REMOTE_ADDR"]=127.0.0.1
$_SERVER["DOCUMENT_ROOT"]=D:/lianxi \\網站的主目錄
$_SERVER["SERVER_ADMIN"][email protected] \\安裝APACHE時設置的郵箱
$_SERVER["SCRIPT_FILENAME"]=D:/lianxi/lianxi/servervalues.php \\當前的網頁的絕對路徑,
$_SERVER["REMOTE_PORT"]=1076 \\遠程埠
$_SERVER["GATEWAY_INTERFACE"]=CGI/1.1
$_SERVER["SERVER_PROTOCOL"]=HTTP/1.1
$_SERVER["REQUEST_METHOD"]=GET
$_SERVER["QUERY_STRING"]=\\獲取?號後面的內容
$_SERVER["REQUEST_URI"]=例子:/lianxi/servervalues.php?a=1&b=2
$_SERVER["SCRIPT_NAME"]=例子:/lianxi/servervalues.php
$_SERVER["PHP_SELF"]=/lianxi/servervalues.php \\返回當前網頁的相對路徑.
$_SERVER["REQUEST_TIME"]=1179190013 \\運行時間 單位為十萬分之一毫秒
$_SERVER["argv"]=Array
$_SERVER["argc"]=0
1,$_SERVER["QUERY_STRING"]
說明:查詢(query)的字元串
2,$_SERVER["REQUEST_URI"]
說明:訪問此頁面所需的URI
3,$_SERVER["SCRIPT_NAME"]
說明:包含當前腳本的路徑
4,$_SERVER["PHP_SELF"]
說明:當前正在執行腳本的文件名
實例:
1,http://www.biuuu.com/ (直接打開主頁)
結果:
$_SERVER["QUERY_STRING"] = 「」
$_SERVER["REQUEST_URI"] = 「/」
$_SERVER["SCRIPT_NAME"] = 「/index.php」
$_SERVER["PHP_SELF"] = 「/index.php」
2,http://www.biuuu.com/?p=222 (附帶查詢)
結果:
$_SERVER["QUERY_STRING"] = 「p=222″
$_SERVER["REQUEST_URI"] = 「/?p=222″
$_SERVER["SCRIPT_NAME"] = 「/index.php」
$_SERVER["PHP_SELF"] = 「/index.php」
3,http://www.biuuu.com/index.php?p=222&q=biuuu
結果:
$_SERVER["QUERY_STRING"] = 「p=222&q=biuuu」
$_SERVER["REQUEST_URI"] = 「/index.php?p=222&q=biuuu」
$_SERVER["SCRIPT_NAME"] = 「/index.php」
$_SERVER["PHP_SELF"] = 「/index.php」
$_SERVER["QUERY_STRING"]獲取查詢語句,實例中可知,獲取的是?後面的值
$_SERVER["REQUEST_URI"] 獲取http://www.biuuu.com後面的值,包括/
$_SERVER["SCRIPT_NAME"] 獲取當前腳本的路徑,如:index.php
$_SERVER["PHP_SELF"] 當前正在執行腳本的文件名
代碼
<?php
/**
__FILE__ ,
getcwd(),
$_SERVER["REQUEST_URI"],
$_SERVER["SCRIPT_NAME"],
$_SERVER["PHP_SELF"],
$_SERVER["SCRIPT_FILENAME"],
來觀察一下這些變數或函數的異同.
假設有一個請求地址為: http://localhost:8080/test.php/age=20
而test.php 的完整路徑是: D:/server/www/example/test.php
1) getcwd()
將得到瀏覽器請求的頁面文件所在的目錄. 即test.php 文件所在的目錄: D:/server/www/example/ ,
如果在test.php 執行了 require 或 include 語句, 比如 inculde(」test_dir/test2.php」),
那麼在 test2.php 里 getcwd()函數 返回的也將是 test.php 所在的目錄.
2) __FILE__
一個魔術變數, 用它將得到 __FILE__ 變數所在文件的完整路徑,
比如: test.php 里 __FILE__ 將得到 D:/server/www/example/test.php ,
test_dir/test2.php 里的 __FILE__ 將得到 D:/server/www/example/test_dir/test2.php
3) $_SERVER["SCRIPT_FILENAME"]
將得到瀏覽器請求的頁面文件的完整路徑.
test.php 和 test_dir/test2.php 里用 $_SERVER["SCRIPT_NAME"] 都將得到 D:/server/www/example/test.php.
4) $_SERVER["SCRIPT_NAME"]
將得到瀏覽器請求的頁面文件的文件名,注意: 與 $_SERVER["SCRIPT_NAME"] 不同, 此變數只得到文件名而不包含路徑,
在test.php 與 test_dir/test2.php 用$_SERVER["SCRIPT_NAME"] 得到的都將是 test.php.
當然, 在test.php 與 test_dir/test2.php 執行 basename($_SERVER["SCRIPT_FILENAME"]) 與 $_SERVER["SCRIPT_NAME"] 相同.
執行 在test.php 與 test_dir/test2.php 執行 realpath(」test.php」) 得到的結果與 $_SERVER["SCRIPT_FILENAME"] 相同.
5) $_SERVER["PHP_SELF"]
將得到瀏覽器請求頁面的文件名, 並剝掉問號 ? 後的內容, 注意:不包含路徑,
比如在客戶端里請求 http://localhost:8080/test.php?age=20&name=Tom,
那麼test.php 和 test_dir/test2.php 的 $_SERVER["PHP_SELF"] 都將得到 「test.php」。「age=20&name=Tom」被剝掉。
而如果客戶端里請求 http://localhost:8080/test.php/age=20&name=Tom,
那麼test.php 和 test_dir/test2.php 的 $_SERVER["PHP_SELF"] 都將得到 「test.php/age=20&name=Tom」。
6) $_SERVER["REQUEST_URI"]
將得到瀏覽器請求頁面的文件名, 以及文件名之後的所有內容(注意: 井號 # 之後的內容將被略去),
比如在客戶端里請求 http://localhost:8080/test.php?age=20&name=Tom,
那麼test.php 和 test_dir/test2.php 的 $_SERVER["REUEST_URI"] 都將得到 「test.php」。「age=20&name=Tom」被剝掉。
而如果客戶端里請求 http://localhost:8080/test.php/age=20&name=Tom,
那麼test.php 和 test_dir/test2.php 的 $_SERVER["REQUEST_URI"] 都將得到 「test.php/age=20&name=Tom」。
*/
// test.php:
echo 「test1.php variables <br />」;
echo 「getcwd: 「, getcwd(), 「<br />」;
echo 「__FILE__: 「, __FILE__, 「<br />」;
echo 「REQUEST_URI: 「, $_SERVER["REQUEST_URI"], 「<br />」;
echo 「SCRIPT_NAME: 「, $_SERVER["SCRIPT_NAME"], 「<br />」;
echo 「PHP_SELF: 「, $_SERVER["PHP_SELF"], 「<br />」;
echo 「SCRIPT_FILENAME 「, $_SERVER["SCRIPT_FILENAME"] , 「<br />」;
// 把 test2.php 包含進來, 在 test2.php 里輸出上面的變數,看有什麼不同:
include_once(」test2/test2.php」);
?>
10、php如何獲得調用網頁的網址
用 file()函數
下面的代碼是
通過PHP的File函數庫來完成上傳圖像文件並讓其顯示
// FILE 1: DISPLAY AND PROCESS ENTRY FORM AND UPLOAD PICTURE FILE TO SERVER
<?php
// full directory path
$filepath = "/home/httpd/html/tut/upload";
// 200K is the maximum (picture) file size to be accepted
define("MAX_FILE_SIZE", 200*1024);
function print_error ($err) {
echo "<h1>$err</h1><hr>";
}
do {
// check if picture name variable has a value; if not, skip to the
// "while(false)" section of "do" statement
if(isset($picture)) {
// here is where the server transparently checks that the client picture file
// doesn't exceed maximum allowable size
if(getenv("CONTENT_LENGTH") > MAX_FILE_SIZE) {
print_error("File too large: $picture_name");
break;
}
// open client picture file for read only; "@" prefix tells fopen not to print
// message if there is an error, since function print_error does that
// if there is an error, break out of "do" loop and continue at "while(false)"
$fp = @fopen($picture,"r");
if(!$fp) {
print_error("Cannot open file: $picture_name");
break;
}
// generate unique name for session, use it to generate unique server
// directory name, and create the directory
srand((double) microtime() * 1000000);
$id = md5(uniqid(rand()));
$dirname = "$filepath/$id";
mkdir($dirname,0700);
// create the server picture file in the newly created server directory
$filename = $dirname . "/picture";
// open server picture file for write only; "@" prefix tells fopen not to
// print message if there is an error, since function print_error does that
// if there is an error, break out of "do" loop and continue at "while(false)"
$out = @fopen($filename,"w");
if(!$out) {
print_error("Cannot open file: $filename");
break;
}
// copy client picture file to server picture file
while($buffer = fread($fp,8192)) {
fwrite($out,$buffer);
}
// close client picture file and server picture file
fclose($fp);
fclose($out);
// create server name file in picture file directory; this file will hold the
// name of the picture file
$filename = $dirname . "/name";
// open server name file for write only; "@" prefix tells fopen not to print
// message if there is an error, since function print_error does that
// if there is an error, break out of "do" loop and continue at "while(false)"
$out = @fopen($filename,"w");
if(!$out) {
print_error("Cannot open file: $filename");
break;
}
// write the server picture name to the server name file, and close the server
// name file
fputs($out,$name);
fclose($out);
// display message that client picture file was successfully copied to the
// server, display a prompt to look at updated server photo gallery, and supply
// the HTML link
?>
Picture added. Thanks.<br>
<a href="upload_display.php">Continue to the gallery</a>
<?php
// exit to the server photo gallery
exit();
}
} while(false);
// you get to here only when "if(isset($picture))" is false, which means that
// no picture name has been submitted, therefore go display the input form where
// the necessary information can be entered
?>
<!-- start upload form -->
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN">
<html>
<head>
<title>Photo gallery - add</title>
</head>
<body bgcolor="white">
<h1>Photo gallery add</h1>
<?php
// start of segment of code for displaying input form
// using $PHP_SELF for value of "form action" causes form to refer to itself
// when "submit" button is clicked
?>
<form action="<? echo $PHP_SELF ?>" method=POST ENCTYPE="multipart/form-data">
<?php
// pass the PHP constant MAX_FILE_SIZE to the HTML maximum file size
// constant MAX_FILE_SIZE
?>
<INPUT TYPE="hidden" name="MAX_FILE_SIZE" value="<? echo MAX_FILE_SIZE ?>">
<?php
// display the text boxes for entering user name and picture name, and store
// the entered values in PHP variables; browsing is enabled
?>
Your name is: <INPUT NAME="name"><br>
Your picture: <INPUT NAME="picture" TYPE="file"><br>
<?php
// display the "submit" button
?>
<INPUT TYPE="submit" VALUE="Add picture" name="send">
</form>
</body>
</html>
// ------------------------------------------------------
// FILE 2: DISPLAY THE SERVER PHOTO GALLERY
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN">
<html>
<head>
<title>Photo gallery</title>
</head>
<body>
<h1>Photo gallery</h1>
<?php
// full directory path
$filepath = "/home/httpd/html/tut/upload";
// user's path in browser -- same as full directory path
$url_path = "/tut/upload";
// get unique server directory used for this user session
$dir = dir($filepath);
// loop through all server subdirectories for this user session
while($entry=$dir->read()) {
// if entry is system file (doesn't have picture files), go to next entry in
// "while" loop
if($entry == "." || $entry == "..") {
continue;
}
// open server name file for read only; "@" prefix tells fopen not to print
// message if there is an error, since function print_error does that
// if there is an error, go to next entry in "while" loop
$fp = @fopen("$filepath/$entry/name","r");
if(!$fp) {
print "Bad entry: $entry<br>";
continue;
}
// get name of the server picture file and close the server name file
$name = fgets($fp,4096);
fclose($fp);
// display each picture and its file name; in addition, "alt=" causes the file
// name to be displayed as ToolTip text when mouse points to picture
?>
<img src="<? echo "$url_path/$entry/picture" ?>"
alt="<? echo $name ?>"> <b><? echo $name ?></b><br>
<?
}
?>
</body>
</html>