URL variables
Moderator: General Moderators
URL variables
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
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
-
magicrobotmonkey
- Forum Regular
- Posts: 888
- Joined: Sun Mar 21, 2004 1:09 pm
- Location: Cambridge, MA
Code: Select all
<?php
$mainID = $_GET['main'];
?>- John Cartwright
- Site Admin
- Posts: 11470
- Joined: Tue Dec 23, 2003 2:10 am
- Location: Toronto
- Contact:
Let me clarify this a bit more:
$_GET['varname']; is used to pull information out from a URL.
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.
$_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
?>Code: Select all
<?php
echo $_POST['varname'];
//outputs foobar
?>- Chris Corbyn
- Breakbeat Nuttzer
- Posts: 13098
- Joined: Wed Mar 24, 2004 7:57 am
- Location: Melbourne, Australia
Phenom, think you've just about explained all there is extracting variables from the url.
One quick point to note
works regardless of whether the variables were sent via POST or GET
One quick point to note
Code: Select all
$varname = $_REQUEST['varname'];- John Cartwright
- Site Admin
- Posts: 11470
- Joined: Tue Dec 23, 2003 2:10 am
- Location: Toronto
- Contact:
We'll I learn something new everyday.. I've noticed that before but never bothered with it.d11wtq wrote:Phenom, think you've just about explained all there is extracting variables from the url.
One quick point to noteworks regardless of whether the variables were sent via POST or GETCode: Select all
$varname = $_REQUEST['varname'];
Slightly miss leading. You don't always use $_POST when submitting information through a form, it depends on what your form method is.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.
...also better to use $_GET or $_POST instead of $_REQUEST to reduce the security risks.
Mark
- WaldoMonster
- Forum Contributor
- Posts: 225
- Joined: Mon Apr 19, 2004 6:19 pm
- Contact:
$_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
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!
Thank you Phenom and everyone elese who replied. That makes it really simple to migrate now!
xtort
xtort