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 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应