Full Point System


First off the SQL
PHP Code
  1. ALTER TABLE `members` ADD `pin` VARCHAR( 255 ) NULL,
  2. ADD `has_bank` INT( 1 ) NOT NULL default '0',
  3. ADD `points` INT( 11 ) NOT NULL default '50';
  4.  
  5. CREATE TABLE `banks` (
  6. `id` INT( 11 ) NOT NULL auto_increment,
  7. `username` VARCHAR( 255 ) NOT NULL,
  8. `points` INT( 11 ) NOT NULL default '0',
  9. `pin` VARCHAR( 255 ) NULL,
  10. PRIMARY KEY(`id`)
  11. );


Add this to your functions.php if you have one. If not, add to your config.php

PHP Code
  1. <?php
  2. function secure($var){ //secure pin code function
  3. $p = $var; //yawn? lets give it a simpler name :D
  4. $md5 = md5(md5(md5(md5(md5($p))))); //md5 the pin/variablke
  5. $sha1 = sha1(sha1(sha1(sha1(sha1($p))))); //sha1 the pin/text
  6. $compile = "xX-$md5-$sha1-Xx"; //combine them
  7. $secure_full = md5(sha1($compile)); //md5 and sha1 them :D
  8. return $secure_full; //return the code
  9. } //end function
  10. function checkpin($pinhash){ //check our pin
  11. $pin = $pinhash; //easier name
  12. $pins = mysql_query("SELECT * FROM `members` WHERE `pin` = '$pin';") or die(mysql_error()); //get pins from members table
  13. if(empty($pin)){ //UHOH they left it empty
  14. return false; //BAD! you fail =]
  15. } //end empty check
  16. if(mysql_num_rows($pins) == 0){ //no pins found =(
  17. return false; //fail.
  18. } //end
  19. if(mysql_num_rows($pins) >= 1){ //more then 1 found :D
  20. return true; //true!!! pass!!!!
  21. } //end
  22. } //end function
  23. ?>


Call this file bank.php. it will be the only file you will need for this tutorial.
PHP Code
  1. <?php
  2. session_start(); //allow sessions
  3. require("config.php"); //get configuration
  4. if(!$logged['username']){ //not logged in
  5. print "You Must Be Logged In For Use of This Function!";
  6. }else{ //they are logged in
  7. if($logged['has_bank'] == 0){ //user doesn't have a bank account
  8. if(!$_POST['getbank']){ //form not submitted
  9. print "<form method=\"post\">
  10. <strong>Pin Code</strong>
  11. <input type=\"password\" size=\"15\" name=\"pincode\" maxlength=\"4\" />
  12. <input type=\"submit\" name=\"getbank\" value=\"Get Bank!\" />
  13. </form>";
  14. }else{ //form was submitted
  15. $pin = (int)$_POST['pincode']; //this is our un-encrypted pin
  16. $enc_pin = secure($pin); //new pin for database
  17. if(empty($pin)){ //it was empty =(
  18. print "You Must Enter a 4 Digit Pin Code!";
  19. }else{ //maybe not..
  20. if(strlen($pin) < '4' || strlen($pin) > '4'){ //too long or too short
  21. print "Pin Must Be 4 Digits Long!";
  22. }else{ //not.
  23. $update_user = mysql_query("UPDATE `members` SET `has_bank` = '1', `pin` = '$enc_pin' WHERE `username` = '$logged[username]';") or die(mysql_error()); //update the user
  24. $insert_bank = mysql_query("INSERT INTO `banks` (`username`,`pin`) VALUES ('$logged[username]','$enc_pin');") or die(mysql_error()); //give them a bank account
  25. print "Bank Setup Successfully!";
  26. }
  27. }
  28. }
  29. }else{
  30. switch($_GET['page']){
  31. default:
  32. $getBank = mysql_query("SELECT * FROM `banks` WHERE `username` = '$logged[username]';"); //get the bank for the logged user
  33. $bArray = mysql_fetch_array($getBank); //array the data.
  34. print "<p>
  35. Welcome to the bank $logged[username]
  36. You Have $logged[points] in your hand and $bArray[points] in your bank account for a total of ".($logged[points] + $bArray[points])." points.
  37. What would you like to do?</p>
  38. <table width=\"300\">
  39. <tr>
  40. <td align=\"center\" valign=\"middle\">
  41. <a href=\"bank.php?page=withdraw\">Withdraw Points</a>
  42. </td>
  43. <td align=\"center\" valign=\"middle\">
  44. <a href=\"bank.php?page=deposit\">Deposit Points</a>
  45. </td>
  46. </tr>
  47. <tr>
  48. <td align=\"center\" valign=\"middle\">
  49. <a href=\"bank.php?page=changepin\">Change Pin</a>
  50. </td>
  51. <td align=\"center\" valign=\"middle\">
  52. <a href=\"bank.php?page=forgotpin\">Forgot Pin</a>
  53. </td>
  54. </tr>
  55. </table>";
  56. break;
  57. case 'withdraw': //the withdraw points page
  58. $getBank = mysql_query("SELECT * FROM `banks` WHERE `username` = '$logged[username]';"); //get the bank for the logged user
  59. $bArray = mysql_fetch_array($getBank); //array the data.
  60. if(!$_POST['withdraw']){ //form not submitted
  61. print "<form method=\"post\">
  62. You Have $bArray[points] In Bank!
  63. <strong>Withdraw Amount</strong>
  64. <input type=\"text\" name=\"amount\" value=\"0\" />
  65. <strong>Your Pin</strong>
  66. <input type=\"password\" name=\"mypin\" size=\"15\" />
  67. <input type=\"submit\" name=\"withdraw\" value=\"Withdraw Points!\" />
  68. </form>";
  69. }else{ //it was
  70. $pin = (int)$_POST['mypin']; //our pin
  71. $withdrawpoints = (int) $_POST['amount']; //ammount wanted
  72. $errors = array(); //errors
  73. if(empty($pin)){ //empty pin
  74. $errors[] = "You Must Enter your 4 Digit Pin Code!";
  75. }
  76. if(empty($withdrawpoints)){ //no moneyto withdraw
  77. $errors[] = "You Must Enter an Amount of Money!";
  78. }
  79. if($bArray['points'] < $withdrawpoints){ //not enough points
  80. $errors[] = "You Don't Have That Much Money In Your Bank!";
  81. }
  82. if(!checkpin(secure($pin))){ //invalid pin code
  83. $errors[] = "Invalid PIN Code!";
  84. }
  85. if($bArray['pin'] != secure($pin)){ //wrong pin code:O
  86. $errors[] = "Invalid Bank PIN!";
  87. }
  88. if($withdrawpoints < 0){
  89. $errors[] = "Invalid Amount of Points!";
  90. }
  91. if(count($errors) > 0){ //errors found
  92. foreach($errors as $err){ //loop for all errors
  93. print $err.''; //print the error
  94. } //end loop
  95. }else{ //none found
  96. $newbankpoints = ($bArray['points'] - $withdrawpoints); //new bank amount
  97. $newhandpoints = ($logged['points'] + $withdrawpoints); //new hand amount
  98. $update_hand = mysql_query("UPDATE `members` SET `points` = '$newhandpoints' WHERE `username` = '$logged[username]';") or die(mysql_error());
  99. $update_bank = mysql_query("UPDATE `banks` SET `points` = '$newbankpoints' WHERE `username` = '$logged[username]';") or die(mysql_error());
  100. print "$withdrawpoints Points has been withdrawn.";
  101. } //end errors count
  102. } //end form check
  103. break;
  104. case 'deposit': //depposit points page
  105. $getBank = mysql_query("SELECT * FROM `banks` WHERE `username` = '$logged[username]';"); //get the bank for the logged user
  106. $bArray = mysql_fetch_array($getBank); //array the data.
  107. if(!$_POST['deposit']){ //form not submitted
  108. print "<form method=\"post\">
  109. You Have $logged[points] In Hand!
  110. <strong>Withdraw Amount</strong>
  111. <input type=\"text\" name=\"amount\" value=\"0\" />
  112. <strong>Your Pin</strong>
  113. <input type=\"password\" name=\"mypin\" size=\"15\" />
  114. <input type=\"submit\" name=\"deposit\" value=\"Deposit Points!\" />
  115. </form>";
  116. }else{ //it was
  117. $pin = (int)$_POST['mypin']; //our pin
  118. $depositpoints = (int) $_POST['amount']; //ammount wanted
  119. $errors = array(); //errors
  120. if(empty($pin)){ //empty pin
  121. $errors[] = "You Must Enter your 4 Digit Pin Code!";
  122. }
  123. if(empty($depositpoints)){ //no money to deposit
  124. $errors[] = "You Must Enter an Amount of Money!";
  125. }
  126. if($logged['points'] < $depositpoints){ //not enough points
  127. $errors[] = "You Don't Have That Much Money In Your Hand!";
  128. }
  129. if(!checkpin(secure($pin))){ //invalid pin code
  130. $errors[] = "Invalid PIN Code!";
  131. }
  132. if($bArray['pin'] != secure($pin)){ //wrong pin code:O
  133. $errors[] = "Invalid Bank PIN!";
  134. }
  135. if($depositpoints < 0){
  136. $errors[] = "Invalid Amount of Points!";
  137. }
  138. if(count($errors) > 0){ //errors found
  139. foreach($errors as $err){ //loop for all errors
  140. print $err.''; //print the error
  141. } //end loop
  142. }else{ //none found
  143. $newbankpoints = ($bArray['points'] + $depositpoints); //new bank amount
  144. $newhandpoints = ($logged['points'] - $depositpoints); //new hand amount
  145. $update_hand = mysql_query("UPDATE `members` SET `points` = '$newhandpoints' WHERE `username` = '$logged[username]';") or die(mysql_error());
  146. $update_bank = mysql_query("UPDATE `banks` SET `points` = '$newbankpoints' WHERE `username` = '$logged[username]';") or die(mysql_error());
  147. print "$depositpoints Points has been deposited.";
  148. } //end errors count
  149. } //end form check
  150. break;
  151. case 'changepin': //change pin page
  152. $getBank = mysql_query("SELECT * FROM `banks` WHERE `username` = '$logged[username]';"); //get the bank for the logged user
  153. $bArray = mysql_fetch_array($getBank); //array the data.
  154. if(!$_POST['changepin']){ //form not submitted
  155. print "<form method=\"post\">
  156. <strong>Current PIN Code</strong>
  157. <input type=\"password\" name=\"cur_pin\" maxlength=\"4\" />
  158. <strong>New Pin</strong>
  159. <input type=\"password\" name=\"new_pin\" maxlength=\"4\" />
  160. <strong>Verify New Pin</strong>
  161. <input type=\"password\" name=\"ver_pin\" maxlength=\"4\" />
  162. <input type=\"submit\" name=\"changepin\" value=\"Change Pin!\">
  163. </form>";
  164. }else{ //or was it..
  165. $pin = (int) $_POST['cur_pin']; //original pin
  166. $newpin = (int) $_POST['new_pin'];//new pin
  167. $verpin = (int) $_POST['ver_pin'];//verify new
  168. $errors = array(); //errors
  169. if(empty($pin)){ //no pin given
  170. $errors[] = "Current Pin Incorrect.";
  171. }
  172. if(empty($newpin)){ //no new pin given
  173. $errors[] = "You Msut Enter a New Pin!";
  174. }
  175. if(empty($verpin)){ //no verify new pin given
  176. $errors[] = "You Must Verify Your New Pin!";
  177. }
  178. if($bArray['pin'] != secure($pin)){ //current pin doesnt equal bank current pin
  179. $errors[] = "Incorrect Current Pin!";
  180. }
  181. if($newpin != $verpin){ //new pins dont match
  182. $errors[] = "New Pins Do Not Match!";
  183. }
  184. if(count($errors) > 0){ //errors found
  185. foreach($errors as $err){ //loop to print errors on page
  186. print $err.'';
  187. }
  188. }else{ //no errors
  189. $update_user_pin = mysql_query("UPDATE `members` SET `pin` = '".secure($newpin)."' WHERE `username` = '$logged[username]';") or die(mysql_error()); //update users pin
  190. $update_bank_pin = mysql_query("UPDATE `banks` SET `pin` = '".secure($newpin)."' WHERE `username` = '$logged[username]';"); //update bank pin
  191. print "Pin Updated!"; //success
  192. } //end errors check
  193. } //end form submit
  194. break;
  195. case 'forgotpin': //forgot my pin code D:
  196. if(!$_POST['requestnew']){ //form not submitted
  197. print "Well, looks like you have forgotten your pin. Too bad you won't be getting it right away. After clicking the button below please check your email for your new pin code.
  198. <form method=\"post\">
  199. <input type=\"submit\" name=\"requestnew\" value=\"Get New Pin!\" />
  200. </form>";
  201. }else{ //it was
  202. $chars = "1234567890"; //available chars
  203. $newpin = substr(str_shuffle($chars), 0, 4); //make new pin
  204. $update_bank_pin = mysql_query("UPDATE `banks` SET `pin` = '".secure($newpin)."' WHERE `username` = '$logged[username]';") or die(mysql_error());
  205. $update_members_pin = mysql_query("UPDATE `members` SET `pin` = '".secure($newpin)."' WHERE `username` = '$logged[username]';") or die(mysql_error());
  206. $mail = mail($logged['email'], "New Pin Code", "hello $logged[username], $newpin is your new pin code. please do not lose it."); //email the user
  207. if(!$mail){ //not sent
  208. print "Error sending email!";
  209. }else{//it sent
  210. print "Please check your Email for your pin code.";
  211. }
  212. }
  213. break;
  214. }
  215. }
  216. }
  217. ?>
ShadowMage's Avatar
Author:
Views:
3,971
Rating:
Posted on Tuesday 12th August 2008 at 01:06 AM
ShadowMage
ShadowMage's Avatar
bank.php
Posted on Tuesday 12th August 2008 at 12:55 AM
Adam981
Adam981's Avatar
whatever page ur calling
Posted on Tuesday 12th August 2008 at 12:51 AM
jambomb
jambomb's Avatar
wat file lol
Posted on Monday 11th August 2008 at 10:37 PM
ShadowMage
ShadowMage's Avatar
It can not get access to the secure function :S you will have to palce it in the file.
Posted on Monday 11th August 2008 at 10:09 PM
jambomb
jambomb's Avatar
I get this error

Fatal error: Call to undefined function secure() in /home/jambomb/public_html/HighRize/main/bank.php on line 246
Posted on Sunday 20th July 2008 at 12:57 AM
UrbanTwitch
UrbanTwitch's Avatar
I got it. Nevermind.
Posted on Saturday 19th July 2008 at 02:37 PM
ShadowMage
ShadowMage's Avatar
Error is as it seems. It can't find the function. Add it to that file maybe ^^'
Posted on Friday 18th July 2008 at 09:58 PM
UrbanTwitch
UrbanTwitch's Avatar
I can't desposit points.

Fatal error: Call to undefined function checkpin() in /home/jsfdan/public_html/bank.php on line 147
Posted on Friday 18th July 2008 at 03:29 PM
MrArmstrong
MrArmstrong's Avatar
Yes It is :P
Posted on Thursday 17th July 2008 at 06:28 PM
UrbanTwitch
UrbanTwitch's Avatar
Is this for the Shop?