Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- The PHP Database Theory
- Author: Fid
- 6/27/2011
- ______________________________________________________________________________________________
- 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.
- ____________________________________________________________________________________________
- }}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}
- Register.php
- {{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{
- ____________________________________________________________________________________________
- <?php
- require_once("functions.php");
- if($_POST['submit']) {
- if($_POST['email'] != '' && $_POST['password'] != '' && $_POST['username'] != '') {
- $register = new register();
- if ($register->If_userexists($_POST['username']) == 1) {
- echo '<p>That user already exists.</p>';
- die();
- }
- if($register->emailisvalid($_POST['email']) == 0) {
- echo 'The email address you entered is incorrect.';
- }
- echo $register->create_user($_POST['username'], $_POST['email'], $_POST['password']);
- } else {
- echo '<p>You have not entered all of the required information.</p>';
- }
- }
- ?>
- <html>
- <body>
- <table>
- <form action="register.php" method="post">
- <tr>
- <td>
- Username:
- </td>
- <td>
- <input type="text" name="username" />
- </td>
- </tr>
- <tr>
- <td>
- Email:
- </td>
- <td>
- <input type="text" name="email" />
- </td>
- </tr>
- <tr>
- <td>
- Password:
- </td>
- <td>
- <input type="text" name="password" />
- </td>
- </tr>
- <tr>
- <td>
- Confirm:
- </td>
- <td>
- <input type="text" name="password2" />
- </td>
- </tr>
- <tr>
- <td colspan="2">
- <input type="submit" name="submit" value="Register" />
- </td>
- </tr>
- </form>
- </table>
- </body>
- </html>
- ____________________________________________________________________________________________
- }}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}
- Login.php
- {{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{
- ____________________________________________________________________________________________
- <?php
- session_start();
- if (isset($_SESSION['user']))
- {
- die("you are already logged in");
- }
- require_once("functions.php");
- if($_POST['submit') {
- if($_POST['username']!="" && $_POST['password']!="") {
- $login=new login();
- if($login->check_user($_POST['user'],$_POST['password'])=1)
- {
- print "Login suceeded";
- $_SESSION['usr']=$_POST['user'];
- } else {
- print "There is an error occured";
- }
- }
- } print '
- <html><body>
- <table>
- <td>
- <tr>
- <form action=login.php method=post>
- Username: <input type=text name=username>
- </tr>
- <br /><br />
- <tr>
- Password: <input type=password name=password>
- </tr>
- <br /><br />
- <tr>
- <input type=submit name=submit value=Login>
- </tr>
- </td>
- </table>
- </form>
- </body></html>';
- ____________________________________________________________________________________________
- }}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}
- Functions.php
- {{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{
- ____________________________________________________________________________________________
- <?php
- include_once("config.php");
- //Register Class , needed functions for register page
- class register
- {
- function If_userexists($user)
- {
- //This simple function checks if the user
- //actually exists. It simply checks the database in
- //a foreach if any line contains in the first category the username
- $get=file("users.txt");
- foreach ($get as $line) {
- $split=explode("|",$line);
- if($split[0]==$user)
- {
- $val=1;
- }
- }
- if ($val!=1)
- {
- $val=0;
- }
- return $val;
- }
- function create_user($user,$email,$password)
- {
- //That function creates the user , it will generate a new line in the file
- //and write in the username the salt md5 encrypted password, the email
- //and the registration date.
- if(!strstr($user,"|"))
- {
- $date= date("m.d.Y");
- $pwd=md5($password.md5("bla"));
- $datei="db/users.txt";
- $zeiger = fopen($datei, "a+");
- fputs($zeiger, $user. "|".$password. "|".$email. "|".$date."\n");
- fclose($zeiger);
- return "Registration succeeded";
- } else {
- return "Registration failed";
- }
- }
- function If_emailisvalid($email)
- {
- //Checks string for an "@"
- if (!strstr($_POST['email'],"@"))
- {
- $val=0;
- } else {
- $val=1;
- }
- return $val;
- }
- function Get_newestuser()
- {
- //Checks last entry in the database
- $file=file('db/users,txt');
- $max=count($file)-1;
- $split=explode("|",$file[$max]);
- return $split[0];
- }
- }
- //Login class, needed functions for login page
- class login
- {
- function check_user($user,$password)
- {
- //Checks every line in the database for the username
- //and then compares the two encrypted passwords
- $file=file("db/users.txt");
- foreach($file as $line)
- {
- $split=explode("|",$line);
- if($split[0]==$user)
- {
- $getPass=md5($password.md5("bla"));
- if($split[1]==$getPass)
- {
- $val=1;
- }
- }
- }
- $val=0;
- }
- }
- class admin
- {
- function del_user($user)
- {
- //checks every line if it contains the user if yes then it saves the string
- //deletes string from array , implodes the array and overwrites the file again
- $data = 'db/users.txt';
- $value = strpos($lines,$user);
- for ($line = 0; $line < count($data); $line++)
- {
- if (strpos($data[$line], $user) >= 0)
- {
- $found=$line;
- }
- }
- $lines=file($data);
- $last = $found+1;
- unset($lines[$last]);
- $fp = fopen($data, 'w');
- fwrite($fp, implode('', $lines));
- fclose($fp);
- return "User deleted";
- }
- }
- ?>
- ____________________________________________________________________________________________
- }}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}
- Config.php
- {{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{
- ____________________________________________________________________________________________
- <?php
- $_admin="your admin name"; // You need to register first.
- ?>
- ____________________________________________________________________________________________
- }}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}
- Config.php
- {{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{
- ____________________________________________________________________________________________
- <?php
- session_start();
- if($_SESSION['user']!=$_admin)
- {
- die("not logged in or no admin");
- }
- print 'Delete user:<br> \n';
- print '<form action=admin.php method=post><br>
- <select name=user>
- <optgroup label=Select>';
- $lines=file("db/users.txt");
- foreach ($lines as $line)
- {
- $split=explode("|",$line);
- print'<option>'.$split[0].'</option>';
- }
- print '</optgroup>
- </select><br>
- <input type=submit name=submit value=register>
- </form>';
- if($_POST['submit'])
- {
- $admin=new admin();
- print $admin->del_user($_POST['user']);
- }
- print '<br />';
- $register=new register();
- print $register->Get_newestuser();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement