amending code for registered_globals = off

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
malcolmboston
DevNet Resident
Posts: 1826
Joined: Tue Nov 18, 2003 1:09 pm
Location: Middlesbrough, UK

amending code for registered_globals = off

Post 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
User avatar
uberpolak
Forum Contributor
Posts: 261
Joined: Thu Jan 02, 2003 10:37 am
Location: Next to the bar

Post 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.
malcolmboston
DevNet Resident
Posts: 1826
Joined: Tue Nov 18, 2003 1:09 pm
Location: Middlesbrough, UK

Post by malcolmboston »

isnt it completely ridiculious that a book specifically stating that it was written for PHP 4 should do this!
User avatar
uberpolak
Forum Contributor
Posts: 261
Joined: Thu Jan 02, 2003 10:37 am
Location: Next to the bar

Post by uberpolak »

Not necessarily, earlier versions of PHP4 had register_globals on by default, the book may have been published when this was standard.
malcolmboston
DevNet Resident
Posts: 1826
Joined: Tue Nov 18, 2003 1:09 pm
Location: Middlesbrough, UK

Post 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
User avatar
uberpolak
Forum Contributor
Posts: 261
Joined: Thu Jan 02, 2003 10:37 am
Location: Next to the bar

Post 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.
malcolmboston
DevNet Resident
Posts: 1826
Joined: Tue Nov 18, 2003 1:09 pm
Location: Middlesbrough, UK

Post 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
User avatar
m3mn0n
PHP Evangelist
Posts: 3548
Joined: Tue Aug 13, 2002 3:35 pm
Location: Calgary, Canada

Post 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.
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post 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.
Gen-ik
DevNet Resident
Posts: 1059
Joined: Mon Aug 12, 2002 7:08 pm
Location: London. UK.

Post 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.
malcolmboston
DevNet Resident
Posts: 1826
Joined: Tue Nov 18, 2003 1:09 pm
Location: Middlesbrough, UK

Post 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
Post Reply