搜索
首页后端开发php教程php获取当前url路径 php服务器变量

在php编程中编程,获取当前url地址,以及服务器变量,主要使用如下全局变量: $_server["query_string"],$_server["request_uri"],$_server["script_name"],$_server["php_self"]

1,$_server["query_string"] 说明:查询(query)的字符串 2,$_server["request_uri"] 说明:访问此页面所需的uri 3,$_server["script_name"] 说明:包含当前脚本的路径 4,$_server["php_self"] 说明:当前正在执行脚本的文件名 实例: 1,http://bbs.it-home.org/ (直接打开主页) 结果: $_server["query_string"] = "" $_server["request_uri"] = "/" $_server["script_name"] = "/index.php" $_server["php_self"] = "/index.php" 2,http://bbs.it-home.org/?p=222 (附带查询) 结果: $_server["query_string"] = "p=222" $_server["request_uri"] = "/?p=222" $_server["script_name"] = "/index.php" $_server["php_self"] = "/index.php" 3,http://bbs.it-home.org/index.php?p=222&q=biuuu 结果:

  1. //处理request_uri

  2. if(!isset($_server['request_uri'])) {
  3. $_server['request_uri'] = $_server['php_self'];
  4. if(isset($_server['query_string'])) $_server['request_uri'] .= '?'.$_server['query_string'];
  5. }
  6. if($_server['request_uri']) {
  7. $temp = urldecode($_server['request_uri']);
  8. if(strexists($temp, ' $_get = shtmlspecialchars($_get);//xss
  9. }
  10. }
  11. echo $_server['document_root']."
    "; //获得服务器文档根变量

  12. echo $_server['php_self']."
    "; //获得执行该代码的文件服务器绝对路径的变量
  13. echo __file__."
    "; //获得文件的文件系统绝对路径的变量
  14. echo dirname(__file__); //获得文件所在的文件夹路径的函数
  15. ?>
  16. //server函数
  17. $_server["http_referer"]=http://localhost/lianxi/
  18. $_server["http_accept_language"]=zh-cn
  19. $_server["http_accept_encoding"]=gzip, deflate
  20. $_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)
  21. $_server["http_host"]=localhost
  22. $_server["http_connection"]=keep-alive
  23. $_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
  24. $_server["systemroot"]=c:\windows
  25. $_server["comspec"]=c:\windows\system32\cmd.exe
  26. $_server["pathext"]=.com;.exe;.bat;.cmd;.vbs;.vbe;.js;.jse;.wsf;.wsh
  27. $_server["windir"]=c:\windows
  28. $_server["server_signature"]=
  29. apache/2.0.55 (win32) php/5.1.1 server at localhost port 80 \\使用的何服务器
  30. $_server["server_software"]=apache/2.0.55 (win32) php/5.1.1
  31. $_server["server_name"]=localhost \\服务器名称
  32. $_server["server_addr"]=127.0.0.1
  33. $_server["server_port"]=80 \\服务器端口
  34. $_server["remote_addr"]=127.0.0.1
  35. $_server["document_root"]=d:/lianxi \\网站的主目录
  36. $_server["server_admin"]=sss@163.com \\安装apache时设置的邮箱
  37. $_server["script_filename"]=d:/lianxi/lianxi/servervalues.php \\当前的网页的绝对路径,
  38. $_server["remote_port"]=1076 \\远程端口
  39. $_server["gateway_interface"]=cgi/1.1
  40. $_server["server_protocol"]=http/1.1
  41. $_server["request_method"]=get
  42. $_server["query_string"]=\\获取?号后面的内容
  43. $_server["request_uri"]=例子:/lianxi/servervalues.php?a=1&b=2
  44. $_server["script_name"]=例子:/lianxi/servervalues.php
  45. $_server["php_self"]=/lianxi/servervalues.php \\返回当前网页的相对路径.
  46. $_server["request_time"]=1179190013 \\运行时间 单位为十万分之一毫秒
  47. $_server["argv"]=array
  48. $_server["argc"]=0
复制代码

例2:

  1. /**
  2. __file__ ,
  3. getcwd(),
  4. $_server["request_uri"],
  5. $_server["script_name"],
  6. $_server["php_self"],
  7. $_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:

  1. echo “test1.php variables
    ”;
  2. echo “getcwd: “, getcwd(), “
    ”;
  3. echo “__file__: “, __file__, “
    ”;
  4. echo “request_uri: “, $_server["request_uri"], “
    ”;
  5. echo “script_name: “, $_server["script_name"], “
    ”;
  6. echo “php_self: “, $_server["php_self"], “
    ”;
  7. echo “script_filename “, $_server["script_filename"] , “
    ”;
  8. // 把 test2.php 包含进来, 在 test2.php 里输出上面的变量,看有什么不同:
  9. include_once(”test2/test2.php”);
  10. ?>
复制代码


声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
继续使用PHP:耐力的原因继续使用PHP:耐力的原因Apr 19, 2025 am 12:23 AM

PHP仍然流行的原因是其易用性、灵活性和强大的生态系统。1)易用性和简单语法使其成为初学者的首选。2)与web开发紧密结合,处理HTTP请求和数据库交互出色。3)庞大的生态系统提供了丰富的工具和库。4)活跃的社区和开源性质使其适应新需求和技术趋势。

PHP和Python:探索他们的相似性和差异PHP和Python:探索他们的相似性和差异Apr 19, 2025 am 12:21 AM

PHP和Python都是高层次的编程语言,广泛应用于Web开发、数据处理和自动化任务。1.PHP常用于构建动态网站和内容管理系统,而Python常用于构建Web框架和数据科学。2.PHP使用echo输出内容,Python使用print。3.两者都支持面向对象编程,但语法和关键字不同。4.PHP支持弱类型转换,Python则更严格。5.PHP性能优化包括使用OPcache和异步编程,Python则使用cProfile和异步编程。

PHP和Python:解释了不同的范例PHP和Python:解释了不同的范例Apr 18, 2025 am 12:26 AM

PHP主要是过程式编程,但也支持面向对象编程(OOP);Python支持多种范式,包括OOP、函数式和过程式编程。PHP适合web开发,Python适用于多种应用,如数据分析和机器学习。

PHP和Python:深入了解他们的历史PHP和Python:深入了解他们的历史Apr 18, 2025 am 12:25 AM

PHP起源于1994年,由RasmusLerdorf开发,最初用于跟踪网站访问者,逐渐演变为服务器端脚本语言,广泛应用于网页开发。Python由GuidovanRossum于1980年代末开发,1991年首次发布,强调代码可读性和简洁性,适用于科学计算、数据分析等领域。

在PHP和Python之间进行选择:指南在PHP和Python之间进行选择:指南Apr 18, 2025 am 12:24 AM

PHP适合网页开发和快速原型开发,Python适用于数据科学和机器学习。1.PHP用于动态网页开发,语法简单,适合快速开发。2.Python语法简洁,适用于多领域,库生态系统强大。

PHP和框架:现代化语言PHP和框架:现代化语言Apr 18, 2025 am 12:14 AM

PHP在现代化进程中仍然重要,因为它支持大量网站和应用,并通过框架适应开发需求。1.PHP7提升了性能并引入了新功能。2.现代框架如Laravel、Symfony和CodeIgniter简化开发,提高代码质量。3.性能优化和最佳实践进一步提升应用效率。

PHP的影响:网络开发及以后PHP的影响:网络开发及以后Apr 18, 2025 am 12:10 AM

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

PHP类型提示如何起作用,包括标量类型,返回类型,联合类型和无效类型?PHP类型提示如何起作用,包括标量类型,返回类型,联合类型和无效类型?Apr 17, 2025 am 12:25 AM

PHP类型提示提升代码质量和可读性。1)标量类型提示:自PHP7.0起,允许在函数参数中指定基本数据类型,如int、float等。2)返回类型提示:确保函数返回值类型的一致性。3)联合类型提示:自PHP8.0起,允许在函数参数或返回值中指定多个类型。4)可空类型提示:允许包含null值,处理可能返回空值的函数。

See all articles

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

AI Hentai Generator

AI Hentai Generator

免费生成ai无尽的。

热工具

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

SecLists

SecLists

SecLists是最终安全测试人员的伙伴。它是一个包含各种类型列表的集合,这些列表在安全评估过程中经常使用,都在一个地方。SecLists通过方便地提供安全测试人员可能需要的所有列表,帮助提高安全测试的效率和生产力。列表类型包括用户名、密码、URL、模糊测试有效载荷、敏感数据模式、Web shell等等。测试人员只需将此存储库拉到新的测试机上,他就可以访问到所需的每种类型的列表。

PhpStorm Mac 版本

PhpStorm Mac 版本

最新(2018.2.1 )专业的PHP集成开发工具

Atom编辑器mac版下载

Atom编辑器mac版下载

最流行的的开源编辑器

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

功能强大的PHP集成开发环境