Showing posts with label php tutorials. Show all posts
Showing posts with label php tutorials. Show all posts

Wednesday, 11 December 2013

Explode-Implode Functions File Handling In Php

Explode - Implode Function With
File Handling In Php
The explode function with file handling example in php programming language. Will read the file. echo out to the user for reading the data of the file by file handling in php programminglanguage.

Code for explode function with file handling example(Screenshot1)

explode-implode functions with file handling
explode-implode functions with file handling (Screenshot1)
1
2
3
4
5
6
7
8
9
10
11
12
<!--?php <br ?-->
$filename = 'names.txt';
$handle = fopen($filename,'r');
 
$datain = fread($handle, filesize($filename));
 
$names_array = explode(',', $datain);
 
foreach($names_array as $name) {
    echo $name. '
';
}

Output (Screenshot2,)

explode-implode functions with file handling
explode-implode functions with file handling (Screenshot2)
In this example we are going to learn to read data from a file but obviously when u are reading data from the file. Its not always line by line basis. You may find commas separating your values and in this case we got few names here commas separated each value. Now how we deal with reading this in. Php file handling reading into the file.
$handle fopen($filename, ‘r’);
Above code will create a handle to open and reading the file r stands for reading the file. We can also use file function which i had shown in example of file handling reading to the file. or we can also write $handle fopen(‘names.txt’,'r’);. I have taken names.txt in variale $filename for futhur use.
$datain = fread($handle, filesize($filename));
Above code will initialize the filesize will tell php file contain that much data instead of writing filesize we can also write the character we want from the file. like fread($handle,100); that’s means it will contain 100 characters from the file.
$names_array = explode(‘,’, $datain);
It will explode all commas from the file. Will remove commas fromthe file array of the name.
foreach($names_array as $name) {
echo $name. ‘<br>’;
}
It will get array will write names one by one.

Read More Extra Tutorials

Using Html Entities For Security In Php

Using Html Entities For Security In Php
Using Html Entities for security of html forms in php programminglanguage.The Article Working with data form, $_POST Variables , $_GET Variables. We seen the form of day, date, and year had taken form user and echo out.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<?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 ):-
Working with $_GET, $_POST Variables (Screenshot2)
Working with $_GET, $_POST Variables (Screenshot1)
Working with $_GET, $_POST Variables
Working with $_GET, $_POST Variables (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
Using html entities for Security (Screenshot3)
Using html entities for Security
Using html entities for Security (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
Using html entities for Security
Using html entities for Security
Using html entities for Security (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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<?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
Using html entities for Security (Screenshot7)
Using html entities for Security
Using html entities for Security (Screenshot8)
Using html entities for Security
Using html entities for Security (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 html entities 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, that’s basic security around your form. Its ensure that your user does not change background change color of background by iframe.