Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- declare(strict_types = 1);
- // Start session.
- session_start();
- // Include helper functions.
- require_once 'helpers.php';
- // Redirect user to login page if not authenticated.
- if (!check_auth())
- {
- redirect('login.php');
- return;
- }
- // does below line looks right? since I need to call this only button click
- if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['submit']))
- {
- if (!is_authorized_to_write())
- {
- alert("Your write access is revoked since another user is logged in now");
- return;
- }
- // Your code here...
- if (isset($_POST['field1']) && isset($_POST['field2']))
- {
- $data = $_POST['field1'] . '-' . $_POST['field2'] . "\r\n";
- $ret = file_put_contents('mydata.txt', $data, LOCK_EX);
- if ($ret === false)
- {
- alert("Error writing into the file");
- }
- else
- {
- alert("$ret bytes written to file");
- }
- }
- else
- {
- alert("No post data to process");
- }
- }
- // created function to alert in popup window
- function alert($msg) {
- echo "<script type='text/javascript'>alert('$msg');</script>";
- }
- ?>
- <!doctype html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <meta name="viewport"
- content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
- <meta http-equiv="X-UA-Compatible" content="ie=edge">
- <title>Home</title>
- </head>
- <body>
- <div>
- <h1>Website Title</h1>
- <a href="logout.php">Logout</a>
- </div>
- <div>
- <p>Welcome back, <?=$_SESSION['user_id'] ?>!</p>
- </div>
- <form action="#" method="POST">
- <input type="text" name="field1" />
- <input type="text" name="field2" />
- <input type="submit" name="submit" value="Save Data">
- </form>
- <script src="https://code.jquery.com/jquery-3.5.1.min.js"></script>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement