tags:

views:

47

answers:

2

I am trying to get the URL from a function I wrote. But because the function is included into the file, its brining back the include path and not the url. Is there a work around?

i should get /example.php but instead i get includes/url-logger.php

this is done via $_SERVER['request_uri']

cheers

A: 

I'm not sure what you mean but perhaps echo $_SERVER['SCRIPT_FILENAME'] and echo __FILE__ will help?

Norbert
I have the url; http://www.example.com/example.php i need the /example.php part but because say includes/logvisit.php is includes from the examples.php instead of getting examples.php back i get includes/logvisit.php... this is on request_uri too
johnsz
A: 

You'll need to use the special __FILE__ inside the PHP file you want the name of. This will give you the full server path (e.g. something like /var/www/sitename/file.php) so to get the URL, you'll need to do something like this:

$path = str_replace( $_SERVER['DOCUMENT_ROOT'], '', __FILE__ );
$url = 'http://' . $_SERVER['HTTP_HOST'] . $path;

(NB you might need an extra forward slash somewhere...)

EDIT: based on the comment on the other answer, you might just need $_SERVER['PHP_SELF'], it's hard to tell.

DisgruntledGoat