Friday, 21 March 2014

Using html entities for Security In Php

php security
The Article Working with data from, $_POST Variables , $_GET Variables. We seen the from of day, date, and year had taken from user and echo out.

Code for Working With $_GET Variables 


<?php



if (isset($_GET['day'])&&isset($_GET['date'])&&isset($_GET['year'])) {

$day = $_GET['day'];

$date = $_GET['date'];

$year = $_GET['year'];

if(!empty($day)&&!empty($date)&&!empty($year)) {

echo 'It is '.$day.' '.$date.''.$year;

}else {

echo 'Fill in all fields.';

}

}

?>



<form action = "index.php" method="GET">



Day:<br><input type="text" name="day"><br>

Date:<br><input type="text" name="date"><br>

Year:<br><input type="text" name="year"><br><br>

<input type="submit" value="submit">

</form>

Output (Screenshot1 , Screenshot2 ):-


using html entities for security in php
Screenshot1
date, time in php
Screenshot2
This example if you want to see in Working with form , $_GET Variables , $_POST variables . In this example we had user input and echo out the variables. 

Now This articles deals with security of the form data. If i want to extracting data from database or submitting data to database then extracting it back and any thing use to submitted. There is always chance the people unwanted things with your codes or with your web application may be now 

For Examples Let see Screenshots3 and Screenshot4
using html entities for security in php
Screenshot3
using html entities for security in php
Screenshot4


You can see the output to the page has now been formatted depending upon what user input in. Now the reasons it is dangerous because of iframe attribute.

<iframe src="pagehere"></iframe>


Output Screenshot5 and Screenshot6

using html entities for security in php
Screenshot5
using html entities for security in php
screenshot6
Now you can see what happen is we written this html tags and its echo down to the page. Therefore we have processing html on the page. We don't want our user to do this and there is simple way to do this there is simple and easiest way to protect against this.

Code for html entities for security

<?php
if (isset($_GET['day'])&&isset($_GET['date'])&&isset($_GET['year'])) {

$day = htmlentities($_GET['day']);

$date = htmlentities($_GET['date']);

$year = htmlentities($_GET['year']);

if(!empty($day)&&!empty($date)&&!empty($year)) {

echo 'It is '.$day.' '.$date.''.$year;

}else {

echo 'Fill in all fields.';

}

}

?>



<form action = "index.php" method="GET">



Day:<br><input type="text" name="day"><br>

Date:<br><input type="text" name="date"><br>

Year:<br><input type="text" name="year"><br><br>

<input type="submit" value="submit">

</form>

Output 

using html entities for security in php
screenshot7
entities for security in php
Screenshot8
html entities for security in php
Screenshot9
What we do is when we declare our variables $day, $date and $year. We can also include this wrapped function means $_GET to take variable user inputted to the form before that call function html entities. What htmlentities does shown in screenshots.  As you can see screenshot7 is the page source its html tags of iframe. So, with html entities what we actually doing Now you can see screenshot8 its just displaying text the user type not an html tags it will convert html tags to normal text.

So, thats basic security around your form. Its ensure that your user does not change background change color of background by iframe.



No comments:

Post a Comment

Thanks For Comment Will get you Soon..

About Me

Welcome to Extra Tutorials! My name is Mohammed and I am the 22 year writer, website developer, and photographer behind the blog. Thanks for visiting! Tutorials Jackpot… In addition to Developer, I love to develop websites and I love to write. Starting a php Blog was inevitable for me. What began as a simple way to share all of my Tutorials with friends and family has developed into my Part time job.

Mohammed Padela

WHAT IS PHP PROGRAMMING

WHAT IS PHP PROGRAMMING
WHAT IS PHP PROGRAMMING

Follow Us

Popular Posts

Designed ByBlogger Templates