Showing posts with label function. Show all posts
Showing posts with label function. Show all posts

Tuesday, 1 April 2014

MD5 encryption In php

MD5 hash in php
MD5 Encryption in php or generating MD5 hash from a string. Now If you don't know what is MD5 ? MD5 is a form of encryption and hash is generated from a string that's given in php we had function called md5(); if you given a string here it will convert MD5 hash.

code for MD5 encryption example(Screenshot1)

MD5 Encryption in php
Screenshot1

<?php



$string = 'password';

$string_hash = md5($string);



echo $string_hash;



?>

Output (Screenshot2)
MD5 encryption in php
Screenshot2
We creating a string like password saving inside the variable called $string. $string = 'password'; giving value of password. Now if you want to encrypt this we could simply create new variable $string_hash i will make this equal to md5 and inside a bracket i am going to supply the $string the data that i want to convert or encrypt. echo out the $string_hash. You can see screenshot2 a password what it looks like it when it be hashed.

5f4dcc3b5aa765d61d8327deb882cf99 is the hash presentation of the string password. However much be refresh it will remain same. We called this one way encryption this $string_hash can not be un-encryption back no more text. We can only do that we can take a input and match to the encrypted data.

For example you had stored a password in database with encrypted value user typed and say mohammed is the password the encrypted data would not match to the password. Therefore password would be incorrect.

Now Let so You example by html form. I am gonna create new file copy and paste 5f4dcc3b5aa765d61d8327deb882cf99 this hash over into file we gonna called this hash.txt. So, obliviously it is unsecure way of saving password because someone could access it. However Now from index.php let the user enter value so we gonna create a form that allows the user to enter value the form will submit we gonna open hash.txt we gonna look content and we gonna see if password match hash string must be equal to password means if we type mohammed password will be incorrect if we type password it will be correct input.


code for Example MD5 Encryption


<?php



if (isset($_POST['user_password']) && !empty($_POST ['user_password'])) {

$user_password = $_POST['user_password'];



$filename = 'hash.txt';

$handle = fopen($filename,'r');

$file_password = fread ($handle,filesize($filename));



if ($user_password==$file_password) {

echo 'password ok!';

}else {

echo 'Incorrect password.';

}





}else {

 echo 'Please enter a password.';

}

?>

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

password: <input type="text" name="user_password"><br><br>

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

</form>

Output 


MD5 Encryption in php
Screenshot3
MD5 encryption in php
Screenshot4
MD5 Encryption in php
Screenshot5
MD5 In php
Screenshot6
MD5 in php
Screenshot7
Creating from with form action with index.php the method is going to be POST remember posting a password so want to keep outside so we use POST. When using login forms in your web application you must sure you must use POST method because you got lot of data in process as well as its going clean as possible. So, Now create label called password: with text called password with input type must be password but purpose of this article we gonna use text so we can see the text what we had written in input box. name of its going to be user_password then creating submit button with value be submit. You can see screenshot.

Then with if condition we will check whether password is correct and form is be submitted. With isset function we will check whether form has been submitted with !empty function also we are checking that form is submitted is not empty It should filled. Now we will open up thid file called hash.txt and we want to grape value and we had to compare with the user input value. Then we will open file with fopen function by file handling in php. We are opening file to read the data in so we use r in file handling in php. Then we will convert user input into md5 function to check whether our password is correct in md5 function is equal same therefore we will check the user input value convert it to md5 function and will check with aur encrypted data which we had already converted and saved to hash.txt.

Now why we use password hashes in first place this md5 encryption whats the point using it. Why we need md5 function used ? . The reason is md5 function is not most secure form of hashing and is also not the most secure of encryption. It can be crack easily by checking. Lets say you had a database setup and there were lot's of username and password means you had a lot's of user on your website and you using an database now what happen's some one goes unauthorized access to your database what going happen is they are going to able to see your passwords if they are un-encrypted. If someone can view users password.

For example Screenshot6 and Screenshot7

In screenshot6 database i can easily use users password and username and login into it and In screenshot7 its difficult to anyone to login easily They can not convert MD5 function encrypted data because MD5 is one way encryption. There is no way that you can return this to there value that we called one way encryption.
Read More

Wednesday, 26 March 2014

Creating Cookies with php

Creating Cookies with php programming Language. You may be seen cookies may be in your browser setting. I am Sure every one know What is Cookies are but how we actually use them and how useful are there in every day programming on websites. A cookies is a piece of data or a file that stored specific information and its unique to you and the website that you viewing. So, If you view on your browser a website that set a specific cookies and you access to that website depending on the expiration time cookies that piece of data will be relate. That will be relate to your computer to the website. So, This way website can store data about users or about your specific preferences on in there website and it can be stored on your computer for later access.


How we can go about setting cookies and why they useful now there are some of the reason why they are useful for is just storing data that's need to be used to be later day for long expression time. You may already seen my article setting session in php. Cookies again allow you to set a much longer expression date. So, sessions are  close as soon as your is close in the connection is short in the server. However with cookies we could store cookies for year.


Code for Creating Cookies With php 

Set.php (Screenshot1)

setcookie
set.php Screenshot1

<?php



setcookie('username','alex','time()+10')



?>

View.php (Screenshot2)

cookies with php
Screenshot2


<?php



echo $_COOKIE['username'];



?>

Output 

cookie php
Screenshot3
cookies in php
Screenshot4
view cookie php
Screenshot5
To set cookies we use setcookie function takes few arguments. Here i will show you few arguments. I will go through three main arguments and three main arguments we look at first one is the cookies name you are setting then the value of cookies want to store in the cookies and the expression date. The time in which the cookies going to be valid for. Now remember when we look at sessions we write like $_SESSION['name']='value'; and in Cookies we write like setcookie('name','value','time');. Here in this example i had set cookies in set.php with setcookie function with arguments name is username value is alex and the expression time is something that how much time we want to put for the cookie exit to the browser. Its value of time expression is in seconds. For set the time expression argument we need to use timestamps infact in every php when we need to use date time function we need timestamps to retrieve date time etc. 

If you don't know what is timestamps please read articles of Extra Tutorials on timestamps. Timestamps in php is basically around of seconds current day in seconds. So, We use time function to grap a timestamps. So, Here we had time() which will retrieve current time then we add more 10 seconds to delay the cookies.

Now in view.php i had echo out the viewing the cookies and you can see after 10 seconds cookies will be expired. After refresh output you can see how cookies had been expired. Because timestamps gone less then the current time. 

So, You can Use cookies in login system where you want to your user can access logged time interval after that you want to logout the user. So, How basically cookies are useful but remember cookies are stored on users computer. So, There are dangers to use things like sensitive data that you don't want to user to change for example you had a shopping card and you have total set in cookies.  You don't necessary want this unless you using some form of encryption some database used to store that information can be stored to the user computer and that can harm websites. 

Code for Unset Cookies with php

Set.php 




<?php



setcookie('username','alex','time()-10')



?>

View.php (Screenshot2)

Screenshot


unset cookies
Screenshot2
 Now For Unsetting the cookies there is not any function for unset or make sure cookies file must delete from the users computer is to subtract the seconds of the cookies. Now If you will set the cookies it will not set because it is unset. Now why you need to unset the cookies or what is need to unset the cookies we answers quite simple login system if you log you user storing file where they logged in cookies and you may be log in for 1000 seconds if you want to log them out you may have logout button now when i click logout button we need some way insuring that cookies is deleted and unset so, users no longer logged in so obviously clicking logout button we perform some kind of operation like subtrating seconds from timestamps to ansure that the cookies had unset and the users no longer logged in. So, thats the simple example for unset the cookies and that basically simple way.

Read More

Thursday, 13 February 2014

More On Error Reporting in php

Error Reporting
Error Reporting
This is a tutorial for Error reporting. Let’s Back to php.ini file(ScreenShot1).

More On Error Reporting in php
Screenshot1
Error Reporting value is E_All, So we are Reporting errors back to our self as we developing. What happen if i want to report errors Strict errors. Now E_Errors Does not Include E_Strict, So i will specify under error_reporting another value E_Strict (ScreenShot2).

error reporting
Screenshot2

error_reporting = E_ALL & E_STRICT


Now this will include errors of run-time notices, enable to have PHP suggest changes to your code which will ensure the best interchangeability and forward compatibility of your code. So basically giving us messages about code itself other then errors that you made be coming across  Now “&” sign is use for including and for not including “~” sign is used. (Screenshot3).

php code errors
Screenshot3

error_reporting = E_All & ~E_NOTICE 

If we don’t wanna Show E_NOTICE types of error we include “~”.This will show all errors, except for notice and coding standards warnings.So Its upto you what level errors you want to display if your codes having errors.If your application is big you can turn onhave a look. Now when u complete your program or applicationon web-server you have to turn error_reporting off
When u complete your program you need to :-

error_reporting=0;

Why the reason being is the errors shows on your pages specially in E_STRICT notices you don’t want to your users or viewers to tell about your errors notices and warnings. For security reasons also u had to turn off. For example if u including file or copying file and there u had errors its shows errors to visitors and they will understand your website structure.So, Best thing is to you put error_reporting=0 when u release your website to web-server to visitors. Now if u had paid hosting you will not have access to php.ini files for that there are some functions have look. What happen if we want to dynamically update error_reporting value inside php (Screenshot4), and (Screenshot5).

php codes error reporting
Screenshot4
error_reporting
Screenshot5
 Code Dynamically update error_reporting value inside php:-

Code Error reporting for E_ALL :-


<?php

error_reporting(0);

echo $var = 'Alex'

$var2 = 'billy';

?>




You can also use functions for writing dynamically update error_reporting value inside php

ini_set function
Code for ini_set function Screenshot6

<?php

ini_set('error_reporting',E_ALL);



echo $var = 'Alex'

$var2 = 'Billy';

?>


This is all About error_reporting how u can put errors for you how can you find errors for visitors.
Read More

Monday, 10 February 2014

Phpinfo function


phpinfo function
Phpinfo Function
The phpinfo function. We should detailed look to php installation so for example you may go later to look at GD library. Its a Image generate Images using its command-set its addition to php may not be included in original php installation. If some one ask you did you had kernel or GD images installed. You should know what its is its a Different library for php. You need to find a way to check every think in php installation and general information about php version some one must ask you or need to find out which version of php you using and different features of php version works with updated php version. Different features of php works with different versions of php. You must using older version of php and could not get some advantages of php updated features. so should check updated version. This stuff you can find out by phpinfo() function.So to call function its relatively simple Within php tags write phpinfo(); () Its a standard notation for calling function. Its a built-in function of php which is built ed in directory ofinstallations files.

Codes To Write phpinfo function :-


Code Phpinfo Function
Code PhpInfo Function
<?php
phpinfo();
?>

Output For Phpinfo Function

Phpinfo function
Output PhpInfo function Php



We can see php version 5.2.9 in output as you scroll down you will see many things enabled and all. Its a list of collection of our php installation  its having information about php core, apache , apache environmental  ftp support and many other stuff. Also gd libaray gd version and its also linked in apache. One thing you should always remember this file should not be upload on your server its available on your local machine. If you upload this file on server had change to hack your website. phpinfo file had lots of information about out php installation on our local machine from that data anyone can harm your files. If you had such file on server should delete it for security purpose.So phpinfo had an information about your php package.
Read More

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