Introduction
This code guides you in making your own currency, and is very simple.
Uses
I have made my own currency, WebCoin, in my early years of developing. (Available at web-coin.org as a reference) I got friends into it, but thats all, so I would like you to do the same!
Code
Making a sign up isnt that hard, nor is a sign in. But this article is about a currency, not a account system. To learn how to make a account system, refer to this: http://www.codeproject.com/Articles/684792/PHP-Flat-file-Beginner-Log-in-Script (Another article of mine)
There is something you must add to the sign up although. You must make a coin file to store the amount of coin they have:
if(!isset($_POST['uname']) || !isset($_POST['pass'])){
$ourFileName = "./users/". strToLower($_POST['uname']) ."_pass.txt";
$ourFileHandle = fopen($ourFileName, 'w') or die("can't open file");
fclose($ourFileHandle); $fopen = fopen($ourFileName, 'a');
fwrite($fopen, $_POST['pass']);
fclose($fopen);
$ourFileName = "./users/". strToLower($_POST['uname']) ."_coins.txt";
$ourFileHandle = fopen($ourFileName, 'w') or die("can't open file");
fclose($ourFileHandle);
$fopen = fopen($ourFileName, 'a');
fwrite($fopen, "3");
fclose($fopen);
$_SESSION['user'] = strToLower($_POST['uname']);
$_SESSION['logged'] = "yes";
echo "You are now a member, please login.";
If you hadn't noticed, there is a whole bunch of strToLower functions, this is so if someone sends coins to somone called "EXAMPLE" but the username is "example" it gets sent to the right person.
So there, they sign up is fixed. I'm going to assume youve got the rest of the account stuff finished.
Now, lets see some code that displays a users coins:
$myFile = "./users/". $_SESSION['user'] ."_coins.txt";
$fh = fopen($myFile, 'r');
$theData = fread($fh, filesize($myFile));
fclose($fh);
echo $theData;
What's a currency without sending coins?
This is a very long part, i will do my best to explain with comments.
$myFile = "./users/". $_SESSION['user'] ."_coins.txt";
$fh = fopen($myFile, 'r');
$coins = fread($fh, filesize($myFile));
fclose($fh);
$usercoins = "./users/". $_SESSION['user'] ."_coins.txt";
$sendcoins = $_POST['amount'];
$sendto = "./users/". strToLower($_POST['runame']) ."_coins.txt";
$exists = file_exists($sendto);
if($sendcoins < 0){
$sendcoins = $sendcoins * -1;
}
if(($exists)
and
($coins >= $sendcoins)
and
($usercoins != $sendto)){
$myFile = $sendto;
$fh = fopen($myFile, 'r');
$rcoins = fread($fh, filesize($myFile));
fclose($fh);
$coins = $coins - $sendcoins;
$myFile = $usercoins;
$fh = fopen($myFile, 'w') or die("can't open file");
$stringData = $coins;
fwrite($fh, $stringData);
fclose($fh);
$rcoins = $rcoins + $sendcoins;
$myFile = $sendto;
$fh = fopen($myFile, 'w') or die("can't open file");
$stringData = $rcoins;
fwrite($fh, $stringData);
fclose($fh);
$ip = $_SERVER['REMOTE_ADDR'];
$myFile = "transactionlog.txt";
$fh = fopen($myFile, 'a') or die("can't open file");
date_default_timezone_set('America/New_York');
$date = date('M/d/y H:i:s');
$stringData = $_SESSION['user'] ."[". $ip ."] >". $sendcoins ."> ". $_POST['runame'] ." @ ". $date ."\n";
fwrite($fh, $stringData);
fclose($fh);
echo "<br /> Your transaction was successful.";
}else{
echo "<br />There was an error processing your coins.";
}
I think you just made your own currency! Or maybe not. It's your job to outline the html of the site. Happy Coding!
Conclusion
You just recieved the code for a currency, but its your job to add a nice look to the site! Please do leave your currency in the comments!
History
V.1 - Creation