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>