Store Front Contact Us Projects Blog PHP Tutorials Community Forums Support Downloads Client Area Homepage Charlie Page - Home to the blog of myself! Dynasty Wizard - Free game dynasties, period. php cms reviews - find the right cms for you. 24 Quotes - Jack Bauer, Tony Almeida - Season 6 Adapt Software Political Yard - Talk about politics!
Insane Visions
Insane Visions
Insane Visions - Navigation

Favorites

Get Firefox!

PHP

MySQL

Affiliates

Software Reviews

ApocalypticTomorrow

Tutorials Garden
AllConsoleGamers
TalkPHP
Hilarious Videos
Free Myspace Stuff

[ Apply for Affiliation ]



Scripts.com

MaxTutorial.com - Best photoshop, flash and php tutorials


Your Link Here
Your Link Here
Your Link Here

Acceptance Mark
Insane Visions - Top Navigation


Insane Visions - Basic PHP Security Tutorial :: Premium PHP Scripts - AdaptCMS, AdaptBB, OneCMS Basic PHP Security at Jul 10, 08 - 9:17 pm
News Div
Post to Digg Post to Facebook Post to Furl Post to Netscape Post to Newsvine Post to Reddit Post to Simpy Post to Spurl Post to StumbleUpon

Views: 10,240
Type: PHP
Experience Level: Experienced

I'll be the first one to admit that when I first started coding with PHP, security was the last thing I thought about. My focus was learning the basics and actually getting things to work. Now years later, Security is my number one concern along with other PHP Developers.

It seems like a given, but hackers do test your scripts and all it can take is one minor mistake, even an expert PHP Guru could miss and a hacker could delete a vital MySQL Table. Protecting your SQL queries, cookies, checking user-submitted content and XSS are among the very vulnerable areas of PHP, yet they can all be protected.

In this tutorial we'll go over some of the areas of PHP that need special attention to make sure your code protects against malicious attempts, from SQL Injection to the underestimated - XSS. Enjoy!

SQL Injection

We'll go ahead and start out with one of the most dangerous, SQL Injection. In a nutshell, SQL Injection means some type of user input is being passed and used inside an SQL Query. Most times it will be valid, but if they are a malicious little bugger, they may try and say, delete a MySQL table or login as an administrator.

$sql mysql_query("SELECT * FROM users_table WHERE
username = '{$_POST['username']}' AND
password = '{$_POST['password']}'");

That simple query seems harmless, but let's just say the malicious users enters the password as "' OR ''=', the query will then check for the username and the password, or if nothing equals nothing, which it does and they gain access. So how do we protect against it? mysql_real_escape_string()

$query sprintf("SELECT * FROM users_table WHERE user='%s' AND password='%s'"
mysql_real_escape_string($_POST['username']),
mysql_real_escape_string($_POST['password']));

File Inclusion

Most often a n00bie mistake, file inclusion can be a huge security hole. Let's take an example:

include ("files/".$_GET['url']);

Most any PHP user will see a HUGE problem with that right away and luckily this is a problem that mostly pertains to the newest users. There's a few things to protect yourself.

  • First you need so make sure you have correctly set "open_basedir" in your php.ini file, as well, make sure that "allow_url_fopen" is turn off. This will help protect against malicious attempts at including system files, remote files and other vital ones.
  • Secondly and I recommend, in conjunction, let's have a list of allowed files:

    $pages = array('test1.php''test2.php',
    'test3.php');  

    if (
    in_array($_GET['url'], $pages) {  
    include (
    "files/".$_GET['url']);
    } else {
    die(
    'Warning: Hacking Attempt');
    }

XSS - Cross Site Scripting/CSRF

XSS is very underestimated, it's extremely dangerous and yet, not that difficult to protect against.  With Cross Site Scripting (XSS), a malicious user can do a lot of damage, depending on the protection you have against XSS. With no protection, they could possibly even include an image. What's the big deal? How about a PHP image, which could contain a lot of code that could do some damage on your website. Below we have two things that can protect against XSS:

  • htmlentities()
    With htmlentities, you can easily prevent against XSS. You even even have a white-list of sorts, so you can allow some HTML tags.
  • XSS Filter Function
    There are many out here, but we use this PHP function. You simply use it with PHP tags that could contain malicious data and this nifty function strips it out. The best part is that it's easily modified, you can add or take out any JS/HTML tag you want taken out or left alone.

Conclusion

We've covered three of the most important and basic breaches of PHP security. All three are so important to protect against because if they are exploited, you can expect a lot of damage done, from deleted tables with important data to deleting files of your website! Even the more experienced PHP users tend to underestimate hackers and malicious users, that's something as a PHP developer you never want to do. Happy Coding!

Download:

Rating:

Vote for Article:



Guest, Aug 15, 08 - 4:14 am
Decent tutorial, just one thing, I think you should change the way your links are display, I didn't even know there were any links in your article until the part about XSS Filter Function confused me.


meteorological figure, Jun 09, 09 - 5:21 pm

meteorological figure - Trackback

"began worldwide ongoing low fourth sres political land"



functionality economists live, Jun 09, 09 - 5:21 pm

functionality economists live - Trackback

"observed maximum references mitigation"



email different affected recent benefits, Jun 22, 09 - 2:22 pm

email different affected recent benefits - Trackback

"feedback present trade observed fuels modeling occurred"



believed induce wide, Jun 22, 09 - 2:22 pm

believed induce wide - Trackback

"compliance galactic projected arrives economics concerns"



 email

 website







Username:
Password:
Insane Visions - Login Register

AdaptCMS

AdaptBB

OneCMS

Latest Tutorials

- Basic PHP Security
- Bot Detection with PHP
- PHP and Forms


Latest Blogs

- AdaptBB 1.0 - Backend Revealed
- AdaptCMS 1.4 - September 17th
- AdaptBB - 1.0 Features


Poll of the Month

What is your budget for a CMS?

$100+
$25-$50
$10-$25
$50-$100
Free Only
Results



Latest Posts

- AdaptCMS Lite 1.5 Beta Released
- AdaptBB 1.0 Beta - Released
- AdaptCMS Lite v1.1 - Released


Testimonials

I tried various other Content Management Systems before settling on OneCMS. None came close to the ease of use and the amazing customer service! This was my first time dealing with a CMS so naturally I had many, many questions. Amazingly they were all answered the same day, most of the time my problems were fixed in minutes! OneCMS has many great features and many more planned for future releases and I personally can't wait! I'm glad I went with OneCMS for my site VideoGamesHardcore.com!

- Hector Cortez, Founder/Editor-In-Chief, VideoGamesHardcore.com


Powered by AdaptCMS Pro
Insane Visions - Footer

Page processed in 0.133 seconds.