• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1405
  • Last Modified:

basename($_SERVER["PHP_SELF"]) to include the quesry string

Hello,

I am using this to get a file name..

$page = basename($_SERVER["PHP_SELF"]);
echo $page;

and it will return news.php which is great...

I need it to also include the query string like so...

news.php?id=1

Is there any way to do this? I cant find anything online...


0
jblayney
Asked:
jblayney
  • 9
  • 8
1 Solution
 
jblayneyAuthor Commented:
I did find this little snippet online...

$path_with_query="http://www.ex.com/getdat.php?dep=n/a&title=boss";
$path=explode("?",$path_with_query);
$filename=basename($path[0]);
$query=$path[1];

but it requires me writing the whole address which i tried to do like so...

$path_with_query=$_SERVER["PHP_SELF"];

but with no luck...

i think they can be combined i just cant figure out how
0
 
Ray PaseurCommented:
Look for $_SERVER["REQUEST_URI"]);

Check the code snippet for lots of good information
<?php phpinfo(); ?>

Open in new window

0
 
Ray PaseurCommented:
The reason I suggest that you check phpinfo() is that the $_SERVER vars are usually (but not always) populated with consistent data.  In most installations you can rely on a fairly standard set of elements in this array.  But since it can vary, it is worth checking to see if the REQUEST_URI is populated correctly.

I would not use the basename() directive.  Here is why.  Let's say you have your script located at

www.domain.com/freddie/index.php?q=ABC

the basename() will only get you index.php?q=ABC and you will lose the directory /freddie/
http://us2.php.net/manual/en/function.basename.php

Best regards, ~Ray
0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
jblayneyAuthor Commented:
Hello Ray,

i actually only want it that way, i have multiple folders with identical file structure (different language for each folder)

so I want exactly....

page.php?id=1

i don't want the folder structure....

i did check REQUEST_URI and got this...

 /folder_name/info.php

i'm not sure what to do with i though...
0
 
Ray PaseurCommented:
OK, I understand.  Couple of things.. Who is your hosting company?  And please print out the $_SERVER part of phpinfo() and post it here.  But when you do it this time, do it like this...

 /folder_name/info.php?q=HELLO+WORLD

Standing by...
0
 
jblayneyAuthor Commented:
Hello,

i cant find $_SERVER

but i did find 34 matches for

_SERVER

which of those do you need?

Also, this is a testing server I am on now, I dont have access to the live server yet, so it will most likely change...
0
 
Ray PaseurCommented:
_SERVER - show them all here, thanks...
0
 
jblayneyAuthor Commented:
Hello,

Maybe this is a newb question, but some of that data seems sensitive, should i really be posting that on a public website?
0
 
Ray PaseurCommented:
Nothing in $_SERVER that will hurt you in a properly configured system.  But if you want, use my public profile email address and send me a URL link to your PHPINFO.  I will pull out the "good stuff" and make sure nothing bad gets posted here.
0
 
jblayneyAuthor Commented:
ok, i checked your profile, no email link
0
 
Ray PaseurCommented:
To quote from the public profile:

I welcome consulting assignments.  Please email me via Ray.Paseur [at] GMail or phone me at 703.346.0600.  

See if you can find it in there!
0
 
Ray PaseurCommented:
Hello?

 /folder_name/info.php?q=HELLO+WORLD

Standing by...
0
 
jblayneyAuthor Commented:
Hello ray,

I sent an email about 15 minutes ago from Darkstar Media
0
 
Ray PaseurCommented:
Got the email.

Did you use the URL with the GET string ?q=HELLO+WORLD ?

Thanks, ~Ray
0
 
jblayneyAuthor Commented:
Hi Ray,ok,

thanks for drawing my attention to this...

what i did was this...

$page = basename($_SERVER["PHP_SELF"]);
$query_string =  $_SERVER["QUERY_STRING"];
$final_path = $page . "?" . $query_string;
echo $final_path;

and it works great

0
 
jblayneyAuthor Commented:
i just tweaked it...

$page = basename($_SERVER["PHP_SELF"]);
$query_string =  $_SERVER["QUERY_STRING"];

if(!$query_string) {
$final_path =       $page;
} else {
$final_path = $page . "?" . $query_string;
}

echo $final_path;


Is this what you were driving at/
0
 
Ray PaseurCommented:
I think you are firm ground there.  The reason I asked about this:

Did you use the URL with the GET string ?q=HELLO+WORLD ?

Is that most PHP installations populate $_SERVER["REQUEST_URI"] with PHP_SELF + QUERY STRING.  It saves the conditional work since you only need to retrieve one variable.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

  • 9
  • 8
Tackle projects and never again get stuck behind a technical roadblock.
Join Now