简体   繁体   中英

Why is my Code running twice with session_start?

I created two files (test.php & test2.php).

test.php should increase the counter in the database. Then it should just redirect to test2.php

Everything works fine, but now I set a $_SESSION and now its increasing the counter by 2. Without the session_start its working perfect, but then it doesn't set my $_SESSION.

test.php

<?php
    if (!isset($_SESSION)) { session_start(); }

    include_once("dbtest.php");
    $id = mysqli_real_escape_string($conn, $_GET["id"]);

    mysqli_query($conn, "update users set clicks = clicks + 1 where id = ".$id);
    $_SESSION['test_id'] = $id;

    header("Location: test2.php");
?>

test2.php

<?php
    if (!isset($_SESSION)) { session_start(); }

    if(isset($_SESSION['test_id'])) {
       echo $_SESSION['test_id'];
    }
?>

It should increase the counter by 1.

It seems that it is about the OPTIONS method sent by the browser or client.

I think they call it "pre flight" internal check of the browser/client to see if the request has valid HEADERS .

https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/OPTIONS

To debug, Enter your browser/client's network debug with log persistence turned on.

To Answer the question:

Some of your code are executed even on OPTIONS HTTP method are sent. Same could happen even you are using PHP frameworks so it might also be due to the client or browser you are using.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM