How to get base URL with PHP?

2019-01-02 19:46发布

I am using XAMPP on Windows Vista. In my development, I have http://127.0.0.1/test_website/.

How do I get http://127.0.0.1/test_website/ with PHP?

I tried something like these, but none of them worked.

echo dirname(__FILE__)
or
echo basename(__FILE__);
etc.

标签: php
19条回答
浮光初槿花落
2楼-- · 2019-01-02 20:35

Edited at @user3832931 's answer to include server port..

to form URLs like 'https://localhost:8000/folder/'

$base_url="http://".$_SERVER['SERVER_NAME'].':'.$_SERVER['SERVER_PORT'].dirname($_SERVER["REQUEST_URI"].'?').'/';
查看更多
登录 后发表回答