|
在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 结果:
例2:
这些变量或函数的异同. 假设有一个请求地址为: 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:
|
PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号