Some Examples to get Started with the User Class

Starting the User object

The User object needs to be started on every page you want to authenticate


    //This creates, starts the object and Resume the users current session
    $user = new ptejada\uFlex\User();
    $user->start();

                

Alternatively you can configured the class inline like this:


    //This creates the object
    $user = new ptejada\uFlex\User(false);

    /*
     * You may now configure the User object before starting it
     */

    //Starts the object and Resume the users current session
    $user->start()

                

Sample Login Form

Login

  <form method="post" action="../your_login_process_page.php">
    <label>Username:</label>
    <input type="text" name="Username" />
    <br>

    <label>Password:</label>
    <input type="Password" name="Password" />
    <br>

    <label>Remember me?:</label>
    <input type="checkbox" name="auto" />
    <br>

    <input type="submit" value="login" />
  </form>

                            

This would be included in your_login_process_page.php file which will process the login request.


  $username = $_POST['Username'];
  $password = $_POST['Password'];
  $auto = $_POST['auto'];  //To remember user with a cookie for autologin

  $user = new ptejada\uFlex\User();

  //Login with credentials
  $user->login($username,$password,$auto);

  //not required, just an example usage of the built-in error reporting system
  if($user->signed){
    echo "User Successfully Logged in";
  }else{
    //Display Errors
    foreach($user->log->getErrors() as $err){
      echo "<b>Error:</b> {$err} <br/ >";
    }
  }

                            

Sample Registration form

Register

Remember that you can add as many fields as you want to your forms as long as the field is validated with in the class and that the field or input name matches a column in the database.


<form method="post" action="../your_registration_process_page.php">
  <label>Username:</label>
  <input type="text" name="Username" />

  <label>Password:</label>
  <input type="Password" name="Password" />

  <label>Re-enter Password:</label>
  <input type="Password" name="Password2" />

  <label>Email: </label>
  <input type="text" name="email" />

  <label>Group: </label>
  <select name="groupID">
    <option value="1">User</option>
    <option value="2">Developer</option>
    <option value="3">Designer</option>
  </select>

  <input type="submit" value="Register" />
</form>

                            

This would be included in "your_registration_process_page.php" file which will process the registration request. Because the $_POST array we are going to get will match the object for the register method we will pass the whole $_POST array to the register method. Please do Note this might not always be the case and that further user input sanitation might be required.


    $input = ptejada\uFlex\Collection($_POST);

    $registered = $user->register(array(
        'Username'  => $input->Username,
        'Password'  => $input->passsword,
        'Password2' => $input->passsword2,
        'email'     => $input->email,
        'groupID'   => $input->groupID,
    ),false);

    if($registered){
        echo "User Registered";
    }else{
        //Display Errors
        foreach($user->log->getErrors() as $err){
            echo "<b>Error:</b> {$err} <br/ >";
        }
    }
                            

Sample Logout link


    <a href="../Your_logout_process_page.php">Logout</a>

                            

This would be included in "your_logout_process_page.php" file which will process the logout request.


    //Logouts user and clears any auto login cookie
    $user->logout();


                            

All Together

If you've gone through all the PHP code in this examples above you can see how simple an easy it is and how you can fit every process in a single script like the one below

<?php
    //This creates the object
    $user = new ptejada\uFlex\User(false);

    /*
    * You may now configure the User object before starting it
    */
    $user->config->database->update(array(
        'host'     => 'localhost',
        'user'     => 'root',
        'Password' => '123',
        'name'     => 'demo', // Database name
    ));

    //Starts the object and Resume the users current session
    $user->start();

    switch($_GET['mode']){
    case 'login':

      $username = $_POST['Username'];
      $password = $_POST['Password'];
      $auto = $_POST['auto'];  //To remember user with a cookie for autologin

      $user = new ptejada\uFlex\User();

      //Login with credentials
      $user->login($username,$password,$auto);

      //not required, just an example usage of the built-in error reporting system
      if($user->signed){
        echo "User Successfully Logged in";
      }else{
        //Display Errors
        foreach($user->log->getErrors() as $err){
          echo "<b>Error:</b> {$err} <br/ >";
        }
      }

      break;

    case 'register':

        $input = ptejada\uFlex\Collection($_POST);

        $registered = $user->register(array(
            'Username'  => $input->Username,
            'Password'  => $input->passsword,
            'Password2' => $input->passsword2,
            'email'     => $input->email,
            'groupID'   => $input->groupID,
        ),false);

        if($registered){
            echo "User Registered";
        }else{
            //Display Errors
            foreach($user->log->getErrors() as $err){
                echo "<b>Error:</b> {$err} <br/ >";
            }
        }

        break;

    case "logout":

        //Logouts user and clears any auto login cookie
        $user->logout();

        break;
    }
    ?>
                


Copyright © 2010-2016 Pablo Tejada, All Right Reserved. Webmaster | Generated in 0.01 seconds