| | |||||
| ||||||||
| Webmaster Tutorials Instructional webmaster-related how-to's and tutorials. |
![]() |
| | LinkBack | Thread Tools |
| | THREAD STARTER #1 (permalink) |
| New Member Join Date: Feb 2006
Posts: 3
![]() | Reducing Exploits Reducing Exploits When dealing with PHP, especially dynamics, it's important to always consider security. Here are some simple tips that will help you deal with the most common of those problems and exploits. 1. Disable register_globals. Disabling register_globals and using $_GET to obtain URL variables is much more secure. It prevents the visitors from changing other important variables in your code. To do this do the following: //In your .htaccess file add: php_flag register_globals 0 //In your PHP files, make sure you're using: Code: <?php $_GET[var]; ?> Code: Bad: <?php //$text should be "something"; //$text could be "<iframe src=\"evilscript...\"></iframe> echo $text; ?> Code: Good: <?php //$text has to be an integer //$text is now much more secure $text = (int) $text echo $text; ?> ????: NamePros.com http://www.namepros.com/webmaster-tutorials/168065-reducing-exploits.html Code: <?php
$input = $_GET[var];
switch ($input) {
case "home":
include("folder/home.php");
break;
case "about":
include("folder/about.php");
break;
//if none of the above
default:
include("folder/default.php");
break;
}
?> 4. Validate ALL user input. Validating all user input is VERY important. If you're careless you could end up having some nasty code embedded in your page or even have it defaced. There's numerous things that people can do, including inject your SQL, which you don't want to happen. Here's a couple functions PHP has which will greatly reduce these risks. Code: <?php
//lets strip out there HTML tags to avoid meta-refreshes and iframe redirections
$variable = strip_tags($variable);
//lets get rid of any special HTML characters which might play with our script.
$variable = htmlspecialchars($variable);
//even with the above, you'll want to give yourself some added protection again mySQL injection magic_quotes are great, but aren't always available.
if (!get_magic_quotes_gpc()) {
$variable = addslashes($variable);
}
//you will want to note that you'll want to set something up so that if they're added, they're also removed later on.
//Let's say you're letting them fill out out form and there's quite a few $_POST variables you want to validate, to get them all easily use array_map
$_POST = array_map('strip_tags',$_POST);
$_POST = array_map('htmlspecialchars',$_POST);
if (!get_magic_quotes_gpc()) {
$_POST = array_map('addslashes',$_POST);
}
?> |
| |
![]() |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| |
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Reducing page size | suthra | Programming | 5 | 10-28-2005 05:23 AM |