How to Create a Blockchain with PHP?

CluxCode
3 min readApr 8, 2022

--

Blockchain With PHP

Yo guys, what's up.

I know this topic is a bit weird but I am getting bored creating websites with PHP that's why I thought to create Blockchain with PHP.

Introduction

Blockchain is the most used tech nowadays, it is the latest and most secure tech.

Use cases of Blockchain

There are many uses of Blockchain

  1. Messaging App
  2. Healthcare App
  3. Transaction System
  4. Secure data storing
  5. and many more…

What You are Learning with this Tutorial

  1. Basics of PHP
  2. PHP Classed and Functions
  3. Working of Blockchain
  4. Working Ok POW (Proof of Work)

So let’s get started….

Requirements

That’s good if you know the basics of PHP and PHP must be installed on your device. To install PHP just simply download XAMPP or WAMP.

Setup

If you are using XAMPP then create a folder in “htdocs” folder, I am creating a folder blockchain in “htdocs” then create a folder blockchain.php in this folder.

Open XAMPP and run Apache Web Server then open “localhost/blockchain/blockchain.php” on your preferred browser.

Creating Blockchain Class

Define blockchain class

class Blockchain {}

Then define variables - chain, and difficulty

public chain
private difficulty

It will look like this

Define construct function

In this function, we are going to define the chain variable and set the difficulty to find the hash and after that, we are going to create a genesis block;

function __construct() {
$this->chain = [] // empty
$this->difficulty = 4 // preffered to set between 3 - 6
$this->createBlock(1, '0'); // genesis block
}

Define createBlock function

The example block looks like this

create a createBlock function with parameters — proof and previous_hash

function createBlock($proof, $previous_hash){
$block = array(
'index' => $this->get_chain_length() + 1,
'timestamp' => time(),
'proof' => $proof,
'previous_hash' => $previous_hash
);
array_push($this->chain, $block); // add block to the chain
return $block;
}

after that we need to define get_chain_length function

Define get_chain_length function

This will return the current chain index

function get_chain_length(){
$i = 0;
foreach($this->chain as $ch){
$i += 1;
}
return $i;
}

Now we need the previous block to hash it and create a new block

Define print_previous_block function

This will return the current block

function print_previous_block() {
return $this->chain[$this->get_chain_length() - 1];
}

Now we have to hash the current block to create a new block

Define hash function

This will hash the block

function hash($block){
return hash('sha256', json_encode($block));
}

Define proof_of_work function

We have to validate or mine the block using the sha256 hash algorithm. If you set the difficulty to 4 then we have to hash the new_proof which is by default set to 1 and previous_proof which we will get from the previous block, then we have to hash the square of new_proof minus the square of previous_proof if the hash contains 4 zeros in the start then we will get the new_proof as correct else we will increment the new_proof till we get 4 zeros.

Now you can mine the block and create new block

Define mine function (outside class)

Complete Code

The final code will look like this

Response

New Block has been created
Index: 2
Timestamp: 1649453381
Proof: 533
Previous Hash: f345ea860cb2c5fb4e6c5845ecb2bce3a09512fb7187fb60f5c7d6e85075442f
New Block has been created
Index: 3
Timestamp: 1649453381
Proof: 45293
Previous Hash: 26bcd51f317c9258beb826912184c4a6f42b4e1c17f8f41cf05b40dfee7ec62f

In this tutorial, you learned about basic blockchain and PHP classes and functions. So please like and follow my page, by doing that I will get appreciation to post more like this.

Thank You

--

--

CluxCode
CluxCode

Written by CluxCode

🐳 Crypto Explorer and Developer, Follow my page to learn about blockchain and defi || Follow me on Twitter - https://twitter.com/ktshacx

Responses (5)