943,660 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Unsolved
  • Views: 2118
  • PHP RSS
Jan 5th, 2006
0

what does this code do?

Expand Post »
Am not too sure what this code does can someone help me please?
I know it starts a session but what all that xtra validation?

// start the session
session_start();

if (!get_magic_quotes_gpc()) {
if (isset($_POST)) {
foreach ($_POST as $key => $value) {
$_POST[$key] = trim(addslashes($value));
}
}
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
nike123 is offline Offline
20 posts
since Jun 2005
Jan 5th, 2006
0

Re: what does this code do?

It "escapes" certain characters. You need to look at the "addslashes" procedure to get a complete picture.
Team Colleague
Reputation Points: 227
Solved Threads: 37
Made Her Cry
tgreer is offline Offline
1,697 posts
since Dec 2004
Jan 6th, 2006
0

Re: what does this code do?

Quote originally posted by nike123 ...
Am not too sure what this code does can someone help me please?
I know it starts a session but what all that xtra validation?

// start the session
session_start();

if (!get_magic_quotes_gpc()) {
if (isset($_POST)) {
foreach ($_POST as $key => $value) {
$_POST[$key] = trim(addslashes($value));
}
}
The function first checks if magic quotes is enabled with get_magic_quotes_gpc(). magic quotes automatically escapes some special characters needed for user submitted data to be safe to save to a database.

If magic quotes is turned off, the function then escapes all special chars in the $_POST http vars.

the function trim just removes white space and new lines, \n, from the beginning and end of each string $_POST var.

So essentially the function emulates magic qoutes turned on on any php configuration.

I dont believe this is a good practise. First off, you dont need to escape every single $_POST, $_GET, $_COOKIE var, only those you will be saving to a sql db, and those you will use in an sql query.

Also each sql db needs its data escaped differently.
A better approach would be to use one of the mysql library functions such as mysql_real_escape_string.

To use this function you need to have an open mysql connection.

see: http://us3.php.net/mysql_real_escape_string
Moderator
Reputation Points: 457
Solved Threads: 101
Nearly a Posting Virtuoso
digital-ether is offline Offline
1,250 posts
since Sep 2005
Jan 18th, 2006
0

Re: what does this code do?

Thanks for the advice. I'll look into that.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
nike123 is offline Offline
20 posts
since Jun 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: second page error
Next Thread in PHP Forum Timeline: how can i display this in white ?????





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC