Advertisement
TechGeek

index.php

Sep 16th, 2020 (edited)
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.95 KB | None | 0 0
  1. <?php
  2. declare(strict_types = 1);
  3.  
  4. // Start session.
  5. session_start();
  6.  
  7. // Include helper functions.
  8. require_once 'helpers.php';
  9.  
  10. // Redirect user to login page if not authenticated.
  11. if (!check_auth())
  12. {
  13.     redirect('login.php');
  14.     return;
  15. }
  16.  
  17. // does below line looks right? since I need to call this only button click
  18. if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['submit']))
  19. {
  20.     if (!is_authorized_to_write())
  21.     {
  22.         alert("Your write access is revoked since another user is logged in now");
  23.         return;
  24.     }
  25.  
  26.     // Your code here...
  27.     if (isset($_POST['field1']) && isset($_POST['field2']))
  28.     {
  29.         $data = $_POST['field1'] . '-' . $_POST['field2'] . "\r\n";
  30.         $ret = file_put_contents('mydata.txt', $data, LOCK_EX);
  31.         if ($ret === false)
  32.         {
  33.             alert("Error writing into the file");
  34.         }
  35.         else
  36.         {
  37.             alert("$ret bytes written to file");
  38.         }
  39.     }
  40.     else
  41.     {
  42.         alert("No post data to process");
  43.     }
  44. }
  45.  
  46. // created function to alert in popup window
  47. function alert($msg) {
  48.     echo "<script type='text/javascript'>alert('$msg');</script>";
  49. }
  50. ?>
  51. <!doctype html>
  52. <html lang="en">
  53. <head>
  54.     <meta charset="UTF-8">
  55.     <meta name="viewport"
  56.           content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
  57.     <meta http-equiv="X-UA-Compatible" content="ie=edge">
  58.     <title>Home</title>
  59. </head>
  60. <body>
  61.     <div>
  62.         <h1>Website Title</h1>
  63.         <a href="logout.php">Logout</a>
  64.     </div>
  65.     <div>
  66.         <p>Welcome back, <?=$_SESSION['user_id'] ?>!</p>
  67.     </div>
  68.  
  69.     <form action="#" method="POST">
  70.         <input type="text" name="field1" />
  71.         <input type="text" name="field2" />
  72.         <input type="submit" name="submit" value="Save Data">
  73.     </form>
  74.  
  75.     <script src="https://code.jquery.com/jquery-3.5.1.min.js"></script>
  76. </body>
  77. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement