Trying to get a full URL without filename in PHP

后端 未结 7 1686
鱼传尺愫
鱼传尺愫 2020-12-31 01:32

I thought this would be simple but I can\'t seem to find a variable of $_SERVER array that has what I\'m looking for.

Let\'s say my url is http://

7条回答
  •  一整个雨季
    2020-12-31 02:30

    There won't be one because it's not a useful attribute to track. If the current script is something other than index.* then it will not return the correct URL for the current script.

    However, this might get you what you want:

    echo '//'.$_SERVER['HTTP_HOST'].dirname($_SERVER['PHP_SELF']);
    

    Edit:
    Also, aside from installing the php.net online manual search tool for Firefox and setting it up with a search keyword (which will let you pull up the documentation on the $_SERVER global by typing something like "php $_server" into your URL bar), you should also know that var_dump() and print_r() lets you output the full contents of any variable, including arrays and objects. This is very useful for debugging.

提交回复
热议问题