/    Sign up×
Community /Pin to ProfileBookmark

input sanitation

When I began codeing in PHP, I read that get and post variables were automatically available (Now I know they were talking about register globals). Many of my old form-handlers rely on register globals without much in the way of varification. I want to clean them up a bit without re-writing them. Would this code be a good start?

[code=php]foreach($_POST as $key=>$value){
$$key=stripslashes($value);
$$key=htmlentities($$key);
}[/code]

of course I could do the same for $_GET

to post a comment
PHP

3 Comments(s)

Copy linkTweet thisAlerts:
@artemisJan 21.2009 — Have you actually even read that code?
Copy linkTweet thisAlerts:
@TecBratauthorJan 22.2009 — Have you actually even read that code?[/QUOTE]

I wrote it, but haven't implemented it anywhere.

Do you have something a little more constructive to say? If it's horrible code, explain why you think so. You have to understand that my organized training in computer programming was BASIC and Pascal in middle school and highschool, 17 years ago. Note what I said earlier that I am just now beginning to look for ways to clean up some old, sloppy, code.

A sarcastic remark doesn't help anyone.
Copy linkTweet thisAlerts:
@NogDogJan 22.2009 — Some things to consider.

stripslashes() should only be used if magic_quotes_gpc is enabled (and you therefore need to undo its damage). You can use get_magic_quotes_gpc() to test if it is turned on.

htmlentities() is for escaping output, not filtering input. Applying it universally to all inputs could potentially cause more problems than it might fix.

You might want to take a look at the [url=http://www.php.net/import_request_variables]import_request_variables[/url]() function as a simple, built-in way to emulate register_globals.

Of course, the best solution would be to update the code to [i]not[/i] need register_globals type variables and use the proper super-global arrays instead. ? Both register_globals and magic_quotes_gpc are deprecated as of PHP5.3, and will be completely removed in PHP6.0, so it's really time for all of us PHP developers to update our code.
×

Success!

Help @TecBrat spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.20,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...