Showing posts with label database. Show all posts
Showing posts with label database. Show all posts

Friday, 25 April 2014

An Introduction to database

An Introduction to database
We are going to talking about databases. Now in this article. I am not going to teach you how to setup the database or how to write in database etc. But we just going to be talk about why database important in most real life applications. I am also going to talking about mysql database. Why we need to use the database now you may come across the database before access another database applications so where you can store values with pacific column name so, you can store this values as a array also. you can also do this with Microsoft excel. If you already seen file handling in php. You know we had store things hit counter value in a file and then we had open the file we read the value in and close the file we may then open the value of the file applicant to rewrite another value. This uses huge amount of strength and complication in as well on whole process. Now let say we want to write a value to a file what we doing is we were opening up that file so, reading file data we are then writing a value to a file. What happen if we want to say to include username , password , firstname, surname , website you may have written 100s of records with people username the password. You may want to create some type of login system for this. Now you not going to do file to store this data. You will use database to store which is more secure and quickly accessible. In this case we will use mysql database connection because its one of the fastest database available to you.

Mysql Database
Screenshot1


Mysql database is hosted on to the server. So, it will have its own port number If you open xampp control panel application Screenshot1. You will see we had process running mysql and its running at this moment. From php you don't need to worry about accessing mysql database on to the server. But we do use some functions to connect our database. then we shall connect to our server. Then we use another functions connect to the databases itself. So, you can have variety of amount of databases within mysql. Then you had variety of table inside databases. Now database let say user's Example of user's database.


Example of user's database


database: tutorialsjackpot
--------------------------
table: Users
------------

id    username   password  firstname  surname  website
1     hello          hello123  hello          tell         hello.com
2     How        how456    how's  you         how.com

Here we had database structure in mysql database in users is the table name. Our database name is tutorialsjackpot and the table name is users. Now the way we can access this is we can select our table. Now we are storing the users into and then selecting different value's. So, i can execute something called query which will select specific amount of data from specific row. You also had field called id which is auto-incremental its increment every time. So, each record created. Its automatically increments and this add  unique property and we can define primary key for this id. Primary key is the unique figure unique column in your database.

Now let say i want to select some data so we can say select the username from 'users' where the id is equal to 2. Here users is the table name and this is what we can called query. Its just an English language. But what i had done here is construct general idea something called query about. query does allow us specific syntax to select information from parts. So, what we are doing here is we are selecting the username in column of username from users table where id is equal to 2 means it will give use how. So, this query return the value hello So you can see advance storing inside the database it may look complicated. At this state but when you start using inside php. Php makes it very easy for you to extract data even insert data and perform idea of queries. This done from mysql database. you can also have other tables.


Table: uploads
---------------

id     user_id      location
1      1               logo.jpg
2      1          php.jpg
3      2          art.jpg

we had table say uploads we may allow users to upload files. Now why it is going to be useful. Just add another columns called id, user_id and location. So, the id will again will be auto-increment. But this time user_id What we are doing here is we referencing user_id into the table of users columns id because that is the users id so you can called this columns name anything you like. But i recommended similar structure. So, User id met be 1 and the location of the file which been uploaded. may be logo.jpg and another file is also uploaded by same user so user_id while be 1 and name of the file may be php.jpg. and another file is uploaded by second user hello which user id is 2 so user_id referencing is 2 location art.jpg. So, now what we can do is we can perform the query to say let say user is logged in hello. What we would do we have its id to select things relative hello the id is 1 this is unique to the user hello. So we can say select column name from table name where column name is field value

Example

select location from 'uploads' where user_id is 1


Now What this query here will do it returns values where ever the user_id equals 1. SO, What essentially return.


Output of the query

1      1            logo.jpg

2      1       php.jpg

Now we had selected locations only So, what it will do is it will only print back logo.jpg , php.jpg data. So, what we can do is to speck this out we can select a specific column name So, we can save our time in a program our load time and we can save resources. So, Its essentially for using  database the reason we use its quick and easy way to add information's, that can be easily retrieve we don't have to deal with text files. we don't had to deal with space between file, or commas between characters, hello,bye,goodmorning, So, retrieve from the file we need to open file. we need to use explode function in php to read all this comma's,and to put this into a array. But in database you are creating something you can visualize how extract specific data. You can also update data very very easily with just a single query. So, that was the introduction to databases. Like i said we are going to be using database MySQL and SQL stands for structured query languages. Now was talking about queries. SQL stands for queries. we had structured query which to retrieve, update, delete, create anything inside our table. MYSQL is the name of the databases. we are going to be using.


Read More

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

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