Why can't I reload the page with header()?

Viewed 40

I want to reload the page after users post string to the server in order to avoid posting the same things. I tried to use header() if users post something with POST method with the following code:

if ($_SERVER['REQUEST_METHOD'] == "POST") {
    if (isset($_SESSION['user'])) {
        date_default_timezone_set('Asia/Tokyo');
        if (strlen($_REQUEST['new-tweet'])) {
            $stmt = $pdo->prepare('INSERT INTO tweets values(?, ?, ?, ?, ?, ?)');
            if ($stmt->execute([null, $_REQUEST['new-tweet'], $_SESSION['user']['username'], $_SESSION['user']['profilepic'], date('Y-m-d H:i:s'), $_SESSION['user']['id']])) {
                header('Location:./index.php');
                exit();
            } else {
                echo 'Something went wrong<br>';
                print_r ($stmt -> errorInfo());
            }
        } else {
            echo '<script>alert("Your tweet has not been entered yet.")</script>';
        }
    } else {
        echo '<script>alert("You are not logged in");</script>';
    }
}

However, it doesn't show the new string which users just submitted and it seems like the header() function doesn't work. Any solutions? The entire code:

<html>
<head>
    <meta charset="utf-8">
    <title>Prototype</title>
    <link rel="stylesheet" href="stylesheet.css">
    <?php
    session_start();
    $pdo = new PDO('mysql:host=localhost;dbname=tweet;charset=utf8;', 'admin', 'password');
    ?>
</head>
<body>
    <header>
        <div class="logo">
            <h2>Prototype</h2>
            <a class="headerlinks" href="./index.php">Home</a>
            <a class="headerlinks" href="./profile.php">Profile</a>
        </div>
        <div class="search">
            <div class="breakwater1">
            <?php
                if (isset($_SESSION['user'])) {
                    echo 'Signed in as <a href="./signout.php">', $_SESSION['user']['username'], '</a>';
                } else {
                    echo '<a href="./signin.php">Sign In</a>';
                }
            ?>
            </div>
            <form action="search-output.php" method="post" class="headersearch">
                <input type="text" name="search" placeholder="Search">
                <input type="submit" value="Search">
            </form>
        </div>
    </header>
    <main>
        <div class="contents">
            <div class="information">
                <p class="titletext">Your Account</p>
                <?php
                if (!isset($_SESSION['user'])) {
                    echo '<div class="infobar1">';
                    echo '<a href="./signin.php">Sign In</a> or <a href="createaccount.php">Create a new account</a>’;
                    echo '</div>';
                }
                ?>
                <div class="account-info">
                    <?php
                    if (isset($_SESSION['user'])) {
                        echo '<img src="', $_SESSION['user']['profilepic'], '" class="prof">';
                        echo '<p class="user">', $_SESSION['user']['username'], '</p>';
                    }
                    ?>
                </div>
            </div>
            <div class="tl">
                <p class="title2">Textarea</p>
                <form action="" method="post">
                    <textarea name="new-tweet" id="text1"></textarea>
                    <input type="submit" value="tweet">
                </form>
                <hr class="division">
                    <div class="timeline">
                        <?php
                        $timeline = $pdo->query('SELECT * FROM tweets');
                        foreach ($timeline as $row) {
                            echo '<div class="breakwater2">';
                            echo '<div class="tweet">';
                            echo '<a href="./user/'.$row['uploader'].'.php">';
                            echo '<img src="', $row['avatar'], '" class="avatar1">';
                            echo '<div class="cont">';
                            echo '<b class="username">', $row['uploader'], '</b></a>';
                            echo '<p class="contents1">', $row['contents'], '</p>';
                            echo '<p class="time">', $row['time'], '</p>';
                            echo '</div>';//cont
                            echo '</div>';//tweet
                            echo '<input type="submit" value="Delete">';
                            echo '</div>';
                            echo '<hr class="division">';
                        }
                        ?>
                        <?php
                        if ($_SERVER['REQUEST_METHOD'] == "POST") {
                            if (isset($_SESSION['user'])) {
                                date_default_timezone_set('Asia/Tokyo');
                                if (strlen($_REQUEST['new-tweet'])) {
                                    $stmt = $pdo->prepare('INSERT INTO tweets values(?, ?, ?, ?, ?, ?)');
                                    if ($stmt->execute([null, $_REQUEST['new-tweet'], $_SESSION['user']['username'], $_SESSION['user']['profilepic'], date('Y-m-d H:i:s'), $_SESSION['user']['id']])) {
                                        header('Location:./signout.php');
                                        exit();
                                    } else {
                                        echo 'Something went wrong<br>';
                                        print_r ($stmt -> errorInfo());
                                    }
                                } else {
                                    echo '<script>alert("Your tweet has not been entered yet.")</script>';
                            }
                            } else {
                                echo '<script>alert("You are not logged in");</script>';
                            }
                        }
                        ?>
                    </div>
            </div>
        </div>
    </main>
</body>
0 Answers
Related