PHP Url Relative and Absolute -
i have class:
class pagebuilder { public function getheader() { include(dirname(dirname(__file__)) . '/template/header.php'); } }
which when called insert header file page. good.
$page_builder->getheader();
in header.php
top of html
file includes menu. problem is, depending on pagebuilder
gets called changes menu link url
s.
how make sure relative root folder.
dirname(__file__)
doesn't work because turns url
file:///
, don't want append entire http://www.blahblah.com/blah
because if relative root doesn't matter of that.
edit
so posted can use $_server['']
1 reliable, no doubt php
have put in blinding caveats.
see thinking $_server['server_addr']
or $_server['server_name']
or $_server['http_host']
.....
going $_server['http_host']
.. result fail.
just prefixing /
resolves localhost root links localhost/admin/profile.php
rather localhost/testapp/admin/profile.php
, have specify sits in folder named testapp
?
you need differentiate between local file paths , urls first , foremost. 2 have nothing 1 another. php __file__
, dirname()
, related parts dealing local file paths on server's hard disk. urls on other hand entirely arbitrary strings you need put in sensible way. there infinite ways how 1 particular file may referred url, it's bring order this.
you could try figure out url requested using various paths available in $_server
, construct relative urls based on that. however, rather error prone , overly complicated. rather should using root-relative urls. i.e. instead of
foo/bar.html
which relative url browser on, use
/foo/bar.html
which refers same url regardless of url it's relative to.
if need prefix urls, i'd create wrapper function adds needed based on configuration or on automatically figuring out own root url based on $_server
.
Comments
Post a Comment