Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

SERVER REQUEST_URI 2

Status
Not open for further replies.

pastorandy

IS-IT--Management
Nov 2, 2006
84
GB
I am doing this on a small intranet
Code:
$current_page = $_SERVER['REQUEST_URI'];

This brings back something like: /dir/page.php

But can also bring back: /dir/order.php?order_id=3456

Which is fine but how can I limit the url string to just the file name without the directory and without any variables passed in the url string?
 
You'll need to manipulate the string, something like:


Code:
$mystr="/dir/order.php?order_id=3456";

if(strpos($mystr,"/")!==false){

$parts=explode("/",$mystr);
$urlA=$parts[count($parts)-1];
}
else{
$urlA=$mystr;
}
if(strpos($urlA,"?")){
$urlB=explode("?",$urlA);

$filename=$urlB[0];
}

else{

$filename=$urlA;
}
echo $filename;

This quite dirty, but gets the job done. You could stick this in a function to tidy it up a bit.

----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Behind the Web, Tips and Tricks for Web Development.
 
Thanks. I'll give this a try tomorrow and post back.
 
you can also try the built in functions from php

Code:
$pageName = pathinfo(parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH) , PATHINFO_BASENAME);
 
Apparently there's a lot of ways to accomplish this:
Code:
$pageName = basename(__FILE__);
__FILE__ is a constant that includes the path to a filename and filename and basename is a function that rips out the path.

[small]Do something about world cancer today: Comprehensive cancer control information at PACT[/small]
 
Afaik the __FILE__ constant reports the name of the script actively being executed at that time. This may well not be the same as the uri that was requested ( for example in despatch applications or in sites that make use of mod rewrite )
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top