Quick method to prevent cross site scripting in php

This Tutorial Has Been Viewed 1,423 Times.

Let’s define a simple function to prevent the querysting from being tampered with external code.

Lets take an example, If you have a webpage like

http://www.mistonline.in/search/index.php?name=java , there is every possiblity that a hacker can try to inject some javascript in that something like this

http://www.mistonline.in/search/index.php?name=<script language=javascript>setInterval
(“window.open(‘http://www.baddomain.com/’,'innerName’)”,50);
</script>

Like this there are numerous techniques, So inorder to prevent this from happening on your webpage use the below code which is very simple written using php

A Quick Look at Cross Site Scripting – Coding for our safety

function validateQueryString ( $queryString , $min=1,
$max=32 ) {
if ( !preg_match ( "/^([a-zA-Z0-9]{".$min.",".$max."}=[a-zA-Z0-9]{".$min.",".$max."}&amp;?)
+$/", $queryString ) ) {
return false;
}
return true;
}?&gt;

Once we have defined this function, we call it this way:

< ?php
$queryString = $_SERVER[‘QUERY_STRING’];
if ( !validateQueryString ( $queryString ) ) {
header( ‘Location:404.php’ );
}
else {
echo ‘Welcome to ’.stripslashes($_GET[‘name’].' pages');
}?>
VN:F [1.9.13_1145]
Rating: 0.0/10 (0 votes cast)
VN:F [1.9.13_1145]
Rating: 0 (from 0 votes)

Incoming search terms:





You will also be interested in ,

Tags: ,

Leave a Reply

Spam protection by WP Captcha-Free

Proudly designed by Mistonline.in.
Affordable Seo PackagesSeo BlogEdu Backlinks
More in PHP, scripts (81 of 150 articles)


Some months before i have written a code to avoid the direct access of files using php, but now i ...