?
Solved

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

Posted on 2009-12-22
17
Medium Priority
?
1,330 Views
Last Modified: 2013-12-12
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
Comment
Question by:jblayney
  • 9
  • 8
17 Comments
 
LVL 1

Author Comment

by:jblayney
ID: 26109549
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
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 26109753
Look for $_SERVER["REQUEST_URI"]);

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

Open in new window

0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 26109768
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 1

Author Comment

by:jblayney
ID: 26110022
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
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 26112191
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
 
LVL 1

Author Comment

by:jblayney
ID: 26112958
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
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 26112981
_SERVER - show them all here, thanks...
0
 
LVL 1

Author Comment

by:jblayney
ID: 26113124
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
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 26113212
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
 
LVL 1

Author Comment

by:jblayney
ID: 26113266
ok, i checked your profile, no email link
0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 26113275
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
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 26113698
Hello?

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

Standing by...
0
 
LVL 1

Author Comment

by:jblayney
ID: 26113756
Hello ray,

I sent an email about 15 minutes ago from Darkstar Media
0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 26113786
Got the email.

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

Thanks, ~Ray
0
 
LVL 1

Author Comment

by:jblayney
ID: 26114262
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
 
LVL 1

Author Comment

by:jblayney
ID: 26114272
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
 
LVL 111

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 26114425
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

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this. Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it i…
This article discusses four methods for overlaying images in a container on a web page
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses
Course of the Month16 days, 12 hours left to enroll

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question