Forum Moderators: coopster
I use $_SERVER["SCRIPT_NAME"] in order to work out where on the server the PHP app has been installed. This is then used (amongst other things) to build absolute links in the resulting page sent to the client, of the form:
<a href="<?=_INSTALLDIR?>index.php">Link</a>
This works great on a couple of development servers, with a fairly standard Apache installation, but on the big ol' live server (Apache 2, PHP 5) it doesn't. This seems to be because of either virtual directories and/or URL rewriting (?!) that results in SCRIPT_NAME / PHP_SELF etc. all returning the file system path to the current file, and *NOT* the URL that appears in the address bar, which is required to build the links.
The PHP app seems to be installed here:
/actualdir/index.php
However, the URL that the browser/client sees is:
/virtdir1/virtdir2/index.php
The problem is that if you try to follow a link to (or type into the address bar) "
/actualdir/index.php" you get a 404 error. So, the 'virtual' address is reqd. I would have thought, however, that the server should be configured to allow any requests to "
/actualdir/index.php" and 'redirect' or handle them appropriately, but it doesn't. Unfortunately I don't know how the live server is configured, nor have any direct control over it! (Besides, I'm not an Apache guru)
Is it an issue in my PHP code that I can resolve, or is it a server admin issue?!
The problem is also in my forms, where I set the action:
<form action="<?=$_SERVER["PHP_SELF"]?>"...
I can also resolve the
_INSTALLDIRissue by again hard coding this as a constant in my config file, but this is undesirable, and I would hope unnecessary (certainly the client doesn't like it, whom incidentally is supposed to have Apache-guru like abilities), with foreseeable complications in the future for this and other projects.
Any comments appreciated, thanks.
What are you thinking? Issueing a redirect or something? But redirecting to a virtual location?
Incidentally, the 'Apache-guru like' guy on the live server seems to think it is a downfall of PHP and not the Apache setup that is the problem. Only I'm not convinced.
Have you tryed var_dump [php.net]ing the $_SERVER array, sometimes I get things like REDIRECT_SCRIPT_URL come up, dumping it will let you see them all.
Andrew
In that, either...
- PHP *should* be returning, as one of the $_SERVER or $_ENV superglobals, a value that includes the client-side URL of the current page ...?
Or,
- The server *should* at least handle any requests to
/actualdir/index.phpand 'redirect' them appropriately ...?
Yes, PHP is tightly integrated with the Apache server and you'll need to know specifics about setup for certain variables if you intend to use them effectively.
phpinfo() [php.net] will tell you a lot of information but sometimes it is just easier to dump that $_SERVER superglobal to have a quick peek. You can do this as simple as
<pre>
<?php phpinfo();?>
</pre>
My problem, however, is that PHP is not returning anything that relates to the path in the current URL in the address bar - on this particular server - it may have been rewritten / a virtual directory or something? But, from the client end, the server can only handle requests to this URL path. Which makes it impossible for me to build (using PHP), absolute links in the resulting HTML pages.
- The workaround so far has been to simply hard-code this path as a constant in the configuration file, which works ok-ish for the moment, but if someone wants to move the app later this may need to be altered. And one of the goals initially was to make this completely independent of the folder structure.
- I've thought about changing all the links to relative paths. A lot of hassle and I'm not even sure it would work thoughout?!
- Also thought about the possibility of using JavaScript (on the very first page request) to send back to the server the actual URL, save it and then use this saved response for subsequent requests...? But this seems like a bit too much hassle for what should really be a simple problem?! And besides, the appropriate permissions probably won't be set, and it's not a DB app.
Hhhhmmm, anyway, thanks for your responses. Like I say 'an easy' workaround exists, but a none hard-coded solution would be better. Only I can't help but feel that this could be resolved with a bit of server-configuration, unfortunately that is out of my control in this case.