Saturday, October 1, 2011

File Operation in PHP (Part 2)

Assalamualaikum and Selamat Sejahtera

Today we will learn these : fopen(),    feof()    ,fgets()    ,fclose().

1.Use the previous post text file.

2.Open the "www" folder,make a new PHP file called : openfile.php.Inside this file,write this code below.



3..Save it and run this file ,by type at the browser's address : http://localhost/openfile.php.


4.So,as we run it it will display this on the browser :

What we usually expect it,it will display the content of file.But it only display this words.For your information,this words are the pointer for phpFILE.txt. So,how to display it?well we will use the next function which is fgets(). Here is the code.

So,before run this file.Open the phpFILE.txt first and then add another this statement :


Okey,now run it....and of course la,the browser will display like this :

So,the function that we will discuss is feof().The meaning of feof is "file end of file".So,in while loop,it will keep asking the function "file end of file" either its already end of file yet.Function "file end of file" will be true when it reach at end of file.That why if we type while like this : while( feof ($file_to_handle) ) ,it will only show the end line of file only.

Furthermore,fopen(first parameter , second parameter) function for to open the file.The first parameter is for the name of file.The second parameter is for mode ,"r" is means read.Here is example of modes:


ModesDescription
rRead only. Starts at the beginning of the file
r+Read/Write. Starts at the beginning of the file
wWrite only. Opens and clears the contents of file; or creates a new file if it doesn't exist
w+Read/Write. Opens and clears the contents of file; or creates a new file if it doesn't exist
aAppend. Opens and writes to the end of the file or creates a new file if it doesn't exist
a+Read/Append. Preserves file content by writing to the end of the file
xWrite only. Creates a new file. Returns FALSE and an error if file already exists
x+Read/Write. Creates a new file. Returns FALSE and an error if file already exists


That all for today
Thank you!

Friday, September 30, 2011

File Operation in PHP



Assalamualaikum and Selamat Sejahtera

1.Make a text file called phpFILE.txt.Inside that text file,just type "This is PHP file.You open this file!".

2.Then put this textfile inside the folder called "www".

3.Inside the "www" folder,make a new PHP file called : readfile.php.Inside this file,write this code below



4.Save it and run this file ,by type at the browser's address : http://localhost/readfile.php

Beside,there is another function same as readfile("nameofFILE.txt") which is called file_get_contents(the_file_that_to_be_read).Here is the example of code:



That all for today,Thank You!!

Sunday, May 29, 2011

Security PHP part(3)

Assalamualaikum and selamat sejahtera

As we already learn at previous post ,we only learn on function that just straightly print out the any special characters or code.But,unfortunately that function is not that dynamic because maybe some special characters or code,we think that it is not harmful.So,for this post we will learn a function that enable us to select any characters and this function will read the selected the characters instead of straightly print......Word is not good as i show the picture,ryte?So,below i show to you,the function that i talk about.

The function is:  strip_tags($string,tags-that-to-ignore);

So,here is the situation,we want the power up the security that we want to block any tags that might harmful such as anchor tag(<A HREF="aaa"></A>) that link to any website.But,for bold tag(<B></B>) is not a harmful tag ryte...it just bold some text.So,to make this situation happen.First,we copy first the previous code ,then find this line:

$name = $_POST['name'];
echo $name;

and change it to this:

$name = $_POST['name'];
$name = strip_tags(
$name, "<B>");
echo $name;
Run it,and type this code below inside the textbox ,then click the 'send'  button:

<B>Hye,i'm a bold text</B>

it should display right this,ryte?
 


   That all for today!!any question??just ask here

N/nOtE:So,we can unblock any tag that we want,just only one type of tag only.

Saturday, May 28, 2011

Security PHP part(2)

Assalamualaikum and selamat sejahtera

Wow,its been a long time that i stop to post.Well,something happen must have some reason to cause  it happened ,right?huhuhu...the reason is i'am quite busy recently.doing some homework..like sweeping the leaves,throw some thrash,reading comic(Shin Chan),sleeping and some jobs that i think it quite reasonable to do,haha.Okey,just stop this some nonsense things,now we just continue what we have not done yet.

To follow this post please refer the previous post first.

There is a lot of ways to tackle this attack,so,for this post,we will only learn the two function to practice the security.

So,the first function is htmlspecialchars().Here is the example:

First copy this code:

 <html>
<head>
<title>Security PHP</title>



<?PHP
if ($_SERVER['REQUEST_METHOD'] == 'POST'){
$name = $_POST['name'];

echo $name;
}


?>

</head>
<BODY>

<Form Method = "Post" action ="SecurityinPHP.php">
<input type = "text" name = "name" value ="Test full name">
<input type="submit" name="Submit" value="send">
</Form>

</BODY>
</html>


 and then,try to change this line:

$name = $_POST['name'];
echo $name;
 

to this:

$name = $_POST['name'];
$name = htmlspecialchars($name);
echo $name;

 Then,run your code and type this code below into the textbox,then click "send" button:

"<A HREF ="dummysite">A Bad/Virused Site</A>"
  

it should display right this ryte:

 So,it means that the textbox does not longer read any special characters or any code anymore just like the previous post.It just straightly print the code or any special characters without read it.It quite safe right.But how about if the attacker use the different language like Russia,Germany,Arabic and others than English.Do not worry we have another flexible function that called htmlentities().To test it,just replace it with htmlspecialchars()  at the code.

Thank you,that's all for today,See you next time,hahaha


N/NOte :i'm more prefer on htmlentities() rather that htmlspecialchars().If you got something to ask or to add,just comment here..^^

Sunday, April 17, 2011

Sunday, March 20, 2011

HOW-TO-INCLUDE-FILE-PHP

Assalamualaikum and selamat sejahtera

Morning!!hope u in good condition or in happy mood...waaa its been a long.....time that i stop posting,huhu..whatever it is...let we start with the new technique that enable us to include a file by using a PHP code.Example,u got a php code and then u want to include some file that content php code too!!wow it sound something new!!ryte2??hahaha..shall we start...off we go!!!

so,we a file that named myFile.txt

inside myFile.txt type:

this is from myFile.txt

close myFile.txt.After that,here is PHP code:

<HTML>
<HEAD>
<TITLE>Include files</TITLE>
</HEAD>

<BODY>
<H3>Normal text here </H3>
This text written in HTML

<H3>Include File here</H3>

<?PHP include "myFile.txt" ; ?>

</ BODY>
</ HTML >

test it!!

okey,then edit myFile.txt.open it and change that text to be like this:


<?PHP

function PrintFromFile( ) {
print "This function from the myFile.php";
}


?>

then save n close it,go to code, search this line:

<?PHP include "myFile.txt" ; ?>  

change it,to be like this:

<?PHP include "myFile.txt" ;
PrintFromFile()
?>

explain:note that,inside the PHP code,we add the name of function that from included file that all.Okey run it!


N/NOTE:anything problem???

Tuesday, March 1, 2011

Header Function PHP

Assalamu'alaikum and selamat sejahtera

Have you ever wonder that a line of code that enable us just go the other website.I meant before this,we just run the php file then it just display as white background and what we can see form address column it just write "http://localhost/...".So,how us to run or execute the php file and then automatically change the address column to any website that we desire.Here is the code:
 
<?PHP
header("Location: http://www.google.com");
?>

run it!so it go to google page ryte?automatically!

N/NOTE:wah!!so fun!!hehe

Special Post:$_SERVER

Assalamu'alaikum and selamat sejahtera

As i know la,we just had 6 post for creating function,well this post it is just for knowledge.ok ?hahaha,well its quite boring if we just had the same title post everyday!So,imagine that you are an admin and then you want to know the information of server and a little bit on visitor like which website the visitor come from,what browser the visitor used.

Here is the code:

<?PHP

foreach($_SERVER as $name => $value) {
print $name . " = " . $value . "<br>";
}

?>

N/NOTE:don't expect me high!on this post....i just know a little bit only...huhuhu,i just know to display that all...but we can learn!!hahaha

Create-new-function-PHP (Part 6)

Assalamu'alaikum and selamat sejahtera

So,now we will learn on the other alternative of the return.Before this,we must type "return" in order the function to return the value to any variable that come from the function.So,we just use one character only!to enable the return value.Here is the example:


<?PHP

$number1 = 120;
calculate_addition($number1);

print $number1;
 
function calculate_addition(&$number1){
$number2 = 10;


if ($number1 > 100) {
$number1=$number1+$number2;
}
 

else {
$number1= $number1-90;
}

}

?>

we just use "&" as the alternative of the return.Compare this code from previous post:

<?PHP
$number1 = 120;
$total = calculate_addition($number1);

print $total;
function calculate_addition($number1){
$number2 = 10;

if ($number1 > 100) {
$number1=$number1+$number2;
}

else {
$number1= $number1-90;
}

return $number1;
}

?>
 


N/NOTE:&!!!!!!

Create-new-function-PHP (Part 5)

Assalamu'alaikum and selamat sejahtera

So,for this post we will learn on how to get value from the function,before we already learn on how display the value of variable from the function.Ok,here is the code:


<?PHP

$number1 = 120;
$total = calculate_addition($number1);
 

print $total;
 
function calculate_addition($number1){
$number2 = 10;


if ($number1 > 100) {
$number1=$number1+$number2;
}
 

else {
$number1= $number1-90;
}
 

return $number1;
}

?>

So for this post,let me explain the new word,"return".the concept is simple is that it is for assigning any variable from outside the function.In our case,we use $total to get the value from the return.So,we just got the value from the function.



 N/NOTE:the function,calculate_addition return the value ,and then $total are the variable that receive the value from the function.Proof:$total = calculate_addition($number1); .Any question?

Monday, February 28, 2011

Create-new-function-PHP (Part 4)

Assalamu'alaikum and selamat sejahtera

For this post,we will learn on how to create a new function that can detect the space or blank space from application.Well,we already learn the trim() but for this function,we want to display some error message if the user did not put anything but still press the submit button.Here is the example:


<?PHP
$user_input = trim("Ali Abu ");
detect_blank($user_input);
function detect_blank($user_input) {
if ($user_input == "") {
print "Sorry,you forget to fill the textbox";
}
else {
print "The form is OK";
}
}

?>

ok,you run it..it print "The form is OK" ryte?so how about change this line,just assuming that $user_input is get the value from the form/application that fill by the user.So change this line:


$user_input = trim("Ali Abu ");
to this:


$user_input = trim("");
so,it print "Sorry,you forget to fill the textbox" ryte?

that's all for today!adios!

N/NOTE:new function for this hectic morning!

Sunday, February 27, 2011

Create-new-function-PHP (Part 3)

Assalamu'alaikum and selamat sejahtera

For this post,we will learn on how to past the variable that declared that outside the function to the our function,than that function will process it and come out with output.Here is the example:


<?PHP
 
$display_text = "Variable message";
display_message($display_text);

function display_message($display_text) {
print $display_text;
}


?>

or how about we apply some mathmatic:


<?PHP
$number = 2;
display_math($number);

function display_math($number) 
{
$number=$number+4;
print $number;
}

?>

it print 2 or 4 or 6?so,i leave that answer to you. How about you just try to create a function that can accept multiple variable.Here is the example of function that can accept more than one variable::

function display_subtraction($number1,$number2)

try it!




N/NOTE:any problem?just comment here ^^

Create-new-function-PHP (part 2)

Assalamu'alaikum and selamat sejahtera

For this post we will try some function to display the variable.Ok,here is the example:

<?PHP
$display_text = "variable Detetceted";
display_message( );
function display_message( ) {
print $display_text;
}

?>

Run it,it print error ryte?so we call it and already declared the variable "$display_text" and yet we still can not display that variable in the function.So,as the conclusion,the function must have its variable itself ,then it can display its variable.what i meant is like this:

<?PHP
display_message( );
function display_message( ) {
$display_text = "display message";
print $display_text;
}

?>

that all....
N/NOTE:how about we declare a variable inside the function.....then directly display that variable,i meant display it without calling the function..try it!taste it! 
 

Saturday, February 26, 2011

Introduction to String function PHP(part 2)

Assalamu'alaikum and selamat sejahtera

Here is the example of the string function in PHP.

str_replace()

$text = "The string function";
$THEwordTHATyouWANTtoCHANGEit = "string";
$REPLACEthatWORDwithTHIS = "strong";

print $text . "<BR>";
$text = str_replace($THEwordTHATyouWANTtoCHANGEit,
$REPLACEthatWORDwithTHIS
, $text);
print $text;

str_word_count()

$number_words = str_word_count("how many word?");
print $number_words;

strlen()

$length_of_string = strlen("This is how to count the length of string");

-note:it count the character and the space in the string.

substr()

$username = "my name is dotcom";
$check = substr($username, strlen($username) - 6);

if ($check == "dotcom" ) {
print "ends in dotcom";
}
else {
print "doesn't end in dotcom";

-note:strlen($username)-6 :PHP will take six character because we put(-6), if -4 then it take 4 character that behind this string.So,our string is this :my name is dotcom,then PHP will start to count at red,bold 'm' as one until six.So sixth charatcer is:d . Then,$check is equal to 'dotcom'.


N/NOTE:selamat mencuba!!!

 

Create-new-function-PHP

Assalamu'alaikum and selamat sejahtera

On this post,we will learn the new function.So,to create new function we just type "function" with name that we desire like this :

function name_that_you_desire(){

print "this is my function!";

}

so,take this code or make your own function and put it inside PHP tag:
<?PHP
function name_that_you_desire(){
print "this is my function!";

}
?>

then run it...nothing happen ryte?its just like the other function..that we need to call it.In this case,we call it:

name_that_you_desire();


<?PHP
name_that_you_desire();

function name_that_you_desire(){

print "this is my function!";

}

?>

or


<?PHP
function name_that_you_desire(){
print "this is my function!";

}
name_that_you_desire();

?>

N/NOTE:custom function...

Introduction to String function PHP(part 1)

Assalamu'alaikum and selamat sejahtera

Here is the example of String function in PHP
----->TRY IT!<----------

chr()


$email = "nuar" . chr(64) . "Email.com";
print $email;

ord()


$asciiNumber= ord("%");
print $asciiNumber; 

echo()


$Alternativedisplay = "the other option to display";
print $Alternativedisplay;
echo $Alternativedisplay;

similar_text()

$real_name ="John lennon";
$guess_name = "John lenon";

$check_answer = similar_text($real_name, $guess_name, $percentCorrect);
print($check_answer) . "<BR>";
print($percentCorrect . "% correct");


//tell the accuracy of $guess_name as compare to $real name in percentage 
 
str_repeat()

$extra_alias = str_repeat("@", 5);
print $extra_alias;


click here for more on ASCII number 
 

Friday, February 18, 2011

Escaping technique-PHP

Assalamu'alaikum and selamat sejahtera

Copy this one and run it:

<?PHP
$escape = 'Alex's pencil';
print $escape;

?>

it print error ryte?it is because PHP is confuse because this variable got three single quotes ,So,we need to remove one of  it but we still want to display "Alex's pencil" on the browser.So change this line :
$escape = 'Alex\'s pencil';

no error ryte? it is because,this variable got  this ' \ '.This ' \ ' is called escape(as i understand la..),so it tell the PHP to skip one character only after it.After ' \ ' is single quote ryte?So PHP skip it..so PHP said that variable got two single quote only!

N/NOTE:  $escape = "Alex\'s pencil"; use double quote also can maa,hahaha

How-to-join-text-in-PHP script (part 2)

Assalamu'alaikum and selamat sejahtera

Okey....we already learn the joining thing in last post ryte...if you did not remember,just click here.but in this post,we will do on array..here is the example:


$group = array("Alpha", "Beta", "Charlie", "Delta");


and then we join it using implode():


$group=implode(",", $group);


the concept is same with explode()  that is,it need two thing: the separator and the variable name(but in array).
then we print it:

print $group;

the code:

<?PHP
$group = array("Alpha", "Beta", "Charlie", "Delta");

$group=implode(",", $group);
 print $group;
?>


try this!

$group=implode("-", $group);

Or

$group=implode("#", $group);
 
Or

$group=implode(" ", $group);

N/NOTE:any question guys?

How-to-split-a-text--->PHP

Assalamu'alaikum dan selamat sejahtera

Selamt pagi!morning!ohayou!

So,for this post,we will learn on how to split a text in PHP ,here is the quick example:

$line="Group 1,200,250,150,100,700";

but we want print it to be like this:


Group 1
200
:250
150
100
700

So,here is the code:

<?PHP
$line = "Group 1,200,250,150,100,700";
$line = explode(",",$line);
print $line[0];
?>

okey,let see...we got the new code again..here is:
explode(",",$line);

 so,explode is function to split the variable...so,inside the round bracket,we got a comma and variable name.we know that variable name is used for reference but for comma is to tell the explode function,that in order to split the text,you must read until you find the comma,then the next character will be split.so far okay?
 another explanation...is order to use this function,you must have two thing...one separator,one is variable..so,in this case,we declare $line  like this:


$line = "Group 1  ,  200  ,   250  ,   150   ,   100  ,   700";

 
so,we got the variable and the separator is "," is comma because it separate the word "Group 1" from other number :200,250,150,100,700.so far okey?hahaha

but the browser print "Group 1" ryte?it is because when the variable "$line" is split (using explode() function,the $line will change to an array that why in order to print it,we must put "[ ]".So,to print all,we can use for loops or this method:


print_r($line);

Then change the declaration of $line to be like this:

$line = "Group 1,Jan:1,Feb:2,Mac:3,April:4,Total:5";

and then use print_r().Then try use for loops....test it!is really fun!haha

 N/NOTE:try it!

Thursday, February 17, 2011

How-to-know-what-type-of-browser-the-users-use

Assalamu'alaikum and selamat sejahtera

So,PHP also provide a function that detect the type of the browsers.Well,this function usually use by administrator or some organization,just to analysis what type of browser that mostly used.So,it is just a additional knowledge only.Who know,maybe this function can be very beneficial to us.

The code:

<?PHP
$browser = $_SERVER["HTTP_USER_AGENT"];
print $browser;
?>

N/NOTE:every knowledge....must been something useful to us.