Building Blockchain using PHP

Building Blockchain in php
Building Blockchain

Blockchain

Building Blockchain using PHP

We are going for building Blockchain in PHP programming Language, Blockchain in itself is a very simple concept, it’s a very simple data structure,  cryptocurrencies are complicated but the Blockchain is not, the reason they are complicated is the consensus algorithm, their mining mechanism and the everything that is around but Blockchain in itself is very easy to understand, what you need to know before you can actually understand what Blockchain is and how it works, you need to know what hashes are? and how they work?

So a hash is basically a digital signature of some kind of data, for example I can take a movie and I can hash it and get a digital signature, I can take an email and I can hash it and get a digital signature, I can take a word and I can hash it, you can take whatever data and hash it and you get the digital signature for that data.

How this hash actually works? Lets dive into it.

We will Build Blockchain in PHP, it will be very simple and if you know programming you can do that in another language and if you don’t know programming, I think you still will be able to understand roughly how this works so let’s talk about hashes.

Learn More: Complete Basic Blockchain Development in JavaScript

As you can see we have two lists we have one list that contains A,B,C and we have a second list that also contains A, B and C so this is the data, this is basically the strings we’re trying to hash so let’s see what kind of  digital signature we will get from list 1 & 2.

Output:

You can see as we get these two hash strings or digital signatures, these are identical for these two lists.

If I now change anything in list 1 for example –

Now I run it again

Output:

You see that I get a completely different digital signature so if I just make a small amendment I just meddle with the data, I try to change anything in this list, I will get a completely different digital signature and that is the foundation of a Blockchain Technology because a Blockchain is a list of blocks basically a chain of blocks and each block holds the digital signature of the previous block and the digital signature of the next block is based upon the digital signature of the current block so they are always together the next signature will be based on the current signature and the current signature is based on the previous signature so if you change anything back in the past you will break all of the signatures they will look completely different.

Learn More: What is Blockchain?

Let’s actually get into our Blockchain coding so the first thing we need to do is talk about our Block class, our Block in this case will be very simple, it will only contain three things it will contain the list of transactions that happened in that block, it will contain the previous hash or the digital signature of the previous block and it will also contain the hash of the current of itself and this hash will be based on the transactions and the previous hash so if anyone would change anything in the previous block the digital signature of the current block will change and that changes the digital signature of the next blocks.

Read More: What is Decentralization in Blockchain?

Create an empty PHP project using your favorite IDE, I am using XAMPP for my localhost environment.

Now create a new file block.php, and put the following code snippet in it.

Now create another file called index.php and use block.php class to create your blocks.

Index.php

Output:

See that the current hash will be based on the previous hash, if you change any of the previous transaction your all digital signatures will effect, for example I changed 11 to 12 in the genesisTransaction it will output completely different like:

Learn More: Why is Trade Finance a hot use-case for Blockchain

Output:

You can see a change in any of these transactions, in any of the data in the one block will propagate and change all of the digital signatures in the future chains and that is how Blockchain works, because if I change something everyone else will see that my digital signatures are completely different from everyone else’s so that means that I have cheated, so if I just tell everyone that hey Person A  gave me this many bitcoins back then and here is the Blockchain that confirms that everyone will take a look at it and say well I will know this blockchain doesn’t confirm that because we have our own versions of the Blockchain and the digital signatures are completely different.

I hope you understood the basic concepts of hashing of linking blocks together via their digital signatures. Implement this example in PHP and let me know your thoughts in the comments section below.

I am a Sr. Android App & Web Developer having 5 plus years of extensive hands-on experience in Object Oriented Programming, Database Design and Implementation using Agile & UML. Pro in Yii2 PHP Framework, Android App Development, MYSQL & a Blockchain enthusiast.

5 Comments

5 Comments

  1. Pingback: Building a Blockchain using PHP - Much Doge News .comMuch Doge News .com

  2. Techval

    July 1, 2018 at 7:03 am

    Interesting read. Now I understand what blockchain is all about.

  3. P RIley

    September 28, 2018 at 8:07 pm

    Could you please tell me what implementation tests you ran on your blockchain PHP code. How did your verify and validate the code works with Ethereum or the other blockchains (Hyperledger e.g.)

  4. Waris

    October 10, 2018 at 11:19 am

    As per your above example both the outputs before and after change is similar

    Genesis Block: d9c559b57e148b19802d8e70555f0303
    Block 1: 29e9e5a309aae81243fdf73112253c74
    Block 2: fbff8f664f6b4069bf7288cb1b86cb3a

    It should be different.please update your post

    • Husnain Aslam

      October 10, 2018 at 12:49 pm

      Thank you for pointing this out, It was a typo and fixed now.

Leave a Reply

Your email address will not be published. Required fields are marked *

More in Blockchain

Recent Posts

Subscription

Categories

Tags

To Top