Subversion Repositories php_users

Rev

Rev 8 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
4 rodolico 1
<!DOCTYPE html>
2
<?php
3
   include_once( '../UsersDataSourceMySQLi.class.php' );
4
   include_once( '../Users.class.php' );
5
   session_start();
6
   //session_destroy(); die;
8 rodolico 7
   // make a connection to the database
8
   mysqli_report( MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT );
9
   $mysqlConnection = new mysqli( 'localhost', 'test', 'test', 'test' );
10
   // create a data source
11
   $connection = new usersDataSource( $mysqlConnection );
12
   // check if table exists and, if not, create it with username admin, password admin
13
   if ( ! $connection->test() ) {
16 rodolico 14
      $initValues = array( 
15
         'users' => array( 
16
            'login' => 'admin', 
17
            'pass' => password_hash( 'admin', PASSWORD_DEFAULT ),
18
            'admin' => 1, 
19
         )
20
         );
21
      $connection->buildTable( );
22
      $connection->initTables( $initValues );
8 rodolico 23
   }
24
   // create an empty Users instance and save it in the session
4 rodolico 25
   if ( ! isset( $_SESSION['user'] ) ) { 
8 rodolico 26
      $_SESSION['user'] = new Users( );
4 rodolico 27
   }
8 rodolico 28
   // if they asked to log out, log out
4 rodolico 29
   if ( isset( $_REQUEST['logout'] ) )
30
      $_SESSION['user']->logout();
31
 
32
   $url = htmlentities($_SERVER["PHP_SELF"]);
33
 
34
?>
35
<html>
36
	<head>
37
		<meta charset="utf-8">
38
		<title>Login</title>
39
     <link href='users.css' rel='stylesheet' type='text/css'>
40
	</head>
41
	<body>
8 rodolico 42
      <!-- this div is only shown if we need to log in -->
4 rodolico 43
      <div class="login">
8 rodolico 44
         <?php
45
            // displays/processes login page if needed, empty otherwise
16 rodolico 46
            if ( isset( $_SESSION['user'] ) )
47
               print $_SESSION['user']->HTML($connection); 
4 rodolico 48
         ?>
49
      </div>
8 rodolico 50
      <!-- Our menu. Shows who is logged in, and gives a change password
51
            and logout menu options
52
      -->
4 rodolico 53
      <div class='menu'>
54
         <ul>
55
         <?php
56
            if ( isset( $_SESSION['user'] ) && $_SESSION['user']->name() ) {
57
               print "<p>Logged in as " . $_SESSION['user']->name() . '</p>';
58
               print "<li><a href='$url?doAdmin=1'>Change Password</a></li>\n";
59
               print "<li><a href='$url?logout=1'>Log Out</a></li>";
60
            }
61
         ?>
62
         </ul>
63
      </div>
8 rodolico 64
      <!--
65
      this is where I'd put my content. The only thing in it right now
66
      is the code to do the admin page if "Change Password" was clicked above
67
      -->
4 rodolico 68
      <div class='content'>
69
         <?php
70
            if ( isset( $_REQUEST['doAdmin'] ) ) {
71
               print $_SESSION['user']->admin($connection);
7 rodolico 72
               print $_SESSION['user']->errors();
73
               $_SESSION['user']->clearErrors();
4 rodolico 74
            }
75
         ?>
76
      </div>
77
	</body>
78
</html>