Simple and minimalistic user system - RMB Scripting
You appear not to have an account with us, Click HERE to register an account...
RMB Scripting
Navigation
  Home
  Contact
  FAQ
  Forum
  Topsites
  Members
  Affiliates
  Arcade <-- New!
  IoxHost
Downloads
  Templates
  Scripts
  Fonts
  Image Sets
  Software
  Other
 
Tutorials
  .htaccess
  Photoshop
  C/C++
  CSS
  Macromedia Flash
  MySQL
  PHP
  PHP - User System
  HTML
  Javascript
  Macintosh
  Macromedia Fireworks
  Visual Basic
  Visual C#
  Windows
  Python
  Other
 
Latest Tutorials
Get the date of every ...
If...Else on one line ...
Drop Calendar Function
Single VS Double
Understanding Variables
Latest Comments
Simple Comment System
Simple Comment System
Simple Comment System
Simple Comment System
Private Message System
Stats
Total Members: 585 [231]
Total Tutorials: 276
Newsest User: zaithstery
Todays Unique Hits: 71
Users Online: 0
 
8 Guests
Poll
Should we change our name?
Hmmmm, Yes!!
Maybe
Depends on what to
NO!
Main Content
Tutorial: Simple and minimalistic user system By afroxav
As I am working on an internal CMS for my company, I had to develop a user system. I've made a few, so here is the first one.

Features:
*File based, no need for database.
*Fast, extremely fast.
*Secure, even though we use files.
*Easy to modify.

Files:
* id.php
* index.php
* login.php
* logout.php
* register.php
* session.php
* user.php

Here we go:
File: id.php
Contains: the id related to each user (one per user)
   
PHP:
<?php exit; ?>


File: index.php
Contains: this is the starting point of everything here
   
PHP:
<?php
require "session.php";

$user = new user;
$user->start();

html_start('Home');
html_nav();

if (
$user->check_login() == true) {
    echo 
"<div>Welcome back, " $user->data['name'] . "[<a href=\"logout.php\">Log Out</a>].</div>\n";
} else {
    echo 
"<div>Please log in.</div>\n";
}

echo 
"<div>\n";
echo 
"The values of the user array.\n";
echo 
"<table>\n";
echo 
"<tr>\n";
echo 
"<th>Key name</th>\n";
echo 
"<th>Key value</th>\n";
echo 
"</tr>\n";

foreach (
$user->data as $key => $value) {
    if (
is_bool($value)) $value = ($value) ? 'yes' 'no';
    if (
$value == 'true'$value 'yes';
    if (
$value == 'false'$value 'no';

    echo 
"<tr>\n";
    echo 
"<td>" $key "</td>\n";
    echo 
"<td>" $value "</td>\n";
    echo 
"</tr>\n";
}

echo 
"</table>\n";
echo 
"</div>\n";

html_end();
?>


File: login.php
Contains: the login code
   
PHP:
<?php
require "session.php";

$user = new user;
$user->start();

html_start('Log In');

if (isset(
$_POST['submit'])) {
    
$login $user->login($_POST['user'], $_POST['pass']);
    if (
$login == true) {
        
html_nav();
        echo 
"<div>You have now logged in, " $user->data['name'] . ".</div>\n";
    } else {
        
html_nav();
        echo 
"<div>\n";
        echo 
"There was an error... please try again.\n";
        echo 
"<form action=\"login.php\" method=\"post\" id=\"login\">\n";
        echo 
"Username: <input type=\"text\" id=\"user\" name=\"user\" />\n";
        echo 
"\n";
        echo 
"Password: <input type=\"password\" id=\"pass\" name=\"pass\" />\n";
        echo 
"\n";
        echo 
"<input type=\"submit\" id=\"submit\" name=\"submit\" />\n";
        echo 
"</form>\n";
        echo 
"</div>\n";
    }
} else {
    
html_nav();
    echo 
"<div>\n";
    echo 
"<form action=\"login.php\" method=\"post\" id=\"login\">\n";
    echo 
"Username: <input type=\"text\" id=\"user\" name=\"user\" />\n";
    echo 
"\n";
    echo 
"Password: <input type=\"password\" id=\"pass\" name=\"pass\" />\n";
    echo 
"\n";
    echo 
"<input type=\"submit\" id=\"submit\" name=\"submit\" />\n";
    echo 
"</form>\n";
    echo 
"</div>\n";
}

html_end();
?>


File: logout.php
Contains: the logout code
   
PHP:
<?php
require "session.php";

$user = new user;
$user->start();

html_start('Log Out');

$user->logout();

html_nav();

echo 
"<div>You have been logged out.</div>\n";

html_end();
?>


File: register.php
Contains: the registration code
   
PHP:
<?php
require "session.php";

$user = new user;
$user->start();

html_start('Register');
html_nav();

if (isset(
$_POST['submit'])) {
    
$uarray $user->prep_reg_array($_POST['user'], $_POST['pass'], $_POST['email']);
    
$reg $user->register($uarray);
    if (
$reg == true) {
        echo 
"<div>You have now registered. You may now <a href=\"login.php\">log in</a>.</div>\n";
    } else {
        echo 
"<div>\n";
        echo 
"There was an error... please try again.\n";
        echo 
"<form action=\"register.php\" method=\"post\" id=\"register\">\n";
        echo 
"Username: <input type=\"text\" id=\"user\" name=\"user\" />\n";
        echo 
"\n";
        echo 
"Password: <input type=\"password\" id=\"pass\" name=\"pass\" />\n";
        echo 
"\n";
        echo 
"Email: <input type=\"text\" id=\"email\" name=\"email\" />\n";
        echo 
"\n";
        echo 
"<input type=\"submit\" id=\"submit\" name=\"submit\" />\n";
        echo 
"</form>\n";
        echo 
"</div>\n";
    }
} else {
    echo 
"<div>\n";
    echo 
"<form action=\"register.php\" method=\"post\" id=\"register\">\n";
    echo 
"Username: <input type=\"text\" id=\"user\" name=\"user\" />\n";
    echo 
"\n";
    echo 
"Password: <input type=\"password\" id=\"pass\" name=\"pass\" />\n";
    echo 
"\n";
    echo 
"Email: <input type=\"text\" id=\"email\" name=\"email\" />\n";
    echo 
"\n";
    echo 
"<input type=\"submit\" id=\"submit\" name=\"submit\" />\n";
    echo 
"</form>\n";
    echo 
"</div>\n";
}

html_end();
?>


File: session.php
Contains: generic functions and all the functions/class to handle everything, from login to logout, passing by register
   
PHP:
<?php
class user {
    var 
$file_dir "D:/public_html/php_random/login/";
    
    function 
start($time 3600) {
        
session_set_cookie_params($time '/php_random/login/');
        
session_name('afroxav-login');
        
session_start();

         
// Reset the expiration time upon page load
        
if (isset($_COOKIE['afroxav-login'])) {
            
setcookie('afroxav-login'$_COOKIE['afroxav-login'], time() + $time'/php_random/login/''localhost'01);
        }

        if (!isset(
$_SESSION['info'])) {
            
$this->data = array('name' => 'Anonymous''logged' => false);
        } else {
            
$this->data $_SESSION['info'];
        }
    }

    function 
check_login() {
        if (
$this->data['logged'] !== true) {
            return 
false;
        } else if (
$this->data['logged'] === true) {
            if (
$this->data['name'] !== 'Anonymous') {
                return 
true;
            }
            return 
false;
        }
        return 
false;
    }

    function 
login($user$pass) {
        
$logins_raw = @file_get_contents($this->file_dir 'users.php');
        
$logins_processed str_replace('<?php exit; ?>'''$logins_raw);
        
$logins_array explode('\n'$logins_processed);
        foreach (
$logins_array as $id => $line) {
            
$logins[$id] = explode('|'$line);
        }
        
$pass $this->hash_pass($pass);

        foreach (
$logins as $user_info) {
            if (
$user_info[1] == $user) {
                if (
$user_info[2] == $pass) {
                    
$this->update_session($user_info);
                    return 
true;
                }
            }
        }

        return 
false;
    }

    function 
hash_pass($string) {
        return 
hash('sha512'$string);
    }

    function 
logout() {
        
$_SESSION['info'] = array('name' => 'Anonymous''logged' => false);
        
$this->data $_SESSION['info'];
        return 
true;
    }

    function 
prep_reg_array($name$pass$email$mod 'false'$admin 'false') {
        
$id_raw = @file_get_contents($this->file_dir 'id.php');
        
$id str_replace('<?php exit; ?>'''$id_raw);
        
$id $id 1;
        @
file_put_contents($this->file_dir 'id.php''<?php exit; ?>' $id);
        return array(
$id$name$this->hash_pass($pass), $email$mod$admin);
    }

    function 
register($userdata) {
        
$write file_put_contents($this->file_dir 'users.php''\n' implode('|'$userdata), FILE_APPEND);
        return (
$write !== false) ? true false;
    }

    function 
update_session($array) {
        
$_SESSION['info'] = array(
            
'id'        => $array[0],
            
'name'        => $array[1],
            
'pass'        => $array[2],
            
'email'        => $array[3],
            
'mod'        => $array[4],
            
'admin'        => $array[5],
            
'logged'    => true
        
);
        
$this->data $_SESSION['info'];
    }
};

//html related functions
//not related at all with the sessions
function html_start($title) {
    
header('Content-type: text/html');

    echo 
"<html>\n";
    echo 
"<head>\n";
    echo 
"<title>\n";
    echo 
$title;
    echo 
"</title>\n";
    echo 
"</head>\n";
    echo 
"<body>\n";
    echo 
"<h1>\n";
    echo 
$title;
    echo 
"</h1>\n";
}

function 
html_nav() {
    global 
$user;
    
    echo 
"<div>\n";
    echo 
"Navigation\n";
    echo 
"<ul>\n";
    echo 
"<li><a href=\"index.php\">Home</a></li>\n";
    if (
$user->check_login() == true) {
        echo 
"<li><a href=\"logout.php\">Log Out</a></li>\n";
    } else {
        echo 
"<li><a href=\"login.php\">Log In</a></li>\n";
        echo 
"<li><a href=\"register.php\">Register</a></li>\n";
    }
    echo 
"</ul>\n";
    echo 
"</div>\n";    
}

function 
html_end() {
    echo 
"</body>\n";
    echo 
"</html>\n";
}
?>


File: users.php
Contains: the list/database of all the users
   
PHP:
<?php exit; ?>


And this is it. It is some very basic user system. There is a simple ranking system, but I haven't setup the code to actually use the ranks.
There is, as I said, a lot of improvement left to do, and I will work on it in my free time.
Difficulty: Easy
Views: 5930
Rating:
Comments
Posted on Saturday 4th April 2009 at 11:40 PM
ShadowMage
Fixed tags.
Posted on Sunday 12th April 2009 at 07:47 PM
Matt
you should put the html in html documents and php in php documents.
Posted on Tuesday 14th April 2009 at 03:12 PM
ShadowMage
Doesn't really matter.....
Posted on Thursday 23rd April 2009 at 05:36 AM
Matt
kinda does, in most ways.
http://www.phpdebutant.com/forum/33/Separating_PHP_from_HTML.htm

or at least look at heredoc
http://en.wikipedia.org/wiki/Heredoc

you could do
echo <<<CODE
<div>
<form>
crap here
</form>
</div>
CODE;

it just looks sloppy the way your doing it.
Posted on Sunday 24th January 2010 at 03:02 PM
afroxav
This is a minimalistic user system. It is also in it's early version. For any bigger project, I implement more defined MVC-type layout, separating the code logic from content processing from data display. I always include templates and a simple parsing/templating engine in bigger projects I do.
Posted on Sunday 24th January 2010 at 03:04 PM
afroxav
If I ever decide to improve this data management script (I have another more complex one in the works), I'll implement a templating engine.
Notice:
Remember to post long codes on our pastbin! - (http://rmb.pastebin.com/)
Add Comment
You must be logged in to post a comment.
Good Morning Guest
Username: 
Password: 
Remember Username
Links
RMB Arcade
$2.50 Resellers
Free Domains
Free Games
Affiliates