Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,154,763 members, 7,824,189 topics. Date: Saturday, 11 May 2024 at 03:55 AM

Php/mysql Applications - Webmasters - Nairaland

Nairaland Forum / Science/Technology / Webmasters / Php/mysql Applications (1052 Views)

Php/mysql Pin And Serial Number Generation Script / Wamp Server Ebook Ft Install Wamp, Write Php / Mysql / Mysql Console / Learn Cakephp, Php/mysql For Free With Video Tutorials (2) (3) (4)

(1) (Reply) (Go Down)

Php/mysql Applications by dmark4real: 5:05pm On Jun 17, 2009
Anyone know how I can Protect my PHP/MySQL Applications from SQL Injection. (Database)

Thanks.
Re: Php/mysql Applications by nitation(m): 5:38pm On Jun 17, 2009
@ Poster

mysql_real_escape_string should solve the problem. Check out this link : http://php.net/mysql_real_escape_string

I believe it should help

I have an example of how to apply it

$username = mysql_real_escape_string($_POST['username']);
$password  = mysql_real_escape_string($_POST['password']);
$sql = "SELECT * FROM table_name WHERE user='$username' AND pass='$password'";

P.s  I do not advice storing passwords in plain text, what I wrote is just an example of how it should be applied. Do not SAVE passwords in plain text  during production.

- nitation
Re: Php/mysql Applications by TechPros(m): 10:21am On Jun 18, 2009
sql injection is majorly cause as a result of unverified/ unsanitized user input, so all u need to do is to make sure u sanitize all input passed in by user on any form that have a connection with ur database wink

nitation:

@ Poster

mysql_real_escape_string should solve the problem. Check out this link :
- nitation

as stated by nitation, mysql_real_escape($input); is a great funtion that can help sanitize all user input that contain characters use in a SQL statement and i think there are other php funtions that can be use or u can also define ur own funtion to do that.

nitation:


P.s I do not advice storing passwords in plain text, what I wrote is just an example of how it should be applied. Do not SAVE passwords in plain text during production.
nitation, i dont get u, pls explain
Re: Php/mysql Applications by kolitos007: 11:13am On Jun 18, 2009
Try this code, you can add as a function, if you are using classes or just add as function on the page you are working on

function sql_quote($value)
{
if( get_magic_quotes_gpc() )
{
$value = stripslashes($value);
}
//check if this function exists
if( function_exists("mysql_real_escape_string"wink )
{
$value = mysql_real_escape_string($value);
}
//for PHP version < 4.3.0 use addslashes
else
{
$value = addslashes($value);
}
return $value;
}



So on your sql query
for example
SELECT * FROM table WHERE id = sql_quote($id);
Re: Php/mysql Applications by segsalerty(m): 4:30pm On Jun 18, 2009
kolitos007:

Try this code, you can add as a function, if you are using classes or just add as function on the page you are working on

function sql_quote($value)
{
if( get_magic_quotes_gpc() )
{
$value = stripslashes($value);
}
//check if this function exists
if( function_exists("mysql_real_escape_string"wink )
{
$value = mysql_real_escape_string($value);
}
//for PHP version < 4.3.0 use addslashes
else
{
$value = addslashes($value);
}
return $value;
}



So on your sql query
for example
SELECT * FROM table WHERE id = sql_quote($id);


sorry everybody , i dislike it when people copy paste codes here with no explanation , why ?
well, let me add this to what my pros has told the poster to do ,
you can additionly sanitize your use inputs again by using this
$username = strip_tags(mysql_real_escape_string($_POST['usernaem']));
use same for the passoword too
am out ! lipsrsealed
Re: Php/mysql Applications by yawatide(f): 4:40pm On Jun 18, 2009
sega,

It is usually good coding practice to assign variables to your POSTs/GETs/REQUESTs for readability and maintenability wink

Consider this:
$username = $_POST['usernaem']);
$username = strip_tags(mysql_real_escape_string($username);

Of course, check my code for any errors.
Re: Php/mysql Applications by segsalerty(m): 4:45pm On Jun 18, 2009
Ofcourse mom ,
i no too dey like post codes here ,, its odd, i better give explanations , i no dey like all these copy paste friends here that wont solve posters problem but let them think "wow, see watin this guy that helped me put down from hin head" grin
just to help buddies here sha
well, i hope the guy has gotten enough help here
Re: Php/mysql Applications by kolitos007: 4:52pm On Jun 18, 2009
@sagsalerty

I am short of words, to be honest, so much for trying to help someone, yes this is my code, I have copied and just pasted it, like i said its a function, and you don't even need to make any changes to it, the user copies that code as it his, and just use the function simple.

Why don't you just explain to the person that needs help, instead of looking at what am doing, you are a such a mug!!
Re: Php/mysql Applications by segsalerty(m): 5:01pm On Jun 18, 2009
ah ah , funny buddy ,
you put up code and no explanation ? did you really helped him ? huh ?
men , tech someone how to catch fish not giving ready made roasted fish

abeg ! can you ask the poster if he doesnt know what GOOGLE does? did he tell us that he cant get help from GOOGLE or any seach engine by typing key word of the help he needed? wont that be wonderful and preferable coz GOOGLE can even link him to pages that have enough explanations of codes he is going to source from the page !
men , i know wat am saying , Better people enjoy what a forum entails,
well, thats enough i can say , , am out , i know those i contact for help that will explain to me into-to , nobody knows all
bye ,
na cafe i dey
Re: Php/mysql Applications by nitation(m): 5:04pm On Jun 18, 2009
@ Segs

What can I say?? Everyone na programmer now oo!

- nitation
Re: Php/mysql Applications by kolitos007: 6:36pm On Jun 18, 2009
@segsalerty
Well Boo Hoo to you then,what a plonker, so you are the super programmer, you don't even know who you talking to, you just start to throw insults at people you don't know, I find your comments very offensive and rude, I don't come on the forum and start to have a go at you for something you have said.

If I paste my code on it, its my own business, if the poster does not understand let him ask and then I explain to him, I don't need to explain myself to you, all I can say is I know your kind.

Good luck to you mate.
Re: Php/mysql Applications by kolitos007: 6:58pm On Jun 18, 2009
This guy segsalerty got on my nerves so am gonna chose to be more civil and explain to the geeza that needed help all over again.

Right my idea is for you to use functions, so that you can reuse it over and over again and it is not a mess when you are using it in your code so you can write your function and may be put it on top of your page if you are not using Object Orientated Programming OOP as it is called.

create the function below, assuming you have some knowledge of coding in php.
function sql_quote($value)
{
if( get_magic_quotes_gpc() )
{
$value = stripslashes($value);
}
//check if this function exists
if( function_exists("mysql_real_escape_string"wink )
{
$value = mysql_real_escape_string($value);
}
//for PHP version < 4.3.0 use addslashes
else
{
$value = addslashes($value);
}
return $value;
}


so you start with checking if there are quotes, quotes can often be used for mail injection as well, hence yo have (get_magic_quotes_gpc(), this is a php function by the way.)
Once there are qoutes, then you will use mysql_real_escape_string,

if( function_exists("mysql_real_escape_string"wink )//you are now checking if there are special characters
if that is the case then the you do this " $value = mysql_real_escape_string($value);" which is what that muppet tried to explain earlier on, note that his function is a php5 and above function, and if you are using php4 or lower version then you use addslashes, as shown above.

So you now have your function and you now know what it does, all you need to do is call the function, so when you post for example $_POST['username'], all you need to do is set variable
$username = sql_quote($_POST['username']);

And user $username in your query.

If segsalerty is not happy with that, he can let me know "plonker"
Re: Php/mysql Applications by biggjoe(m): 8:30pm On Jun 18, 2009
why is it hard to help people here without insults and name calling.

Is that how you people learned?

Most of you got what you have from forums and on these forums, people only aim to help others and not try to prove they are better.

Its pathetic.
Re: Php/mysql Applications by kolitos007: 8:51pm On Jun 18, 2009
biggjoe:

why is it hard to help people here without insults and name calling.

Is that how you people learned?

Most of you got what you have from forums and on these forums, people only aim to help others and not try to prove they are better.

Its pathetic.

Thank you for that, I was just pissed off at first, and realise it is not worth it, am thinking of the poor guy that asked for help, I have been on other forums and posted, no one is going on about what you have done, they just post suggestions, but on this forum, when you try to help, people think you are trying to show off, it is just typical, if you think you know something its good to give your opinion, no matter how ordinary a coder you are, you may still be able to help, I have dealt with coders on a high lever way better than me, but yet they still ask when they don't know, that makes them bigger in my eyes and I give them a lot of respect, and thats why they are good at what they do.

I hope more people see sense like you do
Re: Php/mysql Applications by nitation(m): 10:03pm On Jun 18, 2009
For sure! My advice for you is to act dumb and let the "best" declare. Thank you for the summary!

- nitation
Re: Php/mysql Applications by Nobody: 1:05pm On Jun 20, 2009
lol NL sickness, someone asks a Q, someone posts an A, another man pick A and starts prolonging the thread with it. why not let's pick the Q and extend it. angry
Re: Php/mysql Applications by Nobody: 2:33pm On Jun 20, 2009
OP have you solved the problem? before person die for here.
Re: Php/mysql Applications by Nobody: 8:34pm On Jun 20, 2009
maybe i shud share this.

I set up a function to check user input for strings like

0x, unionselect, outfile, load_file, unhex, hex, etc

you may want to strtolower() and do a trim on them, also put in mind that users might use 'select' as username.
i have had someone try to use unio[b]N[/b] se[b]L[/b]ect and it worked for him, or 'union select'

if it evaluates to true, then it opens the .htaccess file in append mode and appends

"\ndeny from ".$_SERVER['REMOTE_ADDR'];

trust me, he will need to use another ip and since he has no clue what is at the background, his new ip will also be blacklisted.
meanwhile at the background
there is also a mail() function which sends me a mail and since i spen most time online, i'll just log on to the cpanel log and have fun watching.

I think that will save u from sql injection
Re: Php/mysql Applications by kehers(m): 12:46am On Jun 26, 2009
@webdezzi
shocked
Re: Php/mysql Applications by TechPros(m): 3:49pm On Jun 26, 2009
grin
@kehers
that is webdezzi
Re: Php/mysql Applications by Nobody: 10:44pm On Jun 27, 2009
opps! did i just fart or waht

(1) (Reply)

How May I Help You On Php Language, I'm A Php Guru? / How Do I Get People To Register And Actively Participate On My Forum? / How Much Will You Pay For Article Writing And Submission Service?

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 36
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.