dp152153 2013-01-01 22:14
浏览 134
已采纳

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?

  • 写回答

3条回答 默认 最新

  • doumeng1897 2013-01-01 22:43
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题