URL variables

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
xt07t
Forum Newbie
Posts: 19
Joined: Mon Apr 19, 2004 4:01 pm

URL variables

Post 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
magicrobotmonkey
Forum Regular
Posts: 888
Joined: Sun Mar 21, 2004 1:09 pm
Location: Cambridge, MA

Post by magicrobotmonkey »

Code: Select all

<?php
$mainID = $_GET['main'];
?>
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post 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
?>
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post 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.
User avatar
tim
DevNet Resident
Posts: 1165
Joined: Thu Feb 12, 2004 7:19 pm
Location: ohio

Post 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 :)
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post 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
User avatar
WaldoMonster
Forum Contributor
Posts: 225
Joined: Mon Apr 19, 2004 6:19 pm
Contact:

Post 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;
}
?>
xt07t
Forum Newbie
Posts: 19
Joined: Mon Apr 19, 2004 4:01 pm

Works great!

Post by xt07t »

Thank you Phenom and everyone elese who replied. That makes it really simple to migrate now!

xtort :)
Post Reply