0

我有一些选择字段和一个会话 ID,我需要将它们即时传递给一个 php 文件,该文件会将它们保存到一个数据库中。

假设我有这个代码:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>

<?php

// first I declare the session value
$_SESSION["sess_id"] =  uniqid($prefix = "", $more_entropy = TRUE);
$sess_id = $_SESSION["sess_id"];

// some rows forward, I have my select fields like this one
echo "<select id='num_1' name='num_1' onchange='set_num_1()'>";

?>

<script>
var num_1_val = "";
var session_id = '<?php echo $sess_id();?>';

function set_num_1() {

   num_1_val = document.getElementById('num_1').value;
   /* here I need to put the ajax code to send session_id and num_1_val to save_to_db.php */

   /* I suppose it's some code like follows: */
   $.ajax({
    type: "POST",
    url: "save_to_db.php",
    data: xxxxxxxx, /* ------------- here is where I fail
    success: function(){
        /* something to do back in case I need */;
    }
   });
}

如何正确地将 session_id 和 num_1_val 传递给 save_to_db.php?

是否有更安全的模式可以将 $_SESSION["sess_id"] 传递给 save_to_db.php 而不像现在这样使其可见?因为使用var session_id = '<?php echo $sess_id();?>';我使 $sess_id 值在页面脚本中可见。

你能帮帮我吗?谢谢小伙伴。

4

1 回答 1

0
var session_id = '<?php echo $sess_id();?>';

传递会话 ID 的最佳方法是使用Set-Cookie标头,请参见此处

您可以传递多个 cookie。但是 PHP 会自动为您执行此操作,所以我不确定这样做的目的是什么。

于 2020-04-20T16:18:51.227 回答