PHP Mysql
PHP Mysql
PHP Mysql
Syntax
mysql_real_escape_string(string)connection) Parameter string connection Description Require!- Specifies the string to .e escape! /ptional- Specifies the MySQL connection- 0f not specifie!) the last connection opene! .y mysql_connect() or mysql_pconnect() is use!-
!xample "
56php 7con 8 mysql_connect('localhost') 'peter') 'a.c(9:'); if (47con) <
!ie(&Coul! not connect" & - mysql_error()); = >> some co!e to get username an! pass or! >> escape username an! pass or! for use in SQL 7user 8 mysql_real_escape_string(7user); 7p ! 8 mysql_real_escape_string(7p !); 7sql 8 'S,L,CT ? *R/M users @H,R, user8&' - 7user - '& +AB pass or!8&' - 7p ! - '&' >> more co!e mysql_close(7con); 6C
!xample #
Bata.ase attac3- This e$ample !emonstrates hat coul! happen if e !o not use the mysql_real_escape_string() function on the username an! pass or!" 56php 7con 8 mysql_connect('localhost') 'peter') 'a.c(9:'); if (47con) < !ie(&Coul! not connect" & - mysql_error()); = 7sql 8 'S,L,CT ? *R/M users @H,R, user8&<7_P/STD&user&E=& +AB pass or!8&<7_P/STD&p !&E=&'; mysql_query(7sql); >> @e !i!n&t chec3 username an! pass or!>> Coul! .e anything the user ante!4 ,$ample" 7_P/STD&user&E 8 &Fohn&; 7_P/STD&p !&E 8 '& /R &&8&'; >> some co!e mysql_close(7con); 6C The SQL sent oul! .e"
S,L,CT ? *R/M users @H,R, user8&Fohn& +AB pass or!8&& /R &&8&& This means that anyone coul! log in ithout a 2ali! pass or!4
!xample $
The correct ay to !o it to pre2ent !ata.ase attac3" 56php function chec3_input(72alue) < >> Stripslashes if (get_magic_quotes_gpc()) < 72alue 8 stripslashes(72alue); = >> Quote if not a num.er if (4is_numeric(72alue)) < 72alue 8 '&' - mysql_real_escape_string(72alue) - '&'; = return 72alue; = 7con 8 mysql_connect('localhost') 'peter') 'a.c(9:'); if (47con) < !ie(&Coul! not connect" & - mysql_error()); = >> Ma3e a safe SQL 7user 8 chec3_input(7_P/STD&user&E); 7p ! 8 chec3_input(7_P/STD&p !&E); 7sql 8 'S,L,CT ? *R/M users @H,R, user87user +AB pass or!87p !'; mysql_query(7sql); mysql_close(7con); 6C
Display
Aormal" S,L,CT ? *R/M customers @H,R, username 8 &timmy& 0nFection" S,L,CT ? *R/M customers @H,R, username 8 && /R (&&
The normal query is no pro.lem) as our MySQL statement ill Fust select e2erything from customers that has a username equal to timmyHo3e+er) the inFection attac3 has actually ma!e our query .eha2e !ifferently than e inten!e!Gy using a single quote (&) they ha2e en!e! the string part of our MySQL query
an! then a!!e! on to our @H,R, statement ith an /R clause of ( (al ays true)
This /R clause of ( ill al ays .e true an! so e+ery single entry in the 'customers' ta.le oul! .e selecte! .y this statement4
Display
S,L,CT ? *R/M customers @H,R, username 8 & &; B,L,T, *R/M customers @H,R, ( or username 8 & & 0f you ere run this query) then the inFecte! B,L,T, statement oul! completely empty your 'customers' ta.le- Ao that you 3no this is a pro.lem) ho can you pre2ent it6
Luc3y for you) this pro.lem has .een 3no n for a hile an! PHP has a speciallyHma!e function to pre2ent these attac3s- +ll you nee! to !o is use the mouthful of a function mysql_real_escape_string@hat mysql_real_escape_string !oes is ta3e a string that is going to .e use! in a MySQL query an! return the same string ith all SQL 0nFection attempts safely escape!- Gasically) it ill replace those trou.lesome quotes(&) a user might enter ith a MySQLHsafe su.stitute) an escape! quote #&Lets try out this function on our t o pre2ious inFection attac3s an! see ho it or3s-
$name/)ad = "' OR 0'"; $name/)ad = mysq%/rea%/es a+e/string8$name/)ad9; $query/)ad = "SELECT * FROM ustomers !"ERE username = '$name/)ad'"; e #o "Es a+ed :ad -n.e tion& ()r /*" ' $query/)ad ' "()r /*"; $name/e3i% = "'; 6ELETE FROM ustomers !"ERE 0 or username = '";
$name/e3i% = mysq%/rea%/es a+e/string8$name/e3i%9; $query/e3i% = "SELECT * FROM ustomers !"ERE username = '$name/e3i%'"; e #o "Es a+ed E3i% -n.e tion& ()r /*" ' $query/e3i%;
Display
,scape! Ga! 0nFection" S,L,CT ? *R/M customers @H,R, username 8 &#& /R (#&& ,scape! ,2il 0nFection" S,L,CT ? *R/M customers @H,R, username 8 &#&; B,L,T, *R/M customers @H,R, ( or username 8 #&& Aotice that those e2il quotes ha2e .een escape! ith a .ac3slash #) pre2enting the inFection attac3- Ao all these queries ill !o is try to fin! a username that is Fust completely ri!iculous"
Ga!" #& /R (#& ,2il" #&; B,L,T, *R/M customers @H,R, ( or username 8 #&
+n! 0 !on&t thin3 e ha2e to orry a.out those silly usernames getting access to our MySQL !ata.ase- So please !o use the han!y mysql_real_escape_string() function to help pre2ent SQL 0nFection attac3s on your e.sites- Iou ha2e no e$cuse not to use it after rea!ing this lesson4