Page 1 of 1

URL variables

Posted: Mon Apr 19, 2004 4:01 pm
by xt07t
I have been using Cold Fusion for about 3 years. I am moving to PHP because work demands it. Because I am new to this language, I was wondering how to do the following...

I have a variable in a url that I would like to assign to a variable in the page.
http://www.somesite.com/index.php?main=np

In the code I would like the variable mainID set to the value found in the url. How can I do this?

Thanks!
xtort

Posted: Mon Apr 19, 2004 4:07 pm
by magicrobotmonkey

Code: Select all

<?php
$mainID = $_GET['main'];
?>

Posted: Mon Apr 19, 2004 7:36 pm
by John Cartwright
Let me clarify this a bit more:

$_GET['varname']; is used to pull information out from a URL.

Code: Select all

<?php
//URL: http://www.domain.com/?varname=foobar

echo $_GET['varname'];

//outputs foobar
?>
On the other hand if this information is coming from a form that means you are using the $_POST method. The post method always grabs information that is invisible from a form. For example lets say I inputted a textarea with the name varname and I inputted foobar as it's value. Instead of using $_GET I would use $_POST.

Code: Select all

<?php

echo $_POST['varname'];

//outputs foobar
?>

Posted: Mon Apr 19, 2004 7:50 pm
by Chris Corbyn
Phenom, think you've just about explained all there is extracting variables from the url.

One quick point to note

Code: Select all

$varname = $_REQUEST['varname'];
works regardless of whether the variables were sent via POST or GET

Posted: Mon Apr 19, 2004 8:23 pm
by John Cartwright
d11wtq wrote:Phenom, think you've just about explained all there is extracting variables from the url.

One quick point to note

Code: Select all

$varname = $_REQUEST['varname'];
works regardless of whether the variables were sent via POST or GET
We'll I learn something new everyday.. I've noticed that before but never bothered with it.

Posted: Mon Apr 19, 2004 9:14 pm
by tim
I would use phen method.

if you know your getting the var from the URL, use $_GET

Helps you to see where your getting the var(s) if you ever need to come back to the script in the future and maybe forgot some of it :)

Posted: Tue Apr 20, 2004 3:04 am
by JayBird
Phenom wrote:On the other hand if this information is coming from a form that means you are using the $_POST method. The post method always grabs information that is invisible from a form. For example lets say I inputted a textarea with the name varname and I inputted foobar as it's value. Instead of using $_GET I would use $_POST.
Slightly miss leading. You don't always use $_POST when submitting information through a form, it depends on what your form method is.

...also better to use $_GET or $_POST instead of $_REQUEST to reduce the security risks.

Mark

Posted: Tue Apr 20, 2004 3:22 am
by WaldoMonster
$_REQUEST will besides $_GET and $_POST also return $_COOKIE
If you don't know if it is a POST or GET submission you can use this simple function

Code: Select all

function GetPost($key)
{
if (isset($_GET[$key])) $value = $_GET[$key];
elseif (isset($_POST[$key])) $value = $_POST[$key];
return $value;
}
?>

Works great!

Posted: Tue Apr 20, 2004 7:39 am
by xt07t
Thank you Phenom and everyone elese who replied. That makes it really simple to migrate now!

xtort :)