donglun4521 2019-04-16 18:28
浏览 190

为什么我的代码使用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.

  • 写回答

1条回答 默认 最新

  • douchongbang6011 2019-04-19 09:32
    关注

    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.

    评论

报告相同问题?

悬赏问题

  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM