Subversion Repositories php_users

Rev

Rev 16 | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

<!DOCTYPE html>
<?php
   include_once( '../UsersDataSourceMySQLi.class.php' );
   include_once( '../Users.class.php' );
   session_start();
   //session_destroy(); die;
   // make a connection to the database
   mysqli_report( MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT );
   $mysqlConnection = new mysqli( 'localhost', 'test', 'test', 'test' );
   // create a data source
   $connection = new usersDataSourceMySQLi( $mysqlConnection );
   // check if table exists and, if not, create it with username admin, password admin
   if ( ! $connection->test() ) {
      $initValues = array( 
         'users' => array( 
            'login' => 'admin', 
            'pass' => password_hash( 'admin', PASSWORD_DEFAULT ),
            'admin' => 1, 
         )
         );
      $connection->buildTable( );
      $connection->initTables( $initValues );
   }
   // create an empty Users instance and save it in the session
   if ( ! isset( $_SESSION['user'] ) ) { 
      $_SESSION['user'] = new Users( );
   }
   // if they asked to log out, log out
   if ( isset( $_REQUEST['logout'] ) )
      $_SESSION['user']->logout();

   $url = htmlentities($_SERVER["PHP_SELF"]);
   
?>
<html>
        <head>
                <meta charset="utf-8">
                <title>Login</title>
     <link href='users.css' rel='stylesheet' type='text/css'>
        </head>
        <body>
      <!-- this div is only shown if we need to log in -->
      <div class="login">
         <?php
            // displays/processes login page if needed, empty otherwise
            if ( isset( $_SESSION['user'] ) )
               print $_SESSION['user']->HTML($connection); 
         ?>
      </div>
      <!-- Our menu. Shows who is logged in, and gives a change password
            and logout menu options
      -->
      <div class='menu'>
         <ul>
         <?php
            if ( isset( $_SESSION['user'] ) && $_SESSION['user']->name() ) {
               print "<p>Logged in as " . $_SESSION['user']->name() . '</p>';
               print "<li><a href='$url?doAdmin=1'>Change Password</a></li>\n";
               print "<li><a href='$url?logout=1'>Log Out</a></li>";
            }
         ?>
         </ul>
      </div>
      <!--
      this is where I'd put my content. The only thing in it right now
      is the code to do the admin page if "Change Password" was clicked above
      -->
      <div class='content'>
         <?php
            if ( isset( $_REQUEST['doAdmin'] ) ) {
               print $_SESSION['user']->admin($connection);
               print $_SESSION['user']->errors();
               $_SESSION['user']->clearErrors();
            }
         ?>
      </div>
        </body>
</html>