简体   繁体   中英

PHP Bitcoin validate from form

I want to validate Bitcoin address from form. If someone enter invalid address it should echo "Please enter valid address". If address is valid, script should add this to database. I have this code. I only need validate function.

      <form method="post" action="index.php">
        <input type="text" name="address" placeholder="Address">
        <input type="checkbox" name="terms"> <label for="terms">I'm accepting the <a href="#terms">terms of use</a></label><br>
        <input type="submit" value="Submit">
      </form>
      <?php
        if(isset($_POST['address']) & isset($_POST['terms']))
        {
            include 'connect.php';
            $address=$_POST['address'];
            $date=date("Y-m-d");
            $request="INSERT INTO bitcoin (address, date) VALUES ('$address','$date')";
            mysqli_query($connect, $request) or die ("Error while writing to database! Try again...");
            mysqli_close($connect);
          }
       ?>

Being using https://github.com/LinusU/php-bitcoin-address-validator few times. No hustle working solution, with easy function caller to handle Bitcoin address validation.

isValid($addr, $version)

$addr: A bitcoin address $version: The version to test against, defaults to MAINNET Returns a boolean indicating if the address is valid or not.

INSTALL

Download from GitHub or composer require linusu/bitcoin-address-validator

USE

use \LinusU\Bitcoin\AddressValidator;  

// This is a valid address and will thus return true.
AddressValidator::isValid('1AGNa15ZQXAZUgFiqJ2i7Z2DPU2J6hW62i');

USAGE ON CODE

<?php 

    include 'connect.php';
    include 'LinusU\Bitcoin\AddressValidator.php';

    use \LinusU\Bitcoin\AddressValidator;  


    if(isset($_POST['address']) & isset($_POST['terms'])){
        $address=$_POST['address'];     
        if(AddressValidator::isValid($address)){
            // add
            $date=date("Y-m-d");
            $request="INSERT INTO bitcoin (address, date) VALUES ('$address','$date')";
            mysqli_query($connect, $request) or die ("Error while writing to database! Try again...");
            mysqli_close($connect);
        }else{
            // not valid
            throw new Exception('Invalid Bitcoin Address');
        }
    }

?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM