PHP Classes

File: examples/1.basiclogin.php

Recommend this page to a friend!
  Classes of Jorge Castro  >  PHP Security One  >  examples/1.basiclogin.php  >  Download  
File: examples/1.basiclogin.php
Role: Example script
Content type: text/plain
Description: Example script
Class: PHP Security One
Base class to authenticate an logged in users
Author: By
Last change: Update of examples/1.basiclogin.php
Date: 1 year ago
Size: 1,715 bytes
 

Contents

Class file image Download
<?php
include "../lib/SecurityOne.php";

use
eftec\SecurityOne;
// set securityone
$security=new SecurityOne();
$security->setLoginFn(
    function(
SecurityOne $sec) {
        if (
$sec->user=='admin' && $sec->equalPassword('admin')) {
           
// is an admin
           
$sec->fullName="Bob.Admin";
           
$sec->role='admin';
           
$sec->group=['admin','user','sysop'];
            return
true;
        }
        if (
$sec->user=='user' && $sec->equalPassword('user')) {
           
// is an admin
           
$sec->fullName="Bob.User";
           
$sec->role='user';
           
$sec->group=['user'];
            return
true;
        }
        return
false;
    });


$button=@$_POST['button'];
$message="";
$logged=false;

if (
$button) {
   
$logged=$security->login($_POST['user'],$_POST['password']);
   
$message=(!$logged)?"User {$_POST['user']} or password {$_POST['password']} incorrect":"";
}



if (!
$button || !$logged) {
   
?>
<form method="post">
        <h1>Use admin/admin or user/user to login</h1>
        <label for="user" style="display:inline-block; width:100px;">user:</label>
        <input type="text" id="user" name="user"/><br/>
        <label for="password" style="display:inline-block; width:100px;">password:</label>
        <input type="password" id="password" name="password"/><br/>
        <input type="submit" name="button" value="login"/><br/>
        <span style="color:red"><?=$message;?></span>
    </form>
    <?php
} else {
   
?>
<h1>User logged as <?=$security->fullName;?></h1>
    <a href="2.logged.php">go to 2 as any user</a>
    <a href="2.logged.admin.php">go to 2 as administrator</a>
    <a href="3.logout.php">go to 3 logout</a>
    <?php
}




For more information send a message to info at phpclasses dot org.