/    Sign up×
Community /Pin to ProfileBookmark

Retrieve Current Script’s URI

How could I retrieve the current script’s URI? The $_SERVER[‘SCRIPT_URI’] variable works on some, but not all servers. I need a method that will work universally.

Previously, I had been attaching a constant that held the site’s URI to $_SERVER[‘REQUEST_URI’]. This worked fine with sites that had URI’s like [url]http://example.com[/url], but with sites with URI’s like [url]http://example.com/mydir[/url], I had a duplicated “mydir” in the middle.

Any suggestions?

to post a comment
PHP

3 Comments(s)

Copy linkTweet thisAlerts:
@NogDogMar 18.2006 — How about:
[code=php]
$thisUri = "http://".$_SERVER['SERVER_NAME'].$_SERVER['PHP_SELF'];
[/code]
Copy linkTweet thisAlerts:
@patenaudematauthorMar 18.2006 — Hmm... not bad, and it is generally pretty functional, but...

There are a few things that I would prefer be in there. For example, the subdomain... if a user types in http://www.example.com, then clicks a link that has the return URI attached, it'll bring them back to http://example.com (of course, www is the only subdomain that would do that most likely... I'd still like that to be there though). I guess that's mostly stylistic, but I still like it.

I'd also prefer not to have to manually write the scheme. Just as a precaution, I'd like this to automatically grab the scheme in case, for example, someone is connecting using https://.

This is a blog engine, so I do have some user-defined information available. For example, I have a constant that contains the URI of the blog (siteURL). Previously, I was doing siteURL.$_SERVER['REQUEST_URI'], but with a blog at http://example.com/blog and a URI of http://example.com/blog/test, I was getting http://example.com/blog/blog/test. In an ideal world, I want to be able to grab what's in the address bar (I realize it's impossible... just fantasizing ?).

I'm thinking of just writing a function that looks for the intersection between siteURL and $_SERVER['REQUEST_URI']... or, actually, I could so something like this:

[code=php]$url = parse_url(siteURL);
$path = $url['path'];[/code]


And then just strip that from the front of $_SERVER['REQUEST_URI'] with a function. In fact, I think that's what I'll do.

Any better suggestions? ?
Copy linkTweet thisAlerts:
@patenaudematauthorMar 18.2006 — OK, I wrote a function that handles this with the logic above. Seems to work perfectly ?

[code=php]function scriptURI($siteURL) {
$url = parse_url($siteURL);
$path = str_replace("/", "\/", $url['path']);

preg_match("/".$path."(.*)/", $_SERVER['REQUEST_URI'],
$matches);

$uri = $siteURL . $matches[1];

return $uri;
}[/code]


Hope this can be of some use to someone else!
×

Success!

Help @patenaudemat spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.16,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...