Click here to Skip to main content
Click here to Skip to main content

Making a Currency in PHP (Basic)

, 8 Jul 2014 CPOL
Rate this:
Please Sign up or sign in to vote.
This code guides you in making your own currency, and is very simple.

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'])){  //Redirect somewhere } 

$ourFileName = "./users/". strToLower($_POST['uname']) ."_pass.txt"; //we edited this line a bit

$ourFileHandle = fopen($ourFileName, 'w') or die("can't open file");

fclose($ourFileHandle); $fopen = fopen($ourFileName, 'a');

fwrite($fopen, $_POST['pass']); //If i were you i would add md5 to that, with the md5() function but this is a beginner tutorial

fclose($fopen);
//this login is very begginerish, a file exist function is needed. use it like this:
//$exists = file_exists("./users/". strToLower($_POST['uname']) ."_coins.txt");
//if(!exists){ //Do your stuff } else { //Nope }

//Now we are starting to add some code

$ourFileName = "./users/". strToLower($_POST['uname']) ."_coins.txt"; 
//Make sure you create a directory called users
$ourFileHandle = fopen($ourFileName, 'w') or die("can't open file");
fclose($ourFileHandle);
$fopen = fopen($ourFileName, 'a');  
fwrite($fopen, "3"); //The 3 is how many coins/units they start off with, you can change that  
fclose($fopen);
$_SESSION['user'] = strToLower($_POST['uname']);  
$_SESSION['logged'] = "yes";
echo "You are now a member, please login."; //They should already be logged in, so you can 
//remove this

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:

//This is actually quite simple, but remember, before dealing with users accounts and data
//add this line before the <!DOCTYPE html> and <html> tags (with php tags): session_start()
//So, here we go: 
$myFile = "./users/". $_SESSION['user'] ."_coins.txt";
$fh = fopen($myFile, 'r');
$theData = fread($fh, filesize($myFile));
fclose($fh);
echo $theData; //Wrap this in some nice html!!

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"; //get the users coins
$fh = fopen($myFile, 'r');
$coins = fread($fh, filesize($myFile)); //this stores how many this person has
fclose($fh);
$usercoins = "./users/". $_SESSION['user'] ."_coins.txt"; //This stores the file as a variable
$sendcoins = $_POST['amount']; //Stores the amount to send
$sendto = "./users/". strToLower($_POST['runame']) ."_coins.txt"; //stores the file to send to
$exists = file_exists($sendto); //Here is that exist test i was talking about
if($sendcoins < 0){
    $sendcoins = $sendcoins * -1; //No coin theft here! (Protects agains getting negative coins)
    }
if(($exists) //If it exists
        and
    ($coins >= $sendcoins) //and if you have enough
        and
    ($usercoins != $sendto)){ //and if it isnt sending it to yourself
    $myFile = $sendto;
    $fh = fopen($myFile, 'r');
    $rcoins = fread($fh, filesize($myFile));
    fclose($fh);
    
    
    $coins = $coins - $sendcoins; //Subtract coins from your balance
    $myFile = $usercoins;
    $fh = fopen($myFile, 'w') or die("can't open file");
    $stringData = $coins;
    fwrite($fh, $stringData);
    fclose($fh);
    $rcoins = $rcoins + $sendcoins; //Add it to their balance
    $myFile = $sendto;
    $fh = fopen($myFile, 'w') or die("can't open file");
    $stringData = $rcoins;
    fwrite($fh, $stringData);
    fclose($fh);
    $ip = $_SERVER['REMOTE_ADDR']; //Get their IP (For logging reasons)
    $myFile = "transactionlog.txt"; //Make sure to make this file, it will log all transactions
    $fh = fopen($myFile, 'a') or die("can't open file");
    date_default_timezone_set('America/New_York'); //Eastern time
    $date = date('M/d/y H:i:s'); //This is the date format
    $stringData = $_SESSION['user'] ."[". $ip ."] >". $sendcoins ."> ". $_POST['runame'] ." @ ". $date ."\n"; //username[1.1.1.1] >5> recipientusername @ date/time
    fwrite($fh, $stringData);
    fclose($fh);
    echo "<br /> Your transaction was successful.";
    
    
    }else{
    echo "<br />There was an error processing your coins."; //The user doesnt exist or is yourself
    }

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

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

G4mm4R4y
Team Leader Fallen Devs
United States United States
One of two leader of Team Fallen Devs, creators of FallenStore. FallenStore is the revolutionary iOS and soon-to-be android app that allows you to install other peoples own creations. http://www.fallenstore.tk/
~Kaito
Follow on   Twitter

Comments and Discussions

 
BugInjection Security Hole PinmemberChad3F15-Jul-14 12:53 
GeneralRe: Injection Security Hole PinprofessionalG4mm4R4y22-Jul-14 11:39 
AnswerRe: Injection Security Hole PinmemberChad3F22-Jul-14 13:55 
GeneralRe: Injection Security Hole PinprofessionalG4mm4R4y22-Jul-14 17:41 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.150123.1 | Last Updated 8 Jul 2014
Article Copyright 2014 by G4mm4R4y
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid