Forum Moderators: coopster

Message Too Old, No Replies

PHP Globals

         

echoindia756

11:02 am on Jul 5, 2008 (gmt 0)

10+ Year Member



Hi guys,

I'm trying to write this code so it's no dependant on register_globals being "on". I know to do this you have to make use of the PHP globals. I'm a bit stuck on these ones:

[codes]
$SelectPlanet = $_GET['cp'];
$RestorePlanet = $_GET['re'];
$file = end(explode('/',$arr[1]['file']));
$line = $arr[1]['line'];
$debug->add("<tr><th>Query $numqueries: </th><th>$query</th><th>$file($line)</th><th>$table</th><th>$fetch</th></tr>");
[/codes]

How do I make the above lines globals? i.e for example the first line, would it be something like "$SelectPlanet = ['cp]" ?

I appreciate any help or advice. Many thanks.

rob7591

12:35 pm on Jul 5, 2008 (gmt 0)

10+ Year Member



I don't get what you're trying to do.. It doesn't look like register_globals would need to be on for that code to work..

if you had mypage.php?cp=Neptune

$SelectedPlanet would be Neptune

echoindia756

12:44 pm on Jul 5, 2008 (gmt 0)

10+ Year Member



Basically I put the following code after the first PHP tag:

ini_set ("display_errors", "1");
error_reporting(E_ALL);

This showed up what line the error was on in each file, and i've fixed most of them except the above, I can't figure them out. So I guess those errors are nothing to do with register globabls. The erros i'm getting are the following:

By the way, I don't know what some errors are repeating themselves.

Undefined index: cp in SetSelectedPlanet.php on line 14

Notice: Undefined index: re in SetSelectedPlanet.php on line 15

Notice: Undefined offset: 1 in mysql.php on line 22

Notice: Undefined offset: 1 in mysql.php on line 23

Notice: Undefined offset: 1 in mysql.php on line 22

Notice: Undefined offset: 1 in mysql.php on line 23

Notice: Undefined offset: 1 in mysql.php on line 22

Notice: Undefined offset: 1 mysql.php on line 23

Notice: Undefined offset: 1 mysql.php on line 22

Notice: Undefined offset: 1 mysql.php on line 23

Any help appreciated, thanks!

echoindia756

12:48 pm on Jul 5, 2008 (gmt 0)

10+ Year Member



Ok, I just turned register_globals off - the form doesn't work. So I guess i'll have to fix those lines above before it'll work.

echoindia756

12:51 pm on Jul 5, 2008 (gmt 0)

10+ Year Member



Update: The above are the wrong errors. Your right, they're nothing to do with register_globals. I pressed the submit button and it displayed the correct errors.

Now, how would I chance this line so that it's not dependant on register_globals being on?:

$gebruik=htmlspecialchars(addslashes($_POST['gebruik']));

I'm getting confused with all the brackets and stuff. Thanks!

echoindia756

12:54 pm on Jul 5, 2008 (gmt 0)

10+ Year Member



Would this be right: $gebruik = $_POST['gebruik'];

henry0

2:29 pm on Jul 5, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yes,
anyway just try it and echo its value

echoindia756

3:23 pm on Jul 5, 2008 (gmt 0)

10+ Year Member



Ok, that's not working. It's telling me that this line: $gebruik=$_POST['gebruik']; is an Undefined Index: gebruik

How do I fix that error? The form won't work until I do. Thanks!

cameraman

8:05 pm on Jul 5, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



$_GET and $_POST are superglobals, and that is what you're supposed to use instead of depending on register_globals. If register_globals is on, php will extract each of the superglobal arrays into variables, for example, before your script runs,
$_GET['cp'] and $_GET['re'] get turned into $cp and $re.

If $_POST['gebruik'] is undefined, then that means it's not on the form, that is, there's no form element with name="gebruik" - check the HTML form. Could it have been coming from the $_GET via the form's action attribute? (<form action="page.php?gebruik=7">)

henry0

10:29 pm on Jul 5, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You should post relevant sections of your form and landing page, so we may try figuring what's wrong.

eelixduppy

3:20 am on Jul 7, 2008 (gmt 0)



Also note that you should be checking for the variable's existance before you do anything with it. isset [php.net]() or empty [php.net]() are both good for something like this:

if(isset($_POST['gebruik']))
$gebruik = htmlspecialchars(addslashes($_POST['gebruik']));

... this will eliminate your undefined index errors.