Guest User

Untitled

a guest
Dec 6th, 2017
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.25 KB | None | 0 0
  1. <?php
  2. require_once('config.php');
  3. require_once('lib/database.php');
  4. require_once('lib/user.php');
  5. require_once('lib/auth.php');
  6.  
  7. session_start();
  8. //login
  9. if(isset($_POST['submit'])){
  10.     $check_auth = $auth->authenticate_user($_POST['username'] ,$_POST['password']);
  11.     if($check_auth){
  12.         $_SESSION['is_logged_in'] = TRUE;
  13.         $_SESSION['username'] = $_POST['username'];
  14.         header("Location: " . $config['base_url'] . "index.php");
  15.         exit();
  16.     } else {
  17.         $_SESSION['is_logged_in'] = FALSE;
  18.     }
  19. }
  20. //update user
  21.  
  22. if(isset($_POST['update'])){
  23.                            
  24.     if($update_user){
  25.         header("Location: " . $config['base_url'] . "index.php?action=read");
  26.         exit();
  27.     }
  28.  
  29. }
  30.  
  31. if(isset($_POST['signup'])){
  32.     if(!empty($_POST['username']) || !empty($_POST['password'])){
  33.         if($_POST['password'] == $_POST['password2']){
  34.             //check if username already exists
  35.             $user = User::find_by_username($_POST['username']);
  36.            
  37.             if($user->username != $_POST['username']){
  38.            
  39.                 $user->insert('portal', $_POST['username'], $_POST['password']);
  40.                 header("location: " . $config['base_url'] . "login.php");
  41.                 exit();
  42.                    
  43.             } else {
  44.                 echo "username already exists";
  45.             }
  46.         }  
  47.     }
  48. }
  49.  
  50. ?>
  51. <!DOCTYPE html>
  52. <html>
  53. <head>
  54.     <title>Portal</title>
  55.     <link href="css/style.css" rel="stylesheet" />
  56. </head>
  57. <body>
  58. <?php
  59. if(!$_SESSION['is_logged_in']){
  60.     switch($_GET['action']){   
  61.         case 'signup':             
  62. ?>
  63.             <form action="index.php" method="post">
  64.                 <label for="username">Username</label>
  65.                 <input type="text" name="username" / autofocus>
  66.                 <label for="password">Password</label>
  67.                 <input type="password" name="password" />
  68.                 <label for="password2">Confirm Password</label>
  69.                 <input type="password" name="password2" />
  70.                 <input type="submit" value="Sign Up" name="signup">
  71.             </form>
  72.             </div>
  73.         <?php
  74.         break;
  75.        
  76.         default:
  77.         ?>
  78.             <div id="container">
  79.                 <form method="post" action="index.php" >
  80.                     <fieldset>
  81.                         <legend accesskey="l">Login</legend>
  82.  
  83.                         <label for="username">Username :</label>
  84.                         <input type="text" name="username" autofocus required />
  85.                         <label for="password">Password :</label>
  86.                         <input type="password" name="password" />
  87.                         <input type="submit" value="Login" name="submit" />
  88.                         <a href="index.php?action=signup">Sign Up</a>
  89.                     </fieldset>
  90.                 </form>
  91.             </div>
  92.     <?php
  93.         break;
  94.     }
  95.  } else { ?>
  96. <?php
  97.     require_once('template/header.php');
  98.     if(isset($_GET['action'])){
  99.         switch($_GET['action']){
  100.             case 'read':
  101.                 require_once('template/header.php');
  102.                 $user = new User();
  103.                 $display_all = $user->find_all();
  104.                 foreach($display_all as $values){          
  105.                     echo $values['id'] . " ";
  106.                     echo $values['username'] . " ";
  107.                     echo $values['password'] . " ";
  108.                     echo "<a href=" . $config['base_url'] . "index.php?action=update&id=${values['id']}>Update</a>" . " ";         
  109.                     echo "<a href=" . $config['base_url'] . "index.php?action=delete&id=${values['id']}>Delete</a>";
  110.                     echo "<br />";
  111.                     echo "<hr />";             
  112.                 }
  113.                 require_once('template/footer.php');           
  114.             break;
  115.             case "update":
  116.                    
  117.                     $fields = array();
  118.                     $fields  = $_POST;
  119.                     $user = User::find_by_id($_GET['id']);
  120.                     $user->id = $_GET['id'];
  121.                     $update_user = $user->update($user->id, $fields);
  122.                 ?>
  123.                         <form action="index.php?action=update&id=<?php echo $user->id; ?>" method="post">
  124.                             <label for='username'>Username</label>
  125.                             <input type='text' name='username' value='<?php echo $user->username; ?>'/>
  126.                             <label for='password'>Password</label>
  127.                             <input type='password' name='password' value='<?php echo $user->password; ?>' />
  128.                             <input type="submit" value="Update" name="update">
  129.                         </form>
  130.                 <?php
  131.  
  132.             break; 
  133.             case 'delete':
  134.                 $del = new User();
  135.                 $user = User::find_by_id($_GET['id']);
  136.                 if($user){
  137.                     $del->delete($user->id);
  138.                     header("location: " . $config['base_url'] . "home.php?action=read");
  139.                 }
  140.             break; 
  141.                 ?>
  142.             <?php
  143.             case "signup":
  144.             ?>
  145.        
  146.         <?php  
  147.             break;
  148.            
  149.  
  150.             case 'logout':
  151.                 require_once('config.php');
  152.                 session_start();
  153.                 unset($_SESSION['is_logged_in']);
  154.                 unset($_SESSION['username']);
  155.    
  156.                 exit();
  157.             break;
  158.            
  159.             default:
  160.             break;
  161.         }
  162.     }
  163. }
  164. require_once('template/footer.php');
  165. ?>
  166. </body>
  167. </html>
Add Comment
Please, Sign In to add comment