views:

70

answers:

6

I want to create a link like the following:

http://www.myurl.com/?IDHERE

What i want to be able to do is be able to goto the above link, and then pull whats after the ? (in this case IDHERE) and be able to use that information to perform a MySQL lookup and return a page.

Can anyone point me into the right direction? please know this is using PHP not ASP

+1  A: 

Create a default PHP file in that directory that will get loaded when no file name is specified (e.g. index.php). In your PHP script you can get the part after the question mark from the variable $_SERVER['QUERY_STRING'].

John Kugelman
so if i just use http://www.myurl.com/?IDHERE then it will basically just point to http://www.myurl.com/index.php?IDHERE ?
matt
This is not really a good idea, you could end up creating a lot of unneccisary directories.
Josiah
@matt - Yes, that's right. @Josiah - Unnecessary directories? What do you mean?
John Kugelman
+3  A: 

The issue here is not with your scripting language, but with your web server setup. I'll refer to these by their Apache names, but the features should be available in most web servers.

There are three features you might want to use:

1) content negotiation (mod_negotiation), which allows your web server to try a specified list of extensions in a specified order, for example: http://example.com/foo might be http://www.example.com/foo.html or http://example.com/foo.php

2) DirectoryIndex, which tells the web server that when a client asks for http://example.com it should look for a specified list of files in order, so it might server up http://example.com/index.html or http:/example.com/index.php

3) mod_rewrite, which allows you to basically rewrite the URL format received by the server. This allows you to do things like translate http://example.com/foo/bar/baz to http://example.com/foo/bar.php?page=baz

The rest is done by the backend script code as normal.

AllenJB
Nice one about the content negotiation. That's one that I didn't know of, could be really usefull for apps without pretty urls.
Josiah
A: 

Do the following in your site's main index.php:

list($id) = array_keys($_GET);
// right now, $id represents the ID you're looking for.
// Do whatever you want with it.
Felix
A: 

In the link, form or whatever - index.php?id=someid

In your index.php file:

$_GET['id'] = $id

Now you can use it:

e.g.

echo $id;

Since it's your default page, it will work without the extension.

Vafello
A: 
list($id) = array_keys($_GET);
// right now, $id represents the ID you're looking for.
// Do whatever you want with it.

this was exactly what i was looking for, though now i just need to create something to notify if nothing is there or not. Thank you all for your responses.

matt
A: 

Hi, I would solve it by using .htaccess file if posible.

create a .htaccess file in the main directory with the content:

RewriteEngine on RewriteRule cat/(.)/(.)/(.*)/$ /$1/$2.php?$3

that should translate example.com/foo/bar/baz to example.com/foo/bar.php?page=baz

Cintix