0

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.

marlo
  • 6,998
  • 5
  • 28
  • 34
nizon1337
  • 3
  • 2
  • session_start should be on beginning/top of the file . where is your code located ? https://stackoverflow.com/questions/20308478/where-exactly-do-i-put-a-session-start – zod Apr 16 '19 at 18:41
  • Its located on the top – nizon1337 Apr 16 '19 at 18:47
  • What is value of `clicks`? , everytime you are adding it by one ? – Swati Apr 16 '19 at 18:49
  • Yes. Currently its 12, if I open the php file its 14 – nizon1337 Apr 16 '19 at 18:54
  • There is nothing in your question that would explain why it increments in steps of 2. What debugging have you done? Is this your full code? – Dharman Apr 16 '19 at 19:19
  • This is not the fll code, just a code to test. I dont run the query a second time anywhere. I made the same on my register page, and there it works. What for a debugging you mean? – nizon1337 Apr 16 '19 at 19:23
  • `var_dump()`? put a breakpoint if you have debugger setup and follow line by line. Can you show us full code or at least more of it? – Dharman Apr 16 '19 at 19:24
  • Maybe you have another redirect somewhere? Maybe there is something that calls your code twice. Try removing the code which is not crucial to this task and see at which point it will start working properly. – Dharman Apr 16 '19 at 19:29
  • Your problem is likely an issue with your webserver redirecting and loading the file twice. Check your access logs to see if you get two entries when you load the file. If so, you either have an extraneous redirect in your codebase, or an issue with your .htaccess/vhost.conf/httpd.conf. – mopsyd Apr 16 '19 at 19:59
  • Hi, there are two entries in the access log. There is no redirect in the code. What can I change in the vhost.conf/httpd.conf? – nizon1337 Apr 17 '19 at 16:30
  • Have you checked by assigning any static value in the seesion instead of $id? – Arshad Hussain Apr 19 '19 at 09:46

1 Answers1

0

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.

marlo
  • 6,998
  • 5
  • 28
  • 34