Go Back  HTML Forums - Free Webmaster Forums and Help Forums > WEBSITE DEVELOPMENT > All Around Tutorials > Serverside Scripting Tutorials
User Name:
Password:
 

Reply
Thread Tools   Display Modes
  View First Unread
 
Old 02-08-2008, 05:26 PM
  #1
Vege
♥♥♥
 
Vege's Avatar
 
Join Date: Sep 2004
Location: Finland
Posts: 2,500
iTrader: (0)
Vege will become famous soon enough
Basic magic quotes information

What are magic_quotes?
It's a setting you can change in php.ini to automatically escape GET, POST, and COOKIE arrays on page load and by escaping i mean
Quote:
" ' and NULL
characters are turned to
Quote:
\" \'
and soforth.
This automatically makes querys safe to insert in database.

Alltought this setting prevented attackers to injecting extra sql code to your sql querys with method called sql injection, escaping automation and it's debendancy on server settings caused frustration among developers and this feature will be removed in php6.

To check if you have magic_quotes on, check your phpinfo and search (CTRL+F) for magic_quotes
Quote:
<?php
phpinfo();
?>
For example if i wanted to echo the submitted results to page before inserting them to database and magic_quotes would be on, I would have to do a stripslashes() call before displaying the data.
(which is wrong)
And if I'd later moved the same code to another server that has magic_quotes off my code would then remove any slashed i would had entered into the form as unwanted stripslashes would have occured.

Golden rule is that you should never have the need to use stipslashes when outputting data (after form submissin or when fetching data from database), never ever.



Note!
When you insert
Quote:
\"
into mysql database mysql itself will remove the \ and data inserted to database would be ".
Thats why we need to be sure that we prepare the data correctly so we don't get extra \ into database nor loose \ if someone wants to write those to his text.


Short rule how to make sure your query is safe for database.
There are too types of data to be inserted in general.
INT and VARCHAR wheere INT represents all interger types and varchar represents all textual variables.
Here is how you escape them (when using the magic_quotes code provided below).

First the query without validation.
Quote:
$credits = 5;
$firstname = "marge";
$sql = "SELECT * from persons where credits=$credits and firstname='$firstname'";
This query is fully functional, but will fail if credits is not a number or if I replace marge with
Quote:
marge's
as ' would close the sql query too soon and therefore make a nonvalid sql query.
Here is the previous query made safe.
Quote:
$credits = 5;
$firstname = "marge";
$sql = "SELECT * from persons where credits=".intval($credits)." and firstname='".mysql_real_escape_string($firstname)."'";








Here is a simple but effective script of code that will take care magic_quotes for you.
Principle is that you include this page in every php page you make and then proceed with normal form validation meaning you never need to worry about checking get_magic_quotes_gpc() again.

This will not add the validation, you still need to check if the string is a string and int is an int. You just dont need to check magic_quotes, they are allways off after this code is included.

Lets call it magicquotes.php

PHP Code:
<?php
if (get_magic_quotes_gpc()) {
        
$in = array(&$_GET, &$_POST, &$_COOKIE);
        while (list(
$k,$v) = each($in)) {
                foreach (
$v as $key => $val) {
                        if (!
is_array($val)) {
                                
$in[$k][$key] = stripslashes($val);
                                continue;
                        }
                        
$in[] =& $in[$k][$key];
                }
        }
        unset(
$in);
}
?>

Hmm, this "tutorial" is getting off from the tracks.

Comments on this post
bsxiong agrees: infomative
hammerstein_04 agrees: really helpful
__________________
I read the bible
Specially pg.681

Last edited by Vege : 04-27-2008 at 03:51 PM.
Vege is offline   Add to del.icio.us Add to del.icio.us    Can you digg it?Can you digg it? Reply With Quote
Reply


 
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
  
 
 
 



 
  POSTING RULES
 
 
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Thread Tools
Display Modes

Forum Jump

 

All times are GMT -5. The time now is 04:12 PM.

   

Mascot team created by Drawshop.com

Powered by vBulletin® Version 3.6.7
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.

Server Monitoring by ENIACmonitor 0.01
HTMLforums.com © Big Resources, Inc. Web Design by BoxedArt.com
vRewrite 1.5 beta SEOed URLs completed by Tech Help Forum and Chalo Na.