Create a unique address, log it to a DB with a reference to the user id. The accounts feature was deprecated in 2016
Use the walletnotify feature in your bitcoin.conf instead of a cronjob. This will call a file that can take the transaction id(passed as %s
) as an input whenever a new transaction is received in your mempool and again once confirmed. Use the transaction id to find the output address(es), if they match an address in your DB credit that account with the value sent to that output.
Example of passing the transaction id into a GET of "tx"
walletnotify = curl http://mysite.com/newdeposit.php?tx=%s
Example of what your newdeposit.php file could look like
<?php
//db connection
$conn = mysqli_connect("localhost", "root", "password", "database_name");
if (mysqli_connect_errno()){
echo "Connection to DB failed" . mysqli_connect_error();
}
//bitcoin node connection
require("easybitcoin.php");
$bitcoin = new Bitcoin("someusername", "somepassword");
$tx = $_GET['tx'];
//sanitize
$tx = mysqli_real_escape_string($conn, $tx);
$getTrans = $bitcoin->gettransaction($tx);
$confirmations = $getTrans["confirmations"];
if($confirmations !== 1){
die();
} else {
//loop through the outputs
$countDetails = count($getTrans['details']);
for($i=0;$i<$countDetails;$i++){
$getAddress = $getTrans['details'][$i]['address'];
$getReceive = $getTrans['details'][$i]['category'];
if($getReceive == "receive"){
$checkAddy = mysqli_query($conn, "SELECT * FROM users WHERE DEPOSIT_ADDRESS = '$getAddress'");
$doCheckAddy = mysqli_num_rows($checkAddy);
if($doCheckAddy == 1){
$amount = $getTrans['details'][$i]['amount'];
$amount = $amount * 100000000;
$updateBalance = "UPDATE users SET BALANCE = BALANCE + '$amount' WHERE DEPOSIT_ADDRESS = '$getAddress'";
$doUpdateBalance = mysqli_query($conn, $updateBalance);
}
}
}
}
?>
As for re-launching after a crash you could look into using a bash script on a cronjob. I found the below example on bitcointalk
#!/bin/sh
ps auxw | grep bitcoind | grep -v grep > /dev/null
if [ $? != 0 ]
then
/bitcoind start > /dev/null
fi
EDIT: It's important to note this is not meant as a hack-proof solution, but a simple example to show how you could approach your solution.