/    Sign up×
Community /Pin to ProfileBookmark

MySQL Injection in PHP

I am almost positive i have properly protected myself against mysql injections but I would like someone who knows better than I if I actually am protected or if its a facade. Thank you for your help.

[code=php]<?php
include(“include/constants.php”);
$var = @$_GET[‘query’] ;
$trimmed = trim($var); //trim whitespace from the stored variable

// check for an empty string and display a message.
if ($trimmed == “”)
{
include(“s.php”);
echo “<p>Please enter a search…</p>”;
echo “<p><a href=”main.php”>Home</a></p>”;
exit;
}

$connect = mysql_connect(DB_SERVER, DB_USER, DB_PASS);

$query = mysql_real_escape_string($trimmed);
if (!($connect)) // If no connect, error and exit().
{
echo(“<p>Unable to connect to the database server.</p>”);
exit();
}

if (!(mysql_select_db(DB_NAME))) // If can’t connect to database, error and exit().
{
echo(“<p>Unable to locate the database.</p>”);
exit();
}

if (!($limit)){
$limit = 500;} // Default results per-page.

if (!($page)){
$page = 0;} // Default page value.

$numresults = mysql_query(“SELECT * FROM projects WHERE projectassigned LIKE ‘%”. $query .”%’ order by opclo DESC”); // the query.
// More code but its omitted for post length.
?>
[/code]

to post a comment
PHP

7 Comments(s)

Copy linkTweet thisAlerts:
@dtm32236Aug 20.2009 — I'm really new at this and shouldn't be giving advice, but I'd definitely look into mysql_real_escape_string:

http://us2.php.net/manual/en/function.mysql-real-escape-string.php

PS - what does the at symbol (@) do? [B]$var = @$_GET['query'];[/B]

I know I've seen that before. I think it removes error messages or something(?)
Copy linkTweet thisAlerts:
@drumbum360authorAug 20.2009 — the @ sign suppresses error messages.

I have included the mysql_real_escape_string to my query. I tried a test attack to delete everything in my database and it didn't delete anything so I'm pretty sure I'm safe but wanted to make sure from someone on here.

Thanks for the response dtm32236!
Copy linkTweet thisAlerts:
@dtm32236Aug 20.2009 — "$query = mysql_real_escape_string($trimmed);"

That's me being dumb. I didn't see that.

As far as I know, the only thing I do when inserting into a database is trim() and mysql_real_escape_string().

I don't know if you need to worry about XSS (I haven't looked into these issues much yet):

http://shiflett.org/blog/2005/jan/xss-cheatsheet

Hopefully someone with more experience could chime in...
Copy linkTweet thisAlerts:
@NogDogAug 20.2009 — The database couldn't care less about XSS, as all it does is store data. (OK, it does other stuff with the data, but you know what I mean.)

So from the standpoint of SQL injection, XSS is a non-issue. It [i]is[/i] an issue, however, if/when you want to retrieve any of the data and display it to the user's browser. So it is something to consider when validating/filtering user inputs, and also when escaping output to the browser. (See strip_tags(), htmlspecialchars(), and htmlentities() for various approaches. Which approach is correct is dependent on each specific situation.)
Copy linkTweet thisAlerts:
@NogDogAug 20.2009 — PS: With regards to SQL injection, assuming you are running PHP5.0 or later (and if not, [i]why[/i]?), you should consider moving up to the MySQL[b]i[/b] extension or a database abstraction layer such as PDO, and make use of prepared statements for your queries. Besides other advantages of using these extensions, the use of prepared statements with bound parameters [i]forces[/i] the correct escaping of inputs (assuming you use bound parameters for all external inputs).
Copy linkTweet thisAlerts:
@JunkMaleAug 22.2009 — PS: With regards to SQL injection, assuming you are running PHP5.0 or later (and if not, [i]why[/i]?) ...[/QUOTE]

If you are on a web hosts server... even if its dedicated one... you do not get any access to that side of the box. The only time people have access to the server settings beyond a CP is where they are a company hosting their own servers or the web host is hosting the companys own hardware.
Copy linkTweet thisAlerts:
@NogDogAug 22.2009 — If you are on a web hosts server... even if its dedicated one... you do not get any access to that side of the box. The only time people have access to the server settings beyond a CP is where they are a company hosting their own servers or the web host is hosting the companys own hardware.[/QUOTE]

Besides the fact that PHP5 has been out for over 5 years now, it is now over a year since the PHP development team ceased supporting PHP4 in any manner, [i]including security patches[/i]. Any web host that does not provide PHP5 - and prefereably keeps up to date with the latest releases so that security fixes are included - is doing you a disservice both in terms of functionality and security. If nothing else, it is easy to provide [i]both[/i] PHP4 and PHP5 by running one as an Apache module and one in CGI mode (requiring that one or the other use a different file name suffix, such as ".php4" for PHP4 and ".php" for PHP5.

So frankly, if your web hosting company does not support PHP5 and will not add support for it when you ask nicely, then it's time to move to another hosting company that knows what it's doing.
×

Success!

Help @drumbum360 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.18,
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: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

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