PHP URL variable passing inconsistancy

software development

#1

I’m having trouble with the exact same php file in two different sections of my website. When i pass variables in one, it doesn’t accept them. Usually I do something like ?variable=1, and then later on access it via something like if($variable > 0) and it will work fine. Now I made a subdomain, and I can’t get it to work.

Here are the sites with and example file thats exactly the same:
http://dvereb.com/var.php?variable=lol
http://osms.dvereb.com/var.php?variable=lol

var.php consists of:

<?php echo"\$variable: " . $variable; ?>

One site works, the other does not. Any ideas why?


#2

OOPS! Just noticed that its because of an upgraded version of PHP. Is there a way to turn those global variables back on or do I have to use an older version of PHP? Also, by allowing these variables, would it cause a huge security problem?

Also, it says using $_POST[‘variable’] should work, but it will not work for me. I tried in var2.php (http://osms.dvereb.com/var2.php?variable=lol):

<?php echo"\$_POST['variable']: " . $_POST['variable'] . " "; $variable=$_POST['variable']; echo"\$variable: " . $variable; ?>

Ideas?


#3

You must use $_GET[‘variable’] to access information contained with a URL querystring. You are asking about register_globals, which is an evil thing covered with oozing badness!

si-blog
Max discount on any plan with promocode SCJESSEYTOTAL


#4

Yeah I just figured it out right as you posted. ugh, annoying updates :D. Thanks for the help, though. I hate when this happens! As SOON as I post about it I figure it out. lol. The downside of teaching yourself php is not knowing about these things!

Thanks!


#5

An easy solution (or at least one of a few) is:

Create a php file with a name like “setmyvars.php”.

In this file you have:

<? $variable1 = $_GET["variable1"]; $variable2 = $_GET["variable1"]; $variable3 = $_GET["variable1"]; ?>
And at the top of any page referencing these variables, add the line:

<? require_once("setmyvars.php"); ?>
If setmyvars.php isn’t in the same folder, use the standard “…/” etc to finet he right dir, or use the full path, such as “/home/username/domain.com/includes/setmyvars.php”.

That’ll mean you just add new vars to the file whenever you add new ones.

In my opinion a much better way (and you’ll hear me say this a lot) is to rewrite your code explicitly using $_GET[""] for url variables, and $_POST[""] for form variables.

Cheers,
Karl

web design, development, pay per click marketing (PPC) & Search Engine Optimisation (SEO) by DigitalVibe


#6

Other evil ways include using $_REQUEST or extract($_GET). Or it might be quicker just to post your username and password online and let other people fiddle with it for you!

si-blog
Max discount on any plan with promocode SCJESSEYTOTAL


#7

$_REQUEST … euuuughhhhhh!

Hence me explicitly mentioning $_GET & $_POST.

I guess it would have been helpful for me to say “but under no circumstances should you EVER use $_REQUEST”, unless you’re one of those sorts that leaves you house unlocked cos “burglars will do less damage if they don’t have to kick the door down” :wink:

Cheers,
Karl

web design, development, pay per click marketing (PPC) & Search Engine Optimisation (SEO) by DigitalVibe