2

I am trying to get the URL for the my webpage, but want to exclude the current file that I am working on. I have the following code, but it displays the fukl URL with the php file.

function getUrl() {
    $url  = @( $_SERVER["HTTPS"] != 'on' ) ? 'http://'.$_SERVER["SERVER_NAME"] :  'https://'.$_SERVER["SERVER_NAME"];
    $url .= ( $_SERVER["SERVER_PORT"] !== 80 ) ? ":".$_SERVER["SERVER_PORT"] : "";
    $url .= $_SERVER["REQUEST_URI"];
return $url;

This returns http://127.0.0.1:80/proyect/register_form.php and I want http://127.0.0.1:80/proyect/

Rick
  • 2,288
  • 18
  • 66
  • 98

2 Answers2

2

Use dirname function.

$url = 'http://127.0.0.1:80/proyect/register_form.php';
echo dirname($url);

output

http://127.0.0.1:80/proyect
Richard
  • 1,045
  • 7
  • 11
2

Since you have to have a million points to do anything, I was gonna mark this as a duplicate, but a mod can do it when they delete my post.

Since I also can't comment Mister Martin would receive my vote, his answer seems to be correct as it returns the filepath up until your actual file, sans the domain name.

$url .= dirname($_SERVER["REQUEST_URI"]);
echo $url;

Also look here if you need more info, I found this one here Trying to get a full URL without filename in PHP

Community
  • 1
  • 1
semaj0
  • 137
  • 1
  • 14