Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > ‘Pageless’ URLs with question marks

‘Pageless’ URLs with question marks
Thread Tools
Oisín
Moderator Emeritus
Join Date: Mar 2004
Location: Copenhagen
Status: Offline
Reply With Quote
Aug 31, 2006, 06:02 AM
 
I see, now and again, sites that have navigation built up around URLs such as http://site.com/?users.

How are these URLs created and called from a script? Are they put through as regular POST values, and then called via GET? How can they be, when there seems to be no actual values set in them?

Just wondering... tried to do a search, but I couldn’t figure out any search words that made sense and didn’t just find pages about how to send POST values to individual pages (as in index.php?user=XYZ), which is not quite what I was looking for.
     
SirCastor
Professional Poster
Join Date: Jan 2001
Location: Salt Lake City, UT USA
Status: Offline
Reply With Quote
Aug 31, 2006, 01:38 PM
 
You can actually hardcode a GET value simply by writing out the URL with the appropriate variables.
You can write http://www.wherever.com/page?name=anon&thing=this

Depending on your scripting language, you can access those variables. In PHP it looks like:

$name = $_GET['name']
$thing = $_GET['thing']

There's no POST or GET form involved unless you need input from the user. You could assemble your own links dependent on other data. For instance, I could write a script that did this:

if(morning) {
$time = "morning";
}else{
$time = "afternoon";
}
print "http://www.somewhere.com/?time=?".$time.";


and then the interpretive script is something like:

if($_GET['morning']){
print "Good morning";
}elseif($_GET['afternoon']){
print "Good Afternoon";
}else{
print "I don't know what time it is";
}
That's a limited example, but hopefully you get the idea, If the variable isn't set in the URL, it has no value, and won't make a difference.
( Last edited by SirCastor; Aug 31, 2006 at 01:48 PM. )
2008 iMac 3.06 Ghz, 2GB Memory, GeForce 8800, 500GB HD, SuperDrive
8gb iPhone on Tmobile
     
Oisín  (op)
Moderator Emeritus
Join Date: Mar 2004
Location: Copenhagen
Status: Offline
Reply With Quote
Aug 31, 2006, 05:00 PM
 
Sorry, I think I explained myself badly –

I wasn’t actually looking for a way to hard-code GET values into URLs; I do know how to do that, and how that works (sorry for making you type out all that stuff to no use).

What I was talking about is the fact that sometimes, a similar URL is seen, but it doesn’t have any page name, nor any variable names in the GET part of the URL—only GET variable values seem to appear.

In other words, instead of a URL appearing (as would be normal) as

http://website.com/index.php?section=support

– it will instead appear simply as

http://website.com/?support

– with ne’er a mention of either index.php or section=.

That’s the structure I don’t get.

Of course, linking to such a page is no problem—just link directly to that URL, exactly as I wrote it out up there. What I’m having trouble figuring out is how it would go about being called later on.

I can’t really do a $part = $_GET['section'], because the ‘section’ bit is missing! Can I just say if ($_GET[]) { $section = $_GET[]; }, with no further ado? And what would that even make $section into?
     
SirCastor
Professional Poster
Join Date: Jan 2001
Location: Salt Lake City, UT USA
Status: Offline
Reply With Quote
Aug 31, 2006, 05:07 PM
 
$_GET is a SuperGlobal array, so if you tell something to be equal to $_GET, you're going to get the whole array.

I've never run into one of these before, do you have an example?
2008 iMac 3.06 Ghz, 2GB Memory, GeForce 8800, 500GB HD, SuperDrive
8gb iPhone on Tmobile
     
Oisín  (op)
Moderator Emeritus
Join Date: Mar 2004
Location: Copenhagen
Status: Offline
Reply With Quote
Aug 31, 2006, 05:16 PM
 
I had one earlier today, when I made the thread, but now when I need it, I’ve naturally gone and forgotten what it was. I’ll see if I can find it again somehow...

I did figure that setting something to be equal to $_GET would give you the whole array—but if the URL consists of only a domain name and a ?support, for instance, what will be left in the array? Is there a whole lot of other dribble-blather that goes in there as well?

Edit: Just tried testing by adding a simple echo $_GET; (both with $_GET and with $_GET[]), and it just turned out blank in both cases; so I guess if nothing is specified either in the URL or via a form, then $_GET just consists of absolutely nothing.
     
SirCastor
Professional Poster
Join Date: Jan 2001
Location: Salt Lake City, UT USA
Status: Offline
Reply With Quote
Aug 31, 2006, 06:11 PM
 
A form pushes the data to the URL if the method is get to a get. typically it prints something like

Array()

if you don't tell it to specify, but it varies depending on your situation.
2008 iMac 3.06 Ghz, 2GB Memory, GeForce 8800, 500GB HD, SuperDrive
8gb iPhone on Tmobile
     
registered_user
Dedicated MacNNer
Join Date: Nov 2001
Location: Are Eye
Status: Offline
Reply With Quote
Aug 31, 2006, 06:53 PM
 
It's often done with multiviews.

If I have multiviews enabled, Apache routes /script/x/y/3 to /script.php.

script.php parses the url, and uses "x", "y", and "3" as arguments about what the script is meant to do. You can easily explode a url into a convenient array with this in php: $url_bits = explode("/", $_SERVER['REQUEST_URI']); and then you have each variable in the array. You can search for the array value that matches the script name, and then easily get the variables that come later in the array.

On my site, I do stuff like /articles/web_development/page2 which tells articles.php to grab page2 of articles tagged web_development.

Similarly, you can use mod_rewrite if you have a given url schema. For example, if your existing url is index.php?user=XYZ you can use mod_rewrite to do some magic. You can use regular expressions in your .htaccess file let Apache know that you want index/user/XYZ to map to index.php?user=XYZ, but it's all done behind the scenes and invisibly to the user.

that particular rule might look like this:

RewriteRule ^index/user/(.*) /index.php?user=$1 [nc]
     
Oisín  (op)
Moderator Emeritus
Join Date: Mar 2004
Location: Copenhagen
Status: Offline
Reply With Quote
Sep 1, 2006, 05:43 AM
 
Thank you, registered_user! That makes perfect sense. Now if only I had access to either MultiViews or using mod_rewrite on my server, too...

Ah well—will just do it the old-fashioned way, then. Thanks again
     
SirCastor
Professional Poster
Join Date: Jan 2001
Location: Salt Lake City, UT USA
Status: Offline
Reply With Quote
Sep 1, 2006, 10:06 AM
 
You may be able to set it with a .htaccess file
2008 iMac 3.06 Ghz, 2GB Memory, GeForce 8800, 500GB HD, SuperDrive
8gb iPhone on Tmobile
     
Oisín  (op)
Moderator Emeritus
Join Date: Mar 2004
Location: Copenhagen
Status: Offline
Reply With Quote
Sep 1, 2006, 10:22 AM
 
Yeah, I don't have access to using those, either (I can make them, but they're apparently ignored by the server). 's what you get for going with cheap services, I guess.
     
skalie
Mac Elite
Join Date: Mar 2002
Location: Clogland
Status: Offline
Reply With Quote
Sep 1, 2006, 11:36 AM
 
Originally Posted by Oisín
Yeah, I don't have access to using those, either (I can make them, but they're apparently ignored by the server). 's what you get for going with cheap services, I guess.
It might be that the configuration of your ftp client isn't allowing you to see them ( .htaccess files )
     
Millennium
Clinically Insane
Join Date: Nov 1999
Status: Offline
Reply With Quote
Sep 1, 2006, 11:56 AM
 
Ah; I think I see what you're asking about. You're asking about a URL with no path (or rather, just a / for its path), and then everything else in its query string, right? Drupal, for example, does this by default.

It's actually just a default-page mechanism on the server side. It's the same mechanism that bounces people from / to /index.html on many static sites. It points at a different page, but that's all.
You are in Soviet Russia. It is dark. Grue is likely to be eaten by YOU!
     
Oisín  (op)
Moderator Emeritus
Join Date: Mar 2004
Location: Copenhagen
Status: Offline
Reply With Quote
Sep 2, 2006, 02:39 PM
 
Originally Posted by skalie
It might be that the configuration of your ftp client isn't allowing you to see them ( .htaccess files )
No, I can see them all right—but the rules I specify in them are not carried out in any way whatsoever. I tried using a .htaccess file to give a simple password protection to something I did once, but nope, no dice. Worked fine on my localhost, but not once I uploaded it, just did absolutely nothing.
     
skalie
Mac Elite
Join Date: Mar 2002
Location: Clogland
Status: Offline
Reply With Quote
Sep 2, 2006, 03:25 PM
 
.... not a windows server by any chance? ( sorry if that's too obvious a comment, but ..... ), or else, I guess, it's something to do with the way Apache has been configured on the server.

Irritating weirdness.
     
SirCastor
Professional Poster
Join Date: Jan 2001
Location: Salt Lake City, UT USA
Status: Offline
Reply With Quote
Sep 2, 2006, 05:08 PM
 
Who's your hosting provider?
2008 iMac 3.06 Ghz, 2GB Memory, GeForce 8800, 500GB HD, SuperDrive
8gb iPhone on Tmobile
     
registered_user
Dedicated MacNNer
Join Date: Nov 2001
Location: Are Eye
Status: Offline
Reply With Quote
Sep 2, 2006, 08:58 PM
 
it's still possible that multiviews are active. (though highly unlikely) a quick test is to see if /file loads up /file.ext
     
jay3ld
Senior User
Join Date: Jul 2004
Status: Offline
Reply With Quote
Sep 3, 2006, 03:03 AM
 
It is possible to use this

site.com/index.php/support/

then have the php file get the querystring and phrase it. verify it. get the page. output it.

No mod_rewrite needed...

I took this from SMF 1.1 rc3
Code:
// Replace 'index.php/a,b,c/d/e,f' with 'a=b,c&d=&e=f' and parse it into $_GET. parse_str(substr(preg_replace('/&(\w+)(?=&|$)/', '&$1=', strtr(preg_replace('~/([^,/]+),~', '/$1=', substr($request, strpos($request, basename($scripturl)) + strlen(basename($scripturl)))), '/', '&')), 1), $temp);
that section of code phrases the line correctly and sets it up to correctly output...
You shouldn't make fun of nerds... you'll be working for one some day.
     
Oisín  (op)
Moderator Emeritus
Join Date: Mar 2004
Location: Copenhagen
Status: Offline
Reply With Quote
Sep 3, 2006, 05:36 AM
 
Originally Posted by SirCastor
Who's your hosting provider?
Cliche Webhosting Denmark

—cheapest one in the country

MultiViews is actually on, it seems
That was unexpected!
     
registered_user
Dedicated MacNNer
Join Date: Nov 2001
Location: Are Eye
Status: Offline
Reply With Quote
Sep 3, 2006, 08:50 AM
 
Originally Posted by jay3ld
It is possible to use this

site.com/index.php/support/

then have the php file get the querystring and phrase it. verify it. get the page. output it.

No mod_rewrite needed...

I took this from SMF 1.1 rc3
Code:
// Replace 'index.php/a,b,c/d/e,f' with 'a=b,c&d=&e=f' and parse it into $_GET. parse_str(substr(preg_replace('/&(\w+)(?=&|$)/', '&$1=', strtr(preg_replace('~/([^,/]+),~', '/$1=', substr($request, strpos($request, basename($scripturl)) + strlen(basename($scripturl)))), '/', '&')), 1), $temp);
that section of code phrases the line correctly and sets it up to correctly output...
That code is really complicated. I can see why it is that way, but unless your using URIs that are overly complicated, it's not necessary. For more friendly /file/foo/bar/baz/blah URIs, I use this:

Code:
$arguments = explode($_SERVER['SCRIPT_NAME'], $_SERVER['REQUEST_URI']); $arguments = explode("/", substr(array_pop($arguments),1));
now, everything after the filename in url is in the $arguments array. for example:

/file/foo/bar
$arguments[0] = "foo" and $arguments[1] = "bar" and so on
     
genevish
Mac Enthusiast
Join Date: Jan 1999
Location: Marietta, GA, USA
Status: Offline
Reply With Quote
Sep 6, 2006, 06:14 PM
 
I think this has become a more complex topic than it needs to be. Every single webserver on the planet can do this, usually by default. The URL http://www.genevish.org/ does not contain a page to retrieve at all, only a domain and machine name. It references the root directory. The webserver (Apache in this case) is set up to look for a default page in the event that a specific page is not specified. The most common default page is "index.html" or "index.htm", but others can be specified in the configuration for the webserver.

As an example, go to:

http://www.genevish.org/macnn/

Note that you are seeing the directory listing. This is because there is no default page in this directory.

Now click on the root directory. You will be taken to another directory:

http://www.genevish.org/macnn/root/

...but this one has a default page (index.php in this case). It is presenting a simple line of text. Now add "?macnn" to the end of the URL to make it:

http://www.genevish.org/macnn/root/?macnn

The PHP page is detecting the existance of the GET variable "macnn" and presenting a different line of text.

This can be made more powerful by presenting entire webpages based on the GET variables:

http://www.genevish.org/macnn/root/?apple
Scott Genevish
scott AT genevish DOT org
     
registered_user
Dedicated MacNNer
Join Date: Nov 2001
Location: Are Eye
Status: Offline
Reply With Quote
Sep 6, 2006, 06:19 PM
 
that's all well and good, but it's totally different (and inferior) from MultiViews or mod_rewrite.
     
genevish
Mac Enthusiast
Join Date: Jan 1999
Location: Marietta, GA, USA
Status: Offline
Reply With Quote
Sep 7, 2006, 05:12 PM
 
Originally Posted by registered_user
that's all well and good, but it's totally different (and inferior) from MultiViews or mod_rewrite.
That may be, but it answers the original question. The original poster seemed to be asking how this could resolve a page when only a directory was given (answer: The webserver is set to retrieve a default page even though no page name was given), and how you can do something with a variable when no value is set for it (answer: The existence of the variable itself is enough, even with no value set).
Scott Genevish
scott AT genevish DOT org
     
   
Thread Tools
 
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Top
Privacy Policy
All times are GMT -4. The time now is 05:33 AM.
All contents of these forums © 1995-2017 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.8 © 2000-2017, Jelsoft Enterprises Ltd.,