what does this code do?

Reply

Join Date: Jun 2005
Posts: 20
Reputation: nike123 is an unknown quantity at this point 
Solved Threads: 0
nike123 nike123 is offline Offline
Newbie Poster

what does this code do?

 
0
  #1
Jan 5th, 2006
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));
}
}
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 1,655
Reputation: tgreer is an unknown quantity at this point 
Solved Threads: 35
Team Colleague
tgreer tgreer is offline Offline
Made Her Cry

Re: what does this code do?

 
0
  #2
Jan 5th, 2006
It "escapes" certain characters. You need to look at the "addslashes" procedure to get a complete picture.
Reply With Quote Quick reply to this message  
Join Date: Sep 2005
Posts: 1,075
Reputation: digital-ether is just really nice digital-ether is just really nice digital-ether is just really nice digital-ether is just really nice 
Solved Threads: 66
Moderator
digital-ether's Avatar
digital-ether digital-ether is offline Offline
Veteran Poster

Re: what does this code do?

 
0
  #3
Jan 6th, 2006
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
www.fijiwebdesign.com - web design and development and fun
Cpanel Email - Let users Register email accounts on your website upon registration
Ajax Chat - Fully browser based chat!
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 20
Reputation: nike123 is an unknown quantity at this point 
Solved Threads: 0
nike123 nike123 is offline Offline
Newbie Poster

Re: what does this code do?

 
0
  #4
Jan 18th, 2006
Thanks for the advice. I'll look into that.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the PHP Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC