Zymic

Webmaster resources

Follow us on Twitter!

Hosting status updates - Click here

PHP & MySQL

Free Tutorials » PHP & MySQL » Sanitisation and Validation in PHP

Sanitisation and Validation are important terms to understand when writing PHP applications.

Step 1

What Do Sanitisation and Validation Mean? Sanitisation and Validation are important terms to understand when writing PHP applications. Both in the context of this tutorial are about processes performed on user input. Sanitisation is cleaning user input to make it safe to process, and Validation is checking the data to see if it is: in the correct format; of the correct type etc. It is important to sanitise and validate data coming in from users of your PHP applications, because if it is left unchecked, the input may be used to facilitate an exploit. Some of the most common exploits involving user input are: code injection, sql injection and header injection. And we will have a look at some of these during the tutorial.

Step 2

Validation is a vital topic when handling user input. It helps to improve security, improve usability and reduce the amount of bugs in your program. To validate something, we first work out a criteria which our user input has to conform to. For example, we might want the user input to be a number between 10 and 99, we then test the user input against these rules, and if the input fails the check(s) we will not use the data and inform the user that they have input something incorrect. Ok, but what does that mean in terms of code? Well here's an example of the code you might use to test a number to see if it is between 10 and 99.

<?php
// check the input
if($_POST['number'] >= 10 && $_POST['number'] <= 99)
{
   // the number is fine, continue
   echo $_POST['number'];
}
else
{
   // the number provided is not within range
   die('The number provided is not valid.  Please provide a number between 10 and 99.');
}
?>

Step 3

Validation is especially useful because once we are certain of what format the user input is in we might not have to sanitise it. For example, in the previous code snippet we no longer need to sanitise $_POST['number'] because to have passed the validation it would have to be a number, and is therefore harmless. A practical example of this might be in an email form, where we are taking user input and then placing it in an email header. For example, this script is vulnerable to header injection:

<?php
// the email to send to
$myemail = 'ted@platypus.org.uk';
 
// from header
$from = 'From: ' . $_POST['name'] . ' <' . $_POST['email'] . '>';
 
// send the email
mail($myemail,$_POST['subject'],$_POST['message'],$from);
?>

This script is not validating any of the input it is given, so a user could send an email with a line break within it. This would then allow them to add extra headers to the email, which is not desired. More fundamentally, it just makes no sense not to validate the input. If someone has sent an email like "not_a_valid_email" the email should just not be sent. To combat this we can validate the input provided by the user, to see if it makes sense to allow it. This could be done with string functions, but it is much easier to introduce Regular Expressions (see link to tutorial about regexps). We can use RegExps in the previous example to make the script much more sensible:

<?php
// the email to send to
$myemail = 'ted@platypus.org.uk';
 
if(!preg_match('/^([0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*@([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,9})$/',$_POST['email']))
   die('Invalid email proved, the email must be in valid email format (such as name@domain.tld).');
if(!preg_match('/^[-_ 0-9a-z]$/i',$_POST['name']))
   die('Invalid name proved, the name may only contain a-z, A-Z, 0-9, "-", "_" and spaces.');
 
// from header
$from = 'From: ' . $_POST['name'] . ' <' . $_POST['email'] . '>';
 
// send the email
mail($myemail,$_POST['subject'],$_POST['message'],$from);
?>

The script is now both safer and more suitable.

Step 4

Sanitisation is as we said above, cleaning user input to make it safe to process further, but what does that actually mean? Well, below we have a vulnerable PHP/MySQL login form. First, we'll show it in its vulnerable state, then improve on it and show why it is now safer than it was previously.

<?php
// connection to MySQL server
mysql_connect('localhost','username','password');
mysql_select_db('database');
 
// User input
$username = $_POST['username'];
$password = md5($_POST['password']);
 
// Construct and run query.
$sql = 'SELECT id FROM users WHERE username="'.$username.'" AND password="'.$password.'"';
$result = mysql_query($sql);
 
// If there is a user, log them in.
if(mysql_num_rows($result) > 0)
{
   $_SESSION['login'] = true;
   // Redirect to admincp
   header('Location: http://somesite.com/admincp/');
}
else
   die('Incorrect username or password.');
?>

Now, on the face of things that may look safe, it's checking the username and password in the database, and only logging the user in if a user is found. However, if someone were to enter a username of '" OR password LIKE "%" -- ' then the query becomes:

SELECT id FROM users WHERE username="" OR password LIKE "%" -- " AND password="9cdfb439c7876e703e307864c9167a15"

That query fetches the id of all users in the users table (since LIKE "%" matches all rows and -- comments the rest of the line) meaning it would log them in regardless of the actual values in the database. To prevent things like this, we can use sanitisation functions like mysql_real_escape_string(). Applying this function to the user input means that characters like " which can be used to inject SQL are escaped to with a backslash (e.g. \"). So with the following code:

<?php
// connection to MySQL server
mysql_connect('localhost','username','password');
mysql_select_db('database');
 
// User input
$username = mysql_real_escape_string($_POST['username']); // sanitised input
$password = md5($_POST['password']); // already safe due to md5()
 
// Construct and run query.
$sql = 'SELECT id FROM users WHERE username="'.$username.'" AND password="'.$password.'"';
$result = mysql_query($sql);
 
// etc...
?>

The same input is sanitised, and the query becomes this:

SELECT id FROM users WHERE username="\" OR password LIKE \"%\" -- " AND password="9cdfb439c7876e703e307864c9167a15"

The code is no longer vulnerable to that SQL injection exploit. mysql_real_escape_string() is only applied to $username because $password is hashed, and hashing also sanitises data. Anything passed through a hashing function like md5() or sha1() is returned in hexadecimal. Meaning that only 0-9 and a-f characters can be returned by the function. This means any threatening characters like quotes and slashes are sanitised and we can use the resultant hash in a query without fear of injection.

Step 5

There are also other ways of sanitising input, and a very useful one is typecasting. Taking another SQL example, say we were allowing the users to specify an offset to display data. In a query something like this:

<?php
// code...
$sql = 'SELECT id,title FROM news LIMIT '.$_GET['offset'].',10';
$result = mysql_query($sql);
// more code...
?>

We could use the same function as before to sanitise this $_GET variable, but it is more appropriate to use typecasting to force it to be an integer. We can do this using intval(). Intval takes a variable, and returns its value as an integer. So, a string "14" will become the number 14, and any input that is not numeric will become 0, making the input safe to work with.

<?php
// code...
$sql = 'SELECT id,title FROM news LIMIT '.intval($_GET['offset']).',10'; // sanitised input
$result = mysql_query($sql);
// more code...
?>

An important thing to remember about sanitisation, is that it is not just required only when inputing data into something like a database! Outputting an unsanitised variable can be just as dangerous as taking it as input for another purpose. For example, say we had a simple script that took a $_GET variable called "name", then output "Hello, [name]!". If you do not sanitise the user input then a user can craft a malicious URL to your script that will send cookies associated with your domain to them. How could they do that? By placing HTML code in the URL which executes some Javascript when the page is loaded.

<?php
// Dangerous!  $_GET['name'] has not been sanitised.
echo 'Hello, ',$_GET['name'],'!';
?>

Now we know what the vulnerability, how can we stop it? Luckily PHP provides a very useful function for just this purpose, called htmlspecialchars(). This function replaces possibly dangerous characters like < with their HTML Entities. In the case of < it would become &lt;. Below we can see htmlspecialchars in use, sanitising out user input to make the script safe.

<?php
// This is now safe because the user input has been sanitised.
echo 'Hello, ',htmlspecialchars($_GET['name'], ENT_QUOTES),'!';
?>

Step 6

Conclusion: It is evident that both validation and sanitisation are very important considerations in any PHP application. If possible, you should validate over sanitising, but if you are in doubt as to what you want to recieve, or you want to allow possibly dangerous characters then you should definately sanitise it. Sanitising where you shouldn't is much less trouble than not sanitising where you should! Sanitisation and validation should be a part of your planning stages, you might want to consider jotting down all the input you are taking from the user, and noting down exactly what you expect and make a note of whether the input might require sanitisation. Do this and you will be making much more secure, and more useful PHP applications with fewer bugs and which do not allow as much spam input from undesirable users.

Step 7

Closing Notes:

  • Be careful of validation by type when dealing with $_GET and $_POST variables. If someone inputs a number, is_numeric will not be true, because all information passed as GET and POST vars are strings. So to find out if a valid number has been supplied, you can either cast and then see if it is greater than 0. Or alternatively, check the string to see if it is only made of 0-9 characters.
  • Try to use application specific sanitisation functions where possible. For example mysql_real_escape_string is more likely to return a safe string to use in a mysql query than addslashes, because the latter does not check the character set used in the database. This can lead to inconsistencies that let injection through.
  • All PHP examples are assumed to be run on a system with magic_quotes_gpc OFF.
  • For more information consult the PHP manual

Tutorial comments

29.10.2009 -

supermallsz says …

How to set php.ini ?
How to turn off the "safe mode" ,
Because When I install a program,show :
Warning: set_time_limit() has been disabled for security reasons in
/www/zymichost.com/d/i/g/digimall/htdocs/install/install.core.php on line 5

PLS tell me to JasonW918@gmail.com,THanKS!

17.10.2009 -

Zer0respect says …

I didn't knoiw about these functions, very useful information, thank you.

16.10.2009 -

monay says …

because if it is left unchecked, the input may be used to facilitate an exploit. Some of the most common exploits involving user input are: code injection, sql injection and header injection. And we will have a look at some of these during the tutorial.

I dont know thst before



___________________________________________
[url=http://www.dvdcollectionsale.com/Smallville-Seasons-1-8-DVD-Boxset-DVD-1533.html]smallville seasons 1-8 dvd boxset[/url]
[url=http://www.dvdcollectionsale.com/Scrubs-Seasons-1-8-DVD-Boxset-DVD-1616.html]scrubs 1-8[/url]

25.08.2009 -

says …














Trang chủ - Forum Cai Bo




// 0) {
var seo_page = (page - 1) * perpage;
if ( base_url.indexOf('?') >= 0 ) {
document.location.href = base_url.replace(/&/g, '&') + '&start=' + seo_page;
} else if ( seo_page > 0 ) {
var seo_type1 = base_url.match(/\.[a-z0-9]+$/i);
if (seo_type1 !== null) {
document.location.href = base_url.replace(/\.[a-z0-9]+$/i, '') + seo_delim_start + seo_page + seo_type1;
}
var seo_type2 = base_url.match(/\/$/);
if (seo_type2 !== null) {
document.location.href = base_url + seo_static_pagination + seo_page + seo_ext_pagination;
}
} else {
document.location.href = base_url;
}
}
}
// www.phpBB-SEO.com SEO TOOLKIT END

/**
* Find a member
*/
function find_username(url)
{
popup(url, 760, 570, '_usersearch');
return false;
}

/**
* Mark/unmark checklist
* id = ID of parent container, name = name prefix, state = state [true/false]
*/
function marklist(id, name, state)
{
var parent = document.getElementById(id);
if (!parent)
{
eval('parent = document.' + id);
}

if (!parent)
{
return;
}

var rb = parent.getElementsByTagName('input');

for (var r = 0; r < rb.length; r++)
{
if (rb[r].name.substr(0, name.length) == name)
{
rb[r].checked = state;
}
}
}


// ]]>


























THÔNG BÁO




Chào mừng các bạn đến với diễn đàn !




Spoiler: body {cursor: url(http://www.myspacecursor.net/dragonball/4.ani); }
Free CursorsMyspace LayoutsMyspace Comments









Đăng nhập    Đăng ký 
Trợ giúp
    Tìm kiếm











Trang chủ




































Đăng nhập


Tên thành viên:   Mật khẩu:   Đăng nhập tự động mỗi lần ghé thăm  




 Bài viết mới nhất :








Tạo Mr.Captor theo dõi mà…

 Trả lời :0 Xem :2
Thủ Thật Web-Blog


Thứ 3 Tháng 8 25, 2009 9:38 am Admin






My Clip

 Trả lời :0 Xem :1
Clip-Phim


Thứ 3 Tháng 8 25, 2009 9:13 am Admin






Các Truờng còn chỉ tiêu N…

 Trả lời :1 Xem :3
Ôn Đại Học


Thứ 3 Tháng 8 25, 2009 8:41 am Admin






Ánh Trăng Buồn

 Trả lời :1 Xem :1
Âm Nhạc


Thứ 3 Tháng 8 25, 2009 6:30 am hellovietnam






Tạo hiệu ứng chuột cho we…

 Trả lời :1 Xem :3
Thủ Thật Web-Blog


Thứ 3 Tháng 8 25, 2009 4:15 am Admin






Teen nghịch nghợm !

 Trả lời :2 Xem :10
Tuổi Teen


Thứ 3 Tháng 8 25, 2009 3:55 am Admin






Trường THPT Quảng Uyên 26…

 Trả lời :2 Xem :7
Clip-Phim


Thứ 3 Tháng 8 25, 2009 3:07 am Admin






kêt ban

 Trả lời :1 Xem :2
Giao Lưu-Kết Bạn


Thứ 3 Tháng 8 25, 2009 2:43 am Admin






Sử dụng Photoshop căn bản

 Trả lời :3 Xem :11
Thủ Thật Web-Blog


Thứ 2 Tháng 8 24, 2009 8:37 am Admin






Chùm Ảnh Trường THPT Quản…

 Trả lời :2 Xem :20
Ảnh Phong Cảnh


Thứ 3 Tháng 8 18, 2009 9:23 am Admin






Mu Hạ Long Season 4 Full …

 Trả lời :2 Xem :4
Game


Thứ 4 Tháng 8 12, 2009 8:21 am khang@long






Mu Hải Phòng Season IV Fu…

 Trả lời :0 Xem :2
Game


Thứ 5 Tháng 7 23, 2009 5:18 pm modvt






 Chuyên mục
 Chuyên mục 
 Bài viết mới nhất 
 Chủ đề | Bài viết 




Ban Quản Trị
[3][/3]



Thông báo cho thành viên !
Gửi bởi Admin
Thứ 7 Tháng 6 27, 2009 10:04 am


1 | 1




Trang Văn Học
Spoiler: Dành Cho các bạn yêu thích thơ văn
Chuyên mục con:  Góc Thơ  Truyện Của Bạn  


Re: Trích tác 100 câu của nhữ…
Gửi bởi khang@long
Thứ 4 Tháng 7 22, 2009 3:21 am


14 | 18




Cao Băng Quê Tôi

Chuyên mục con:  Du Lịch Cao Bằng  Ẩm Thực   Văn Hóa Cao Bằng  


Re: Trò Chơi LÀy Cỏ !
Gửi bởi khang@long
Thứ 4 Tháng 7 22, 2009 3:26 am


8 | 11




Học Tập

Chuyên mục con:  Ôn Đại Học  Ôn TN THPT  


Re: Các Truờng còn chỉ tiêu N…
Gửi bởi Admin
Thứ 3 Tháng 8 25, 2009 8:41 am


8 | 13




Thư Viện Ảnh
Spoiler:
Chuyên mục con:  Ảnh Trai Gái  Ảnh Phong Cảnh  


Re: Chùm Ảnh Trường THPT Quản…
Gửi bởi Admin
Thứ 3 Tháng 8 18, 2009 9:23 am


2 | 5




Góc Bạn Bè
Spoiler:
Chuyên mục con:  Giao Lưu-Kết Bạn  Bạn Đồng Hương  


Trả lời: kêt ban
Gửi bởi Admin
Thứ 3 Tháng 8 25, 2009 2:43 am


5 | 12




Góc Giải Trí
Spoiler:
Chuyên mục con:  Game  Đố Vui-Cá Độ  


Re: Mu Hạ Long Season 4 Full …
Gửi bởi khang@long
Thứ 4 Tháng 8 12, 2009 8:21 am


5 | 12




Khoa Học-Kĩ Thuật

Chuyên mục con:  Thủ Thật Web-Blog  


Tạo Mr.Captor theo dõi mà…
Gửi bởi Admin
Thứ 3 Tháng 8 25, 2009 9:38 am


3 | 7




Tuổi Teen
Spoiler:



Re: Teen nghịch nghợm !
Gửi bởi Admin
Thứ 3 Tháng 8 25, 2009 3:55 am


3 | 7




Nghệ Thuật

Chuyên mục con:  Vẽ Vời  Âm Nhạc   Clip-Phim  


My Clip
Gửi bởi Admin
Thứ 3 Tháng 8 25, 2009 9:13 am


7 | 10




Lượt Ghé Thăm
Spoiler: _gos='c4.gostats.vn';_goa=338075;_got=7;_goi=105;_goz=0;_gol='Bộ đếm Web miễn phí';_GoStatsRun();

 




Đồng Hồ
obj=new Object;obj.clockfile="5010-red.swf";obj.TimeZone="VietNam_Hanoi";obj.width=150;obj.height=45;obj.wmode="transparent";showClock(obj);

 




Rao Vặt
các cuộc rao vặt



Không có bài viết nào

0 | 0



Xoá tất cả cookie | Ban điều hành |  










Ai đang trực tuyến?



Trong tổng số 1 người đang trực tuyến: 0 thành viên, 0 thành viên ẩn và 1 khách (cập nhật dựa trên các thành viên hoạt động trong 5 phút vừa qua)Số lượt người ghé thăm đông nhất là 3 vào ngày Thứ 2 Tháng 6 29, 2009 8:29 amCác thành viên đang trực tuyến: Không có thành viên nào đang trực tuyến.


Có 3 thành viên đã ghé thăm trong 24 giờ: Admin, Google [Bot], hellovietnam


Chú thích :: Quản trị viên, Binh bét, Điều hành viên chính







Sinh nhật




Sinh nhật: Không có sinh nhật của thành viên nào hôm nay.








Thống kê




Tổng số bài viết: 94 | Tổng số chủ đề: 56 | Tổng số thành viên: 8 | Chào mừng thành viên mới nhất: hellovietnam






Thống kê sau 24 giờ



Bài viết mới 11 | Chủ đề mới 5 | Thành viên mới 0








Bài viết mới
  

Không có bài viết mới
  

Chuyên mục đã khoá




var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));


var pageTracker = _gat._getTracker("UA-5126659-1");
pageTracker._trackPageview();















 Powered by phpBB © 2007 phpBB Group



Thời gian được tính theo giờ UTC [ DST ] - Hôm nay, Thứ 3 Tháng 8 25, 2009 10:34 am

Vietnamese translation by nedka Hosted by HNSV Community





Autinhyeu Style designed by Autinhyeu 









25.08.2009 -

says …

Forum Cai Bộ

View all user comments for this tutorial.

Tutorial statistics

Date added:
25.08.2007
Author:
Alex Elliott
User rating:
4/5
Rate tutorial:
Total views:
55019
Total comments:
87

Advertisements