jquery,sessions&php session_id();

jquery,sessions&php session_id();

问题描述:

I have a session_id which I am using to identify the user in a shopping basket type application.

The session_id is stored in the database as the session id.

When I update the cart, the session_id, product_id and qty are stored in the Basket table.

I am trying to add an item to the Basket, then show the updated basket on the screen without page refresh using jquery.

Do I store the session id in a hidden html field or is there a better way of getting jQuery to know what the session_id is?

UPDATE: Ok, so now based on answers and comments below, I don't really want to be using the session id in a hidden field. I certainly don't want to show the session id within the html page.

I may need to backtrack a little...

When ajax is calling my update basket function, I am faced with the following error...

Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at ..... /ajaxData.php on line 105

The code I am using to start the session is...

if (!session_id()){
session_start();
}

I don't really understand what this warning means, so this is why I was thinking of sending the session id via the hidden html field.

I guess the solution is to fix what the warning was and try to do things properly...

Any suggestions on my warning message?

我有一个session_id,用于识别购物篮类型应用程序中的用户。 p> \ n

session_id作为会话ID存储在数据库中。 p>

当我更新购物车时,session_id,product_id和qty存储在Basket表中。 p>

我正在尝试将项目添加到购物篮,然后在屏幕上显示更新的购物篮而不使用jquery进行页面刷新。 p>

我是否存储会话 在一个隐藏的html字段中的id还是有更好的方法让jQuery知道session_id是什么? p>

更新: strong> Ok,所以现在基于答案 和下面的评论,我真的不想在隐藏字段中使用会话ID。 我当然不想在html页面中显示会话ID。 p>

我可能需要回溯一点...... p>

当ajax时 正在调用我的更新篮功能,我遇到以下错误... p>

 警告:session_start()[function.session-start]:无法发送会话缓存限制器 -  标头已经发送(输出从..... / ajaxData.php开始在第105行
  code>  pre> 
 
 

我用来启动会话的代码是......

  if(!session_id()){
session_start(); 
} 
  code>  pre> 
 
 

我不知道 真的明白这个警告意味着什么,所以这就是我想通过隐藏的html字段发送会话ID的原因。 p>

我想解决方法是修复警告的内容并尝试 正确地做事...... p>

对我的警告信息有任何建议吗? p> div>

IMO all session data must be managed in the server, if you really need it in the front-end get it asynchronously. For example:

1.- jquery stuff

$.get(
        window.location.href,
        {
            get_session_id : 1
        },
    function(data) {
    var session_id = data.session_id
    if ("" == session_id){
            // alert user
            // give a friendly way to proceed without loss user's time
            // return;
    }
    // use session_id
}, "json");

UPDATE To avoid Warning: session_start() [function.session-start]... error put this snippet early as possible in your script. BEFORE any html output.

2.- server stuff

// none output before this snippet

if ($_GET['get_session_id'])
{
    // get $session_id
    $output['session_id'] = $session_id ? $session_id : '';
    echo json_encode($output);
    exit;
}

UPDATE 2:

The reason I propose to you this solution is that I think it is better to get the session ID dynamically when needed, instead of having it stored in a static way, is that this identifier may be modified, deleted, etc., while the user takes his time to fill a form.
Unfortunately this defect is very common and annoying. Submit a form and instead of being processed, we are redirected to a login page, losing all data sent.

Use this JavaScript code (combined with PHP):

<script type="text/javascript">
var sessionid = "<?php echo session_id(); ?>";
</script>

Then use the sessionid variable in your jQuery (AJAX?) call.

You could use a data- attribute:

<form id="cart-form" action="" data-sessionid="<?php echo session_id(); ?>">
    <!-- rest of form code -->
</form>

Then in your jQuery, just use $(this).data('sessionid') to retrieve it:

$('#cart-form').submit(function() {
    var session_id = $(this).data('sessionid'); 
    // ... rest of your code here ...
});