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, 06:26 PM
  #1
Vege
Super Deity (Level 18)
 
Join Date: Sep 2004
Location: Finland
Posts: 3,390
iTrader: (0)
Vege is just really niceVege is just really niceVege is just really niceVege is just really nice
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 query safe to insert in database.

Although this setting prevented attackers to injecting extra SQL code to your query's with method called sql injection, escaping automation and it's dependency 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() function call would have occurred.

Golden rule is that you should never have the need to use stipslashes() when outputting data (after form submission 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 where INT represents all integer 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 PHP settings (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 don't need to check magic_quotes, it's effects on variables are removed.

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

Last edited by Vege : 09-25-2009 at 05:30 AM.
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
KEEP TABS
SPONSORS
 
Boxedart
 
 


 
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 03:49 PM.

   

Mascot team created by Drawshop.com

Powered by vBulletin® Version 3.6.7
Copyright ©2000 - 2009, 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.