1

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.

Fox
  • 25
  • 5
  • 8
    chrome and firefox can not evaluate php-code differently, because php is executed completely **server-side**. chrome and firefox *never even see* one line of php, and wouldn't know what to do with it except display as text. – Franz Gleichmann Nov 15 '16 at 14:39
  • 1
    As far as I know php does not run on the browser, it runs on the server which feeds the browser with a html page. So, that means, in my opinion that it's the server that does different, not the browsers – Andreas Nov 15 '16 at 14:39
  • Show the html code you are using. As Franz stated the browser doesn't see any PHP, something in your html is being displayed differently. – Blinkydamo Nov 15 '16 at 14:40
  • Fox, post the `
    ` so we can test your code. And where are those `$_SESSION` variables created ("message", "code")?
    – Jose Manuel Abarca Rodríguez Nov 15 '16 at 14:48

1 Answers1

3

Your problem is here:

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

For some reason Chrome send this HTTP_CACHE_CONTROL header for your script and you really shouldn't rely on this in case of if data was added or not. This is wrong way. To check if data were added, you can

just check first in database if they exists,

or you can add some key to session after send the form and check if this key exists already in session or

or just redirect page after proper adding data into database.

There is a lot of ways to do it, just use google. Using 'HTTP_CACHE_CONTROL' is just one big mistake

nospor
  • 4,190
  • 1
  • 16
  • 25
  • Thank you for your suggestions. With what you have given me and the related link from @raina77ow I can see that I need to change the way I check for a page refresh. The link is very helpful and I will be using some of the code from there. – Fox Nov 15 '16 at 15:54