Advertisement
Guest User

Untitled

a guest
Sep 20th, 2017
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 7.70 KB | None | 0 0
  1. The PHP Database Theory
  2.  
  3. Author: Fid
  4.  
  5. 6/27/2011
  6. ______________________________________________________________________________________________
  7.  
  8. Hello Fid here. I have had this theory for a while but have never acted upon it until this morning. I wanted to figure out an easier way to record information from a form and retrieve it from a md5 encrypted .txt file. The obvious way to record and retrieve information is using MySQL so being the nice guy I am, I decided to code an easier way to do this without setting up the MySQL database. You need to make a file named 'users.txt' and add chmod to it. The username's and passwords will be saved in the text file and encrypted using md5 and salt. I hope you learn something from this.
  9. ____________________________________________________________________________________________
  10.  
  11. }}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}
  12. Register.php
  13. {{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{
  14. ____________________________________________________________________________________________
  15.  
  16. <?php
  17.  
  18. require_once("functions.php");
  19.  
  20. if($_POST['submit']) {
  21.  
  22.     if($_POST['email'] != '' && $_POST['password'] != '' && $_POST['username'] != '') {
  23.         $register = new register();
  24.         if ($register->If_userexists($_POST['username']) == 1) {
  25.             echo '<p>That user already exists.</p>';
  26.             die();
  27.         }
  28.         if($register->emailisvalid($_POST['email']) == 0) {
  29.             echo 'The email address you entered is incorrect.';
  30.         }
  31.         echo $register->create_user($_POST['username'], $_POST['email'], $_POST['password']);
  32.     } else {
  33.         echo '<p>You have not entered all of the required information.</p>';
  34.     }
  35.  
  36. }
  37.  
  38. ?>
  39. <html>
  40.     <body>
  41.         <table>
  42.             <form action="register.php" method="post">
  43.                 <tr>
  44.         <td>
  45.         Username:
  46.         </td>
  47.         <td>
  48.         <input type="text" name="username" />
  49.         </td>
  50.                 </tr>
  51.                 <tr>
  52.         <td>
  53.         Email:
  54.         </td>
  55.         <td>
  56.         <input type="text" name="email" />
  57.         </td>
  58.                 </tr>
  59.                 <tr>
  60.         <td>
  61.         Password:
  62.         </td>
  63.         <td>
  64.         <input type="text" name="password" />
  65.         </td>
  66.                 </tr>
  67.                 <tr>
  68.         <td>
  69.         Confirm:
  70.         </td>
  71.         <td>
  72.         <input type="text" name="password2" />
  73.         </td>
  74.                 </tr>
  75.                 <tr>
  76.         <td colspan="2">
  77.         <input type="submit" name="submit" value="Register" />
  78.         </td>
  79.                 </tr>
  80.             </form>
  81.         </table>
  82.     </body>
  83. </html>
  84. ____________________________________________________________________________________________
  85.  
  86. }}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}
  87. Login.php
  88. {{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{
  89. ____________________________________________________________________________________________
  90.  
  91. <?php
  92. session_start();
  93. if (isset($_SESSION['user']))
  94.  {
  95.  die("you are already logged in");
  96.  }
  97.  
  98. require_once("functions.php");
  99.  if($_POST['submit') {
  100.   if($_POST['username']!="" && $_POST['password']!="") {
  101.    $login=new login();
  102.     if($login->check_user($_POST['user'],$_POST['password'])=1)
  103.    {
  104.     print "Login suceeded";
  105.     $_SESSION['usr']=$_POST['user'];
  106.    } else {
  107.    print "There is an error occured";
  108.    }
  109.  
  110.   }
  111.  } print '
  112. <html><body>
  113. <table>
  114.  <td>
  115.   <tr>
  116.    <form action=login.php method=post>
  117.    Username: <input type=text name=username>
  118.   </tr>
  119.  <br /><br />
  120.   <tr>
  121.   Password: <input type=password name=password>
  122.   </tr>
  123.  <br /><br />
  124.   <tr>
  125.    <input type=submit name=submit value=Login>
  126.   </tr>
  127.  </td>
  128. </table>
  129. </form>
  130. </body></html>';
  131. ____________________________________________________________________________________________
  132.  
  133. }}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}
  134. Functions.php
  135. {{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{
  136. ____________________________________________________________________________________________
  137.  
  138. <?php
  139. include_once("config.php");
  140.  
  141. //Register Class , needed functions for register page
  142.  
  143. class register
  144. {  
  145.  
  146. function If_userexists($user)  
  147. {  
  148.    //This simple function checks if the user
  149.    //actually exists. It simply checks the database in
  150.    //a foreach if any line contains in the first category the username
  151.     $get=file("users.txt");
  152.     foreach ($get as $line) {
  153.     $split=explode("|",$line);
  154.     if($split[0]==$user)
  155.   {
  156.   $val=1;
  157.   }
  158.     }
  159.   if ($val!=1)
  160.   {
  161.   $val=0;
  162.   }
  163.   return $val;
  164. }
  165.  
  166. function create_user($user,$email,$password)
  167. {
  168.    //That function creates the user , it will generate a new line in the file
  169.    //and write in the username the salt md5 encrypted password, the email
  170.    //and the registration date.
  171.   if(!strstr($user,"|"))
  172.    {
  173.     $date= date("m.d.Y");
  174.     $pwd=md5($password.md5("bla"));
  175.     $datei="db/users.txt";
  176.     $zeiger = fopen($datei, "a+");
  177.     fputs($zeiger, $user. "|".$password. "|".$email. "|".$date."\n");
  178.     fclose($zeiger);
  179.     return "Registration succeeded";
  180.    } else {
  181.    return "Registration failed";
  182.    }
  183. }
  184.  
  185. function If_emailisvalid($email)
  186. {
  187.    //Checks string for an "@"
  188.   if (!strstr($_POST['email'],"@"))
  189.    {
  190.     $val=0;
  191.  
  192.    } else {
  193.    $val=1;
  194.    }
  195.  
  196.   return $val;
  197.  
  198. }
  199.  
  200. function Get_newestuser()
  201. {
  202.    //Checks last entry in the database
  203.   $file=file('db/users,txt');
  204.   $max=count($file)-1;
  205.   $split=explode("|",$file[$max]);
  206.   return $split[0];
  207. }
  208.  
  209. }
  210.  
  211. //Login class, needed functions for login page
  212.  
  213. class login
  214. {
  215.  
  216.     function check_user($user,$password)
  217.     {
  218.   //Checks every line in the database for the username
  219.   //and then compares the two encrypted passwords
  220.   $file=file("db/users.txt");
  221.   foreach($file as $line)
  222.   {
  223.     $split=explode("|",$line);
  224.     if($split[0]==$user)
  225.     {
  226.     $getPass=md5($password.md5("bla"));
  227.     if($split[1]==$getPass)
  228.     {
  229.   $val=1;
  230.     }
  231.     }
  232.   }
  233.     $val=0;
  234.     }
  235.  
  236. }
  237.  
  238. class admin
  239. {
  240. function del_user($user)
  241. {
  242.   //checks every line if it contains the user if yes then it saves the string
  243.   //deletes string from array , implodes the array and overwrites the file again
  244. $data = 'db/users.txt';
  245. $value = strpos($lines,$user);
  246. for ($line = 0; $line < count($data); $line++)
  247. {
  248. if (strpos($data[$line], $user) >= 0)
  249.   {
  250.   $found=$line;
  251.   }
  252. }
  253. $lines=file($data);
  254. $last = $found+1;
  255. unset($lines[$last]);
  256. $fp = fopen($data, 'w');
  257. fwrite($fp, implode('', $lines));
  258. fclose($fp);
  259. return "User deleted";
  260. }
  261. }
  262. ?>
  263. ____________________________________________________________________________________________
  264.  
  265. }}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}
  266. Config.php
  267. {{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{
  268. ____________________________________________________________________________________________
  269. <?php
  270. $_admin="your admin name"; // You need to register first.
  271. ?>
  272. ____________________________________________________________________________________________
  273.  
  274. }}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}
  275. Config.php
  276. {{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{
  277. ____________________________________________________________________________________________
  278. <?php
  279. session_start();
  280. if($_SESSION['user']!=$_admin)
  281. {
  282. die("not logged in or no admin");
  283. }
  284.  
  285. print 'Delete user:<br> \n';
  286. print '<form action=admin.php method=post><br>
  287.   <select name=user>
  288.    <optgroup label=Select>';
  289.    $lines=file("db/users.txt");
  290.     foreach ($lines as $line)
  291.   {
  292.     $split=explode("|",$line);
  293.     print'<option>'.$split[0].'</option>';
  294.   }
  295.   print '</optgroup>
  296.   </select><br>
  297. <input type=submit name=submit value=register>
  298. </form>';
  299.  
  300. if($_POST['submit'])
  301.  {
  302.  $admin=new admin();
  303.  print $admin->del_user($_POST['user']);
  304.  }
  305.  
  306. print '<br />';
  307.  $register=new register();
  308.  print $register->Get_newestuser();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement