Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to generate absolute URL path to a script file?

This is almost same question as this

I need to get ABSOLUTE url to my script. For example, I have this structure

http://mydomain.com/downloads/games/freegame_222/ajax.php

My application root would be in freegame_222/

but, how can I "build" absolute, not relative path to ajax.php? Result will be

http://mydomain.com/downloads/games/freegame_222/ajax.php

or

/downloads/games/freegame_222/ajax.php

but not:

ajax.php

These examples will return after url("ajax.php"); is called

Is there a native function in PHP for it? If no, does anybody know about any custom one?

EDIT:

I want to call that page from another page, for example index.php

like image 839
genesis Avatar asked Jun 28 '11 18:06

genesis


1 Answers

Similar to George Cummins' answer, simply do something like such:

$path = $_SERVER['SERVER_NAME'] . '/' . $_SERVER['REQUEST_URI'];

This should give you the complete path. Now if you want to get this from another page, what reason is there to have it generate the url dynamically? Why can't you just use the absolute path, or if anything, $_SERVER['SERVER_NAME'] + rest of path so it works between domains/hosts.

like image 81
grep Avatar answered Nov 02 '22 23:11

grep