/    Sign up×
Community /Pin to ProfileBookmark

Product Search Security

I’m developing an e-commerce site, and thus far I have been able to add security against SQL injections by either removing spaces when getting information from the user, or replacing spaces with underscores when they are necessary (Like for addresses). But for the product search I am unable to do this without causing the query to fail. It is working fine right now but it takes the users query as is, so there is 0 security.

If anyone has some ideas for this or some experience please offer some advice.

to post a comment
PHP

1 Comments(s)

Copy linkTweet thisAlerts:
@NogDogMar 23.2006 — Spaces are not normally a security issue, to the best of my knowledge. Usually it's quotes and possibly back-slashes. The way I normally clean up user input before use in a query is:
[code=php]
function sanitize($text)
{
if(get_magic_quotes_gpc())
{
$text = stripslashes($text);
}
$text = mysql_real_escape_string($text);
return($text)
}
// USAGE:
$search = sanitize($_POST['search']);
[/code]

If using a database system other than MySQL, you'll probably need to replace mysql_real_escape_string() with addslashes():
[code=php]
function sanitize($text)
{
if(!get_magic_quotes_gpc())
{
$text = addslashes($text);
}
return($text);
}
[/code]
×

Success!

Help @Ebola spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.5,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...