View Full Version : How do you get the URL?
I need to be able to figre out what the current page is in order to determin the proper path for an external PHP file. How do I capture the URL? (I can figure out the rest).
thetestingsite
05-13-2007, 11:43 PM
Well, you could use the combination of the $_SERVER variables to figure get this information. An example:
echo "http://".$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'];
This will display something along the lines of http://yourdomain.com/dir/file.php
Hope this helps.
djr33
05-14-2007, 12:57 AM
Hmm... $_SERVER['REQUEST_URI'] is quicker. That's exactly what was requested, though it would include extra stuff on the end, like get variales (.htm?var=value) and anchors (.htm#pictures).
alexjewell
05-14-2007, 09:00 PM
But you could use explode to split the url at the ? or #:
$url = $_SERVER['REQUEST_URI'];
$newurl = explode("?",$url);
So then $newurl[0] would be the url without the get variables. You can do this with anchors, too, by just replacing the "?" with "#"
But using Testing's solution is probably easier, anyway.
djr33
05-15-2007, 03:37 AM
It depends if the get variables are needed. In some cases, those determine how the page functions, like with a site based on a database with articles definied in the url, etc.
mwinter
05-15-2007, 06:11 PM
Hmm... $_SERVER['REQUEST_URI'] is quicker. That's exactly what was requested, though it would include extra stuff on the end, like get variales (.htm?var=value) and anchors (.htm#pictures).
No, it wouldn't. Anchors are irrelevant in HTTP requests and aren't included in the URI on the request line (the first line of a HTTP request).
I didn't mean to spark a debat lol. Anyway, thanks! I figured it out and the script works perfect! That right: MY LOGGIN SCRIPT IS FINALLY DONE! Over 1,000 lines-- whew! But you need not think that it runs slowly. Each file is used for different aspects of the script, for instance blocking users. Thanks for all of your help DD. Can't wait until I figure out what my next project will be. . .
djr33
05-16-2007, 01:09 AM
Mike, is that true if it is sent as the url?
For example, clicking this link:
<a href="mypage.htm#pictures">click</a>
I realize it wouldn't be part of the request if it were just on the page, using it as a target to move up or down to a specific section, but I'd think it would be part of it were it included that way. I was under the impression (perhaps just a random guess that I never tested) that you could use the # method to send a value to PHP, much like GET, but just that you would need to code the retrieval yourself (by splitting at '#').
And... if that is the case, then all the better for using the REQUEST_URI string instead.
Anyway, glad it's working. Don't worry about starting a debate. We're just discussing/learning. It's a good thing.
No, it's entirely client-side.
mwinter
05-16-2007, 06:06 PM
Mike, is that true if it is sent as the url?
Yes, that's what I was talking about. You can read for yourself in RFC 2616. You could also test for yourself by changing:
<a href="mypage.htm#pictures">click</a>
to:
<a href="mypage.php#pictures">click</a>
where mypage.php is:
<?php
header('Content-Type: text/plain');
echo $_SERVER['REQUEST_URI'];
?>
I was under the impression (perhaps just a random guess that I never tested) that you could use the # method to send a value to PHP, much like GET, but just that you would need to code the retrieval yourself (by splitting at '#').
No. With other URI schemes, the fragment identifier may actually be significant, but it's not with HTTP.
Powered by vBulletin® Version 4.2.2 Copyright © 2021 vBulletin Solutions, Inc. All rights reserved.