Advertisement
Guest User

Untitled

a guest
Oct 18th, 2017
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.73 KB | None | 0 0
  1. <?php
  2.  
  3. session_start();
  4.  
  5. $mysqli = new mysqli('localhost', 'testblog', 'password', 'testblog');
  6.  
  7. try {
  8.     if ($mysqli->connect_error) {
  9.         echo "<h5 class='red-text center'>Can't connect to the database!</h5><br>";
  10.         throw new Exception($conn->connect_error);
  11.     }
  12.  
  13. } catch (Exception $e){
  14.     echo $e->getMessage(), "\n";
  15. }
  16.  
  17. function makeNewPost() : bool
  18. {
  19.     $stmt = $mysqli->prepare("INSERT INTO posts VALUES (null, ?, ?, ?, NOW());");
  20.     $stmt->bind_param('sss', $_POST['title'], $_POST['text'], $_POST['image']);
  21.  
  22.     if ($stmt->execute()) {
  23.         return true;
  24.     } else {
  25.         return false;
  26.     }
  27. }
  28.  
  29. function correctCredentials() : bool
  30. {
  31.     if (isset($_POST['user']) && isset($_POST['pass'])) {
  32.         if ($_POST['user'] == 'username' && $_POST['pass'] == 'password') {
  33.             return true;
  34.         } else {
  35.             return false;
  36.         }
  37.     } else {
  38.         return false;
  39.     }
  40. }
  41. ?>
  42. <html lang="en">
  43. <head>
  44.     <meta charset="UTF-8">
  45.     <meta name="viewport" content="width=device-width, initial-scale=1.0">
  46.     <meta http-equiv="X-UA-Compatible" content="ie=edge">
  47.     <title>Dashboard</title>
  48.  
  49.     <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/materialize/0.100.2/css/materialize.min.css">
  50.     <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
  51.     <link rel="stylesheet" href="css/main.css">
  52. </head>
  53. <body>
  54.     <div class="ui container">
  55. <?php
  56. if (isset($_SESSION['isLoggedIn'])) {
  57.     if (isset($_POST['title']) && isset($_POST['text']) && isset($_POST['image'])) {
  58.         makeNewPost();
  59.         if (!makeNewPost()) {
  60.             echo '<h5 class=\'red-text center\'>Error creating post!</h5>';
  61.         } else {
  62.             header('Refresh:0; url=index.php');
  63.         }
  64.     } else {
  65.         ?>
  66.         <br>
  67.         <div class="container">
  68.             <div class="card-panel">
  69.                 <h4>New Post</h4>
  70.                 <div class="row">
  71.                 <form method="POST" action="index.php" class="col s12">
  72.                     <div class="row">
  73.                         <div class="input-field col s6">
  74.                             <i class="fa fa-book prefix"></i>
  75.                             <input id="Title" name="title" type="text" class="validate">
  76.                             <label for="Title">Title</label>
  77.                         </div>
  78.                         <div class="input-field col s12">
  79.                             <i class="fa fa-file-text prefix"></i>
  80.                             <input id="Text" name="text" type="text">
  81.                             <label for="Text">Text</label>
  82.                         </div>
  83.                         <div class="input-field col s12">
  84.                             <i class="fa fa-file-image-o prefix"></i>
  85.                             <input id="Image" name="image" type="text">
  86.                             <label for="Image">Image</label>
  87.                         </div>
  88.                         <div class="input-field col s12">
  89.                             <i class="fa fa-lock prefix"></i>
  90.                             <input id="password" name="pass" type="password" class="validate">
  91.                             <label for="password">Confirm Password</label>
  92.                         </div>
  93.                     </div>
  94.                     <button class="btn waves-effect waves-light right" type="submit" name="action">Submit
  95.                         <i class="fa fa-arrow-circle-right"></i>
  96.                     </button>
  97.                 </form>
  98.                 </div>
  99.             </div>
  100.         </div>
  101.         <?php
  102.     }
  103. } else {
  104.     if (correctCredentials()) {
  105.         $_SESSION['isLoggedIn'] = true;
  106.         header('Refresh:0');
  107.     } else {
  108.         ?>
  109.         <div class="container"><div class="container"><div class="container"><div class="container">
  110.         <br><br><br><br><br><br>
  111.             <div class="card-panel">
  112.                 <h4>Login</h4>
  113.                 <div class="row">
  114.                     <form class="col s12" method="POST" action="dashboard.php">
  115.                         <div class="row">
  116.                             <div class="input-field col s10">
  117.                                 <i class="fa fa-user-circle prefix"></i>
  118.                                 <input type="text" name="user"></input>
  119.                                 <label for="user">User Name</label>
  120.                             </div>
  121.                         </div>
  122.                         <div class="row">
  123.                             <div class="input-field col s10">
  124.                                 <i class="fa fa-lock prefix prefix"></i>
  125.                                 <input type="password" name="pass"></input><br/>
  126.                                 <label for="pass">Password</label>
  127.                             </div>
  128.                         </div>
  129.                         <input type="submit" name="submit" value="Go"></input>
  130.                     </form>
  131.                 </div>
  132.             </div>
  133.         </div></div></div></div>
  134.         <?php
  135.     }
  136. }
  137. /*
  138. if(isset($_POST['title']) && isset($_POST['text']) && isset($_POST['image'])){
  139.     $mysqli = new MySQLi('localhost', 'root', null, 'test');
  140.    
  141.     $stmt = $mysqli->prepare("INSERT INTO alphy VALUES (null, ?, ?, ?, NOW());");
  142.     $stmt->bind_param('sss', $_POST['title'], $_POST['text'], $_POST['image']);
  143.        
  144.     if ($stmt->execute()) {
  145.         echo "successfull";
  146.     } else {
  147.          echo "error";
  148.     }
  149. }
  150. */
  151. $mysqli->close();
  152. ?>
  153.     </div>
  154.     <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
  155.     <script src="https://cdnjs.cloudflare.com/ajax/libs/materialize/0.100.2/js/materialize.min.js"></script>
  156. </body>
  157. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement