Click here to Skip to main content
15,886,766 members
Articles / Web Development / HTML
Tip/Trick

Making a Currency in PHP (Basic)

Rate me:
Please Sign up or sign in to vote.
5.00/5 (3 votes)
8 Jul 2014CPOL1 min read 11.9K   2   4
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:

PHP
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:

PHP
//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.

PHP
$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)


Written By
Team Leader Carina Development
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
BugInjection Security Hole Pin
Chad3F15-Jul-14 11:53
Chad3F15-Jul-14 11:53 
GeneralRe: Injection Security Hole Pin
G4mm4R4y22-Jul-14 10:39
professionalG4mm4R4y22-Jul-14 10:39 
AnswerRe: Injection Security Hole Pin
Chad3F22-Jul-14 12:55
Chad3F22-Jul-14 12:55 
Assuming it was stored as-is (i.e. no md5() or anything), and the hack-uploaded content contained something like commercial copyrighted software or underage porn. Depending on jurisdiction, the lawyers / government may tend to punish first and ask questions later. Even if eventually proven innocent, the time and money spent trying to defend yourself is in itself effectively a punishment.
GeneralRe: Injection Security Hole Pin
G4mm4R4y22-Jul-14 16:41
professionalG4mm4R4y22-Jul-14 16:41 

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

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