Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- require_once('config.php');
- require_once('lib/database.php');
- require_once('lib/user.php');
- require_once('lib/auth.php');
- session_start();
- //login
- if(isset($_POST['submit'])){
- $check_auth = $auth->authenticate_user($_POST['username'] ,$_POST['password']);
- if($check_auth){
- $_SESSION['is_logged_in'] = TRUE;
- $_SESSION['username'] = $_POST['username'];
- header("Location: " . $config['base_url'] . "index.php");
- exit();
- } else {
- $_SESSION['is_logged_in'] = FALSE;
- }
- }
- //update user
- if(isset($_POST['update'])){
- if($update_user){
- header("Location: " . $config['base_url'] . "index.php?action=read");
- exit();
- }
- }
- if(isset($_POST['signup'])){
- if(!empty($_POST['username']) || !empty($_POST['password'])){
- if($_POST['password'] == $_POST['password2']){
- //check if username already exists
- $user = User::find_by_username($_POST['username']);
- if($user->username != $_POST['username']){
- $user->insert('portal', $_POST['username'], $_POST['password']);
- header("location: " . $config['base_url'] . "login.php");
- exit();
- } else {
- echo "username already exists";
- }
- }
- }
- }
- ?>
- <!DOCTYPE html>
- <html>
- <head>
- <title>Portal</title>
- <link href="css/style.css" rel="stylesheet" />
- </head>
- <body>
- <?php
- if(!$_SESSION['is_logged_in']){
- switch($_GET['action']){
- case 'signup':
- ?>
- <form action="index.php" method="post">
- <label for="username">Username</label>
- <input type="text" name="username" / autofocus>
- <label for="password">Password</label>
- <input type="password" name="password" />
- <label for="password2">Confirm Password</label>
- <input type="password" name="password2" />
- <input type="submit" value="Sign Up" name="signup">
- </form>
- </div>
- <?php
- break;
- default:
- ?>
- <div id="container">
- <form method="post" action="index.php" >
- <fieldset>
- <legend accesskey="l">Login</legend>
- <label for="username">Username :</label>
- <input type="text" name="username" autofocus required />
- <label for="password">Password :</label>
- <input type="password" name="password" />
- <input type="submit" value="Login" name="submit" />
- <a href="index.php?action=signup">Sign Up</a>
- </fieldset>
- </form>
- </div>
- <?php
- break;
- }
- } else { ?>
- <?php
- require_once('template/header.php');
- if(isset($_GET['action'])){
- switch($_GET['action']){
- case 'read':
- require_once('template/header.php');
- $user = new User();
- $display_all = $user->find_all();
- foreach($display_all as $values){
- echo $values['id'] . " ";
- echo $values['username'] . " ";
- echo $values['password'] . " ";
- echo "<a href=" . $config['base_url'] . "index.php?action=update&id=${values['id']}>Update</a>" . " ";
- echo "<a href=" . $config['base_url'] . "index.php?action=delete&id=${values['id']}>Delete</a>";
- echo "<br />";
- echo "<hr />";
- }
- require_once('template/footer.php');
- break;
- case "update":
- $fields = array();
- $fields = $_POST;
- $user = User::find_by_id($_GET['id']);
- $user->id = $_GET['id'];
- $update_user = $user->update($user->id, $fields);
- ?>
- <form action="index.php?action=update&id=<?php echo $user->id; ?>" method="post">
- <label for='username'>Username</label>
- <input type='text' name='username' value='<?php echo $user->username; ?>'/>
- <label for='password'>Password</label>
- <input type='password' name='password' value='<?php echo $user->password; ?>' />
- <input type="submit" value="Update" name="update">
- </form>
- <?php
- break;
- case 'delete':
- $del = new User();
- $user = User::find_by_id($_GET['id']);
- if($user){
- $del->delete($user->id);
- header("location: " . $config['base_url'] . "home.php?action=read");
- }
- break;
- ?>
- <?php
- case "signup":
- ?>
- <?php
- break;
- case 'logout':
- require_once('config.php');
- session_start();
- unset($_SESSION['is_logged_in']);
- unset($_SESSION['username']);
- exit();
- break;
- default:
- break;
- }
- }
- }
- require_once('template/footer.php');
- ?>
- </body>
- </html>
Add Comment
Please, Sign In to add comment