mardi 15 novembre 2016

Chrome evaluates php if statement differently to Firefox

I am attempting to create a set of if statements that check for certain conditions and return a value. The problem that I have come across is that I have been designing this and running it in Firefox, but I have tried it in Chrome and I am presented with a different result from the statements.

What I am attempting to do with the if statements is, a user fills out a form on a page and is then sent to another page which will:

  1. Check content of values and if empty, produce a message saying "You should not be here" - (as if someone directly accessed the page)
  2. If the values are not empty, check if the page has been refreshed and produce a message "This has already been entered into the database" - (so it is not re-entered)
  3. If the values are not empty (as in 1) and the page has not been refreshed (as in 2) then store the data into the database and produce a message "Success"

When I fill out the form and am taken to this page, it runs through the code and displays "Success" as it should. But when I run it in exactly the same way in Chrome, I get the message "This has already been entered into the database" - as if the page had been refreshed.

The code I am using (minus all the layout code) is:

<?php session_start();

$seen = 0;
$pageWasRefreshed = isset($_SERVER['HTTP_CACHE_CONTROL']) && $_SERVER['HTTP_CACHE_CONTROL'] === 'max-age=0';

$message = $_SESSION['message'];
$code = $_SESSION['code'];

require 'inc/database.php';

if (!(isset($message) && ($message!==null))) {
    echo 'You should not be here!';
} else {

    $message = strip_tags(trim($_SESSION['message']));

    if ($pageWasRefreshed) {

        echo 'This has already been entered into the database';

    } else {

        $stmt = $conn->prepare('INSERT INTO data (code, message) VALUES (:code, :pmessage)');
        $stmt->execute(array(
            'pcode' => $code,
            'pmessage' => $message,
        ));

    $last_id = $conn->lastInsertId();

    echo $last_id . 'Success!';

    }
}

I am (obviously) learning as I go and using nested if statements is getting me a bit confused so it might be something simple, but as it works in Firefox and not in Chrome I don't know how to work my way through to find out where it's going wrong.

Aucun commentaire:

Enregistrer un commentaire