PHP 比特币从表单验证

PHP Bitcoin validate from form

我想从表单中验证比特币地址。如果有人输入无效地址,它应该回显 "Please enter valid address"。如果地址有效,脚本应该将其添加到数据库中。 我有这个代码。我只需要验证功能。

      <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);
          }
       ?>

使用 https://github.com/LinusU/php-bitcoin-address-validator 几次。没有繁琐的工作解决方案,使用简单的函数调用程序来处理比特币地址验证。

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.

安装

GitHubcomposer require linusu/bitcoin-address-validator

下载

使用

use \LinusU\Bitcoin\AddressValidator;  

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

代码用法

<?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');
        }
    }

?>