Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
224 views
in Technique[技术] by (71.8m points)

php - 获取PHP中的完整URL(Get the full URL in PHP)

I use this code to get the full URL:

(我使用以下代码来获取完整的URL:)

$actual_link = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'];

The problem is that I use some masks in my .htaccess , so what we see in the URL is not always the real path of the file.

(问题是我在.htaccess使用了一些掩码,因此我们在URL中看到的并不总是文件的真实路径。)

What I need is to get the URL, what is written in the URL, nothing more and nothing less—the full URL.

(我需要的是获取URL,写在URL中的内容,仅此而已-完整的URL。)

I need to get how it appears in the Navigation Bar in the web browser, and not the real path of the file on the server.

(我需要了解它在Web浏览器的导航栏中的显示方式,而不是服务器上文件的真实路径。)

  ask by DiegoP. translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Have a look at $_SERVER['REQUEST_URI'] , ie

(看看$_SERVER['REQUEST_URI'] ,即)

$actual_link = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";

(Note that the double quoted string syntax is perfectly correct )

((请注意,双引号字符串语法完全正确 ))

If you want to support both HTTP and HTTPS, you can use

(如果要同时支持HTTP和HTTPS,则可以使用)

$actual_link = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http") . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";

Editor's note: using this code has security implications .

(编者注:使用此代码具有安全性 。)

The client can set HTTP_HOST and REQUEST_URI to any arbitrary value it wants.

(客户端可以将HTTP_HOST和REQUEST_URI设置为所需的任意值。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...