Page 1 of 1

amending code for registered_globals = off

Posted: Sat Dec 06, 2003 2:21 pm
by malcolmboston
Hello Guys

bought a book earlier PHP for the World Wide Web (Visual QuickStart Guides) you can find it here http://www.amazon.co.uk/exec/obidos/ASI ... 46-5486229

Basically bought the book, which clearly states it supports the latest PHP (version 4+) unfortanately the book uses the old syntax for variables and such (basically the guys writes the whole book as if you had registered globals switched on, i dont!)

so all the tutorials ive been following dont work properly until iu switched it on, but i dont want it on, how do i make the variables work without it?

so that

Code: Select all

<?php
$username = "username" /* from form field */
$password = "password" /* from form field */
?>
now the syntax probably isn't correct above but you should know what i mean anyway

Mal

Posted: Sat Dec 06, 2003 2:43 pm
by uberpolak
Ideally you'd go through all your code and change it to use the superglobal arrays. For example, any vars passed through the URL would be accessed as $_GET['var'] instead of $var, and any vars passed through a POST method form would be $_POST['var'] instead of $var, and so on.
This can be quite time-consuming though, and if you want your site to work while you're in the process of updating your code to match current methods, you could include these lines at the top of all your PHP files.

Code: Select all

<?php
extract($_POST);
extract($_GET);
?>
This is a very cheap quickfix, and creates myriad security issues, so it's best to go through and get your code up to snuff sooner than later.

Posted: Sat Dec 06, 2003 2:45 pm
by malcolmboston
isnt it completely ridiculious that a book specifically stating that it was written for PHP 4 should do this!

Posted: Sat Dec 06, 2003 2:46 pm
by uberpolak
Not necessarily, earlier versions of PHP4 had register_globals on by default, the book may have been published when this was standard.

Posted: Sat Dec 06, 2003 2:50 pm
by malcolmboston
well tbh i think its lame, i bought the book so i could start learning the advanced things like arrays and such and now even the simple tutorials that go a little more in-depth than ive been before wont work

any chance someone could give me a snippet of example code so i know what to change?

Thanks

Posted: Sat Dec 06, 2003 2:54 pm
by uberpolak
I'll use what you gave above as an example.

You had:

Code: Select all

<?php
$username = "username";
$password = "password";
?>
You said those came from a form. If the method of that form was POST (check the HTML), do this.

Code: Select all

<?php
echo $_POST['username']; //to use the username as it was entered, instead of $username
echo $_POST['password']; //instead of $password
?>
If the method of the form was GET, do the same thing, except change $_POST to $_GET.

Posted: Sat Dec 06, 2003 2:57 pm
by malcolmboston
hmmm
ok gonna sound really n00bish now

but i tried that earlier actually, first thing i thought of, always worked for me before with advanced passing of variables, weirdly enough it never worked for me

maybe its just me, syntax was probably <span style='color:blue' title='I&#39;m naughty, are you naughty?'>smurf</span> somewhere

Thanks alot guys

Posted: Sat Dec 06, 2003 5:34 pm
by m3mn0n
Well, the book is a very good starter guide to PHP, but for an older version. :?

After you master all the concepts within it, adjusting to the newer ways of coding, such as superglobals, will be a snap.

I used it to learn and I'm thankful that I did. Those Visual Quick Starts books are very good for newbies in any subject.

Posted: Sun Dec 07, 2003 7:45 am
by JAM
@malcolmboston,
I agree that it's rather annoying that you buy a 'new' book, but it isn't up-to-date in it's content. Thats just a negative side effect of using open source applications, something that we all need to live with.

Iv'e seen books that has a papercover telling me that 'Since this book was printed, more functions as X & Y has been added. Referering to page http://... for more info' that gave me a couple of warnings.

Happy coding tho.

Posted: Sun Dec 07, 2003 9:21 am
by Gen-ik
Unless I'm creating PHP for my own server which I know has register_globals on and also has safe_mode turned off, I always write the code so that it will work with with register_globals turned off and safe_mode turned on..... that way problems very rarely crop up.

Can't think of a 'quick fix' for you though (sorry)...... sounds like you will have to go through all your code and change it manually.

Posted: Sun Dec 07, 2003 11:19 am
by malcolmboston
yeah sami, the reason i bought it was that i wanted to learn it for myself and stop asking other people

im sure ill overcome the problems


Thanks anyway guys