Rick Rick - 6 months ago 12
PHP Question

PHP: Get URL except current file

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/

Answer

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

Comments