Web Hosting Talk







View Full Version : Passing Variable From URL - PHP


FTN BLZZRD
05-15-2007, 11:44 PM
Hey guys, I'm writing a script in PHP, obviously. Anyway, I've been searching the web on different ways to pass variables, and saw that it is possible to do it from the URL. This would be perfect for my script. I'm a bit of a PHP noob, but detailed technical explanations aren't necessary.

The script is located at domain.com/script.php. I'm looking to do something where I can point people to domain.com/variableData and that the 'variableData' automatically populates as the data for a variable in domain.com/script.php.

Is this possible? Any help would be appreciated.

ak7861
05-16-2007, 12:21 AM
Use $_GET[]. :)

Hastings
05-16-2007, 12:32 AM
http://yoursite.com/example.php?something=whatever
would translate to the variable
$_GET['something'];
and would store the data "whatever" (without quotes).

This is probably the easiest way, but it's up to you. Check out w3schools.com

Rich
05-16-2007, 12:36 AM
http://yoursite.com/example.php?something=whatever
would translate to the variable
$_GET['something'];
and would store the data "whatever" (without quotes).

This is probably the easiest way, but it's up to you. Check out w3schools.com

Just an extension to this.. you said you wanted to point them to domain.com/variableData

Like..

favorite-foods.com/Pizza

And capture Pizza?

Notice that doesn't meet your exact requirements for the URL you want to be able to send them to.

So what I'd do is have your script capture it like they are suggesting..
www.favorite-foods.com/index.php?food=Pizza (http://www.favorite-foods.com/index.php?food=Pizza)
THEN.. use mod_rewrite to rewrite the URL so that it works just as they are suggesting but can be written www.favorite-foods.com/Pizza (http://www.favorite-foods.com/Pizza)

nick_phost
05-16-2007, 03:26 AM
try this,

if ($_SERVER['PATH_INFO']) {
$var = ereg_replace("/", "", $_SERVER['PATH_INFO']);
}

this will allow you to pass vars like this
domain.com/something

or domain.com/myfile.php/something

nick_phost
05-16-2007, 03:28 AM
one more thing you dont need "if".

FTN BLZZRD
05-16-2007, 08:28 AM
try this,

if ($_SERVER['PATH_INFO']) {
$var = ereg_replace("/", "", $_SERVER['PATH_INFO']);
}
this will allow you to pass vars like this
domain.com/something

or domain.com/myfile.php/something

Haha, thanks alot guys. That worked. I had it the way Slip7 showed with mod_rewrite rules, and that worked too.