no script is 100% unhackable .. ive worked with some of the best PHP coders you will ever find and there scripts will grow an extra 1000 lines from a basic security while adding all the extra security stuff and still i have been able to exploit scripts
the thing to think about is how can someone hack ur site .. well the two most common ways are SQL injection and XSS exploits.
XSS exploits are generally performed by the user running javascript on a page. Now they cant directly affect your site. Javscript is client side and is managed by the browser after the server has sent the page to you. However they can grab details from you. How about if i use an unprotected form field to write a javascript redirect refresh header to my server. Ive done this before to test the theory and it works. i simply send the user to a file on my server which grabs there SID, saves it to a file then sends them back to the users online page. Now i have that users SID to access the site (if there an owner im now using an owner account with all the goodies).
Ofcourse this was just a thoery practice.. With a little messing about i could instead send the user back to my user page and have it display as normal and they wouldnt have the faintest idea about the script.
So how do you protect against XSS .. well for javascript and most other basic XSS atacks you use the htmlspecialchars() function built into PHP. this will stop the script tags being passed correctly to the browser and will show up as < script > instead of < script >
SQL is a lot more complicated .. there are 2 main rules of thumb here:
- NEVER USE MAGIC QUOTES !
magic quotes are bad... period. if you wanna know why then use google but basically u shudnt rely on them. This is proven by the fact php dev's are removing them from php6 and above (i think php 5 will support magic quotes throughout the minor updates).
- SANTIZE ALL DATA !
any data that the user enters must be sanitised. If you expect a number then make sure its a number. If its a string then make sure all "bad" chars are escaped.
for example
$id = $_GET['id'];
that is bad .. what happens if i enter ?id=djlee .. when the sql is run it will run the £id as djlee not a number and will cause an error. obviously from here the hacker knows you have not secured the script and can populate the ID with some sql statements in order to delete tables or make themselves owner.
to protect it is simple
$id = 0 + $_GET['id'];
i prefer to use 0 + however (int) works fine too. so now lets check what happens . $id = 0 + djlee .. $id = 0; .. any string that is added/subtracted/divided/multiplied by a number must be type casted to a integer. as $_get['id'] is not a valid number when it is type casted it becomes 0 and 0 + 0 = 0
to protect strings then as stated before use the mysql_real_escape_string() function at least
these are just basic preventions though. other things you need to do is go over php.ini, my.cnf and httpd.conf and secure them.. they are not secure on 99% of servers.
also remember that i can encode things too. if you have the username in the GET request $user = $_GET['user'] i can use html encoded characters to avoid your protection... urldecode () will decode html encoded characters so that any other protection methods will function correctly.
Protecting your site from hackers will never stop. its an ongoing thing. however it is important you protect ur site the scope your experience will allow you to do so. There will always be others out there that have figured a way to get around ever protective measure on a site. and as soon as they tell you (if they are a community developer and not a hacker ofcourse) you will patch it and someone else will come along with a new working idea to hack past it all
personally id learn how to script PHP properly and build your own site from the ground up .. mostly because lavalair is shit and is by fair over used .. its bout time we had a few more wapsites that didnt look like the other 1000 already out there