Location: PHPKode > scripts > XS PHP Library > xs-php-library/src/auth.php
<?php
//
// +--------------------------------------------------------------------------+
// |                                                                          |
// |                   XS PHP Library  Generic Classes Library                |
// |                                                                          |
// |                   Copyright (c) 2001-2002 XSPHPLib Group.                |
// |                                                                          |
// +--------------------------------------------------------------------------+
// |                                                                          |
// | Distributed under the terms of the GNU Lesser General Public License as  |
// | published by the Free Software Foundation version 2.1                    |
// | See the GNU Lesser General Public License for more details. You should   |
// | have received a copy of the GNU Lesser General Public License along with |
// | this package; if not, write to the Free Software Foundation, Inc.,       |
// | 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.                 |
// |                                                                          |
// +--------------------------------------------------------------------------+
// |                                                                          |
// | Authors: Robert Bala <hide@address.com>                                   |
// |                                                                          |
// +--------------------------------------------------------------------------+
//
// $Id: auth.php,v 1.2 2002/11/28 09:51:38 rbala Exp $

include_once('../inc/object.inc.php');
include_once('../inc/auth.inc.php');
include_once('../inc/file.inc.php');
include_once('../inc/sgfiles.inc.php');
include_once('../inc/session.inc.php');

class User {
    var $name;
    var $pass;
    var $perm;
    
    function User($name, $pass, $perm) {
        $this->name = $name;
        $this->pass = $pass;
        $this->perm = $perm;
    }
    
}

function _login(&$params) {
    global $list;
    for ($i = 0; $i < count($list); $i++) {
        $user =& $list[$i];
        if (($user->name == $params['username']) && ($user->pass == $params['password'])) {
            $params['perms'] = $user->perm;
            return true;
        }
    }
    return false;
}

function _logout($params) {
   return true;
}

$list[] = new User('admin', 'secret', 'admin');
$list[] = new User('guest', 'anonymous', 'user');

$sess = new Session();

$result = $sess->open();

if (object_isError($result)) {
    die($result->getMessage());
}

$string = $sess->getParam('_session_auth');

if (strlen($string) && $sess->exists('_session_auth')) {
    $auth = @unserialize($string);
} else {
    $auth = new Auth('_login', '_logout');
}

if (isset($HTTP_POST_VARS['action'])) {
    $action = $HTTP_POST_VARS['action'];

    if ($action == 'start') {

        if (isset($HTTP_POST_VARS['username'])) {
            $username = $HTTP_POST_VARS['username'];
        } else {
            $username = '';
        }
        if (isset($HTTP_POST_VARS['password'])) {
            $password = $HTTP_POST_VARS['password'];
        } else {
            $password = '';
        }

        if (!$auth->login($username, $password)) {
            echo 'Unauthorized login!!!';
        }
    } elseif ($action == 'close') {
        $auth->logout();
    }

}

if ($auth->isLogged()) {
    echo 'You are sucessfully logged user: <b>' . $auth->getUsername() . '</b><br>';
    echo 'Your permission level is: <b>' . $auth->getPerms() . '</b><br>';

    echo '<form action="' . $_SERVER['PHP_SELF'] . '" method="post">';
    echo '<input type="hidden" name="action" value="close">';
    echo '<input type="submit" name="logout" value="Logout">';
    echo '</form>';
    
    echo '<br>';
    
    if ($auth->validPerms('admin')) {
       echo 'You are allowed to do almost everything!!<br>';
    } elseif ($auth->validPerms('user')) {
       echo 'You are not allowed to do many things!!<br>';
    }

} else {
    echo '<form action="' . $_SERVER['PHP_SELF'] . '" method="post">';
    echo '<input type="hidden" name="action" value="start">';
    echo 'Username: <input type="text" name="username" value=""><br>';
    echo 'Password: <input type="password" name="password" value=""><br>';
    echo '<input type="submit" name="login" value="Login">';
    echo '</form>';
}

$string = @serialize($auth);

if (strlen($string)) {
    $sess->setParam('_session_auth', $string);
} else {
    die('Could not store session data.');
}

unset($auth);

$sess->close();

?>
Return current item: XS PHP Library