Page not reading variables in URL

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

tsg
Forum Contributor
Posts: 142
Joined: Sun Jan 12, 2003 9:22 pm
Location: SE, Alabama
Contact:

Page not reading variables in URL

Post by tsg »

I have someone setting up PHP on a server, but the pages do not seem to pick up the variable in the URL ... for example:

domain.com/page.php?variable=$test

Say I am pulling the $test information from the database, but the page doesn't read it.

Any ideas on what I can tell him to change to make it work in the set up? I am kinda at a loss on this.

Thanks,
Tim
qads
DevNet Resident
Posts: 1199
Joined: Tue Apr 23, 2002 10:02 am
Location: Brisbane

Post by qads »

Code: Select all

<?php
echo $_GET['variable'];
?>
if that does't work then try:

Code: Select all

<?php
echo $HTTP_GET_VARS['variable'];
?>
tsg
Forum Contributor
Posts: 142
Joined: Sun Jan 12, 2003 9:22 pm
Location: SE, Alabama
Contact:

Post by tsg »

Have it working now. It was the register_globals, they were off and I didn't know (I should have checked that first).

I had him turn it on ... if he hadn't, I would have had to recode to what you had posted. I guess I should start making that a habit anyways.

Thanks,
Tim
User avatar
mudkicker
Forum Contributor
Posts: 479
Joined: Wed Jul 09, 2003 6:11 pm
Location: Istanbul, TR
Contact:

Post by mudkicker »

some advice: you should make a habit to use it off and write your codes like qads told.
have a nice day! ;)
Cruzado_Mainfrm
Forum Contributor
Posts: 346
Joined: Sun Jun 15, 2003 11:22 pm
Location: Miami, FL

Post by Cruzado_Mainfrm »

dude, u have to start making a habit of using the $_GET[] variables, do u want someone to break in your site :D? then follow the rules dude

that register_globals should be turned off, and by the way, the people at PHP are going to deprecate it
tsg
Forum Contributor
Posts: 142
Joined: Sun Jan 12, 2003 9:22 pm
Location: SE, Alabama
Contact:

Post by tsg »

thanks for the input guys ... always something to learn ya know.

And for "following the rules" .. the rules in my PHP handbook I bought a couple of years ago does not talk about that way of doing it. However, I constantly find myself implimenting a better, more secure way of doing things. It's an ongoing process as we progress.

Tim (dude)
Sinnix
Forum Commoner
Posts: 43
Joined: Mon Apr 28, 2003 11:01 am
Location: Ottawa, ON Canada
Contact:

Post by Sinnix »

Yeah, no PHP book I own says anything about $_POST or $_GET (not even the one I bought two weeks ago), but the reality is that this has been in place for a while now and if you want your apps to keep working, you'ld best get in the habit now! ;)
qads
DevNet Resident
Posts: 1199
Joined: Tue Apr 23, 2002 10:02 am
Location: Brisbane

Post by qads »

old books (1 year or older) are about php 4.0 or php 4.01 at best, in those version register_globals are set to on, i myself was in shock when they turned them off in 4.2 lol..all my code screwed up and i was alittle mad lol..but little extra time saves headachs later :wink:
User avatar
mudkicker
Forum Contributor
Posts: 479
Joined: Wed Jul 09, 2003 6:11 pm
Location: Istanbul, TR
Contact:

Post by mudkicker »

i agree qads, this headaches are really annoying lol ;)
some more coding prevents the bad smell of your boss or client's mouth ;)

p.s.: qads i like your breasts lol! :) :) :)
qads
DevNet Resident
Posts: 1199
Joined: Tue Apr 23, 2002 10:02 am
Location: Brisbane

Post by qads »

lol..uh..thanks :lol:
tsg
Forum Contributor
Posts: 142
Joined: Sun Jan 12, 2003 9:22 pm
Location: SE, Alabama
Contact:

Post by tsg »

Just to follow up on a couple of things ...

When I use the $_GET['thing'] .. is that only for variables coming from the url? Would I use that for variables pulled from the database? (I don't think so, but want to be reassured)

When I use it doing a query on the database, there "WHERE this_item='$_GET['thing']'" throws an error, but this works: "WHERE this_item='$_GET[thing]'" (notice no ' ' inside the []) .. Is that cool?

I do use $_POST[] on forms ... I guess I should have picked this up at the same time ...

that's all for now ...
thanks,
Tim
Paddy
Forum Contributor
Posts: 244
Joined: Wed Jun 11, 2003 8:16 pm
Location: Hobart, Tas, Aussie
Contact:

Post by Paddy »

Not sure why your second attempt worked. Some guru might be able to answer that, but I think this is what you want.

Code: Select all

<?php
$thing = $_GET['thing'];
$result = mysql_query("select * from atable where this_item='".$thing."'") or exit();
?>
And yes, to get data from a form you use POST.
qads
DevNet Resident
Posts: 1199
Joined: Tue Apr 23, 2002 10:02 am
Location: Brisbane

Post by qads »

use this ONLY if $thing is a number :D

Code: Select all

<?php
$thing = (int)$_GET['thing']; 
$result = mysql_query("select * from atable where this_item='$thing'") or exit(); 
?>
Cruzado_Mainfrm
Forum Contributor
Posts: 346
Joined: Sun Jun 15, 2003 11:22 pm
Location: Miami, FL

Post by Cruzado_Mainfrm »

u can access variables in $_POST[] when using forms that have POST as method, ex: method="POST", if you set it to GET then variables are going to be passed in the url(not recommended) and can be accessed obviously with $_GET[]
tsg
Forum Contributor
Posts: 142
Joined: Sun Jan 12, 2003 9:22 pm
Location: SE, Alabama
Contact:

Post by tsg »

In this example:

WHERE this_item='$_GET[thing]'

$thing is a number in the url (?thing=9) and is working ... is that "correct"?
Post Reply