PHP Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPHP Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old October 8th, 2012, 12:54 AM
WebDevStudio WebDevStudio is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2012
Posts: 12 WebDevStudio User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 3 m 39 sec
Reputation Power: 0
Php & mysql security

Hello ,
I'd like to ask which way is better to protect my scripts from hacking / sql injections ?

example:
is $uid= htmlspecialchars($_POST['uid']);

i use htmlspecialchars for all the $_POST/$_GET types and also make sure that sql parts include $uid and other tags in ''.

Is that secure enough? or its not secure at all or just give me an advice how to improve script security.

Please reply even if its good enough so i would know if i'm doing everything right

Reply With Quote
  #2  
Old October 8th, 2012, 03:56 AM
Jacques3 Jacques3 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2012
Posts: 57 Jacques3 User rank is Sergeant (500 - 2000 Reputation Level)Jacques3 User rank is Sergeant (500 - 2000 Reputation Level)Jacques3 User rank is Sergeant (500 - 2000 Reputation Level)Jacques3 User rank is Sergeant (500 - 2000 Reputation Level)Jacques3 User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 1 Day 10 h 6 m 14 sec
Reputation Power: 11
Hi,

this is not secure at all, because htmlspecialchars is for escaping HTML characters (just like the name says). What you need to do is escape chracters that have a special meaning in SQL. Those are two completely different things. All your function will do is mess up the strings.

In fact, you should forget the idea of a magic allround escaping function that takes care of everything. This doesn't work. Filtering input depends on the specific context and requires specific precautions. So do not just apply a function to all input. Instead, escape the input specifically for the particular context.

When you use the old mysql_ functions, this would look like this:
PHP Code:
 $sql "
    SELECT
        `name`
    FROM
        `users`
    WHERE
        `email` = '" 
mysql_real_escape_string($_POST['email']) . "'
"



Note that it's not always enough to escape special characters. Sometimes you need to do additional things (like the quotes for SQL values). And sometimes all escaping doesn't help (like when you insert user input in a <script> element).

The best approach, however, is to completely separate code and data. In SQL, you can do this with prepared statements (as provided by the Mysqli library or the PDO interface).

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPHP Development > Php & mysql security

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap