2

i need to avoid the creation of the session in the application unless it is completely necessary.

I have noticed that xajax calls dont work properly if the session is not started :(. My first approach was to create the session (if it doesn't exist) at the begining of the xajax function, however, it doesn't work the first time the user invokes the call (it works the second time since the session was created).

There is any way to handle/fix this situation?

Edit: an example code:

function example ($parameters) {
    if (!isset($_COOKIE["PHPSESSID"])) {
        session_start(); // we create the session if it didn't exist previously 
    }
    $response = new XajaxResponse();
    .....
    return $response;
}

My idea is to create the session when the user makes an ajax call. With this situation, the first time i call the "example" function it doesnt work. The second it goes ok, i think because the session was created.

EDIT: Hello, i have noticed a problem under chrome and explorer :(. The first ajax call is not received (i get not answer). Than means the user needs to click twice in order to receive the properly answer (with a popup for example)

Thanks!

4

2 に答える 2

3

問題は、設定されている場合に呼び出していないため、現在の ajax リクエストに対してセッションが初期化されていないことにあるようです。セッションを使用するすべてのスクリプトを呼び出す必要があります。セッションの初期化だけではありません。session_start()$_COOKIE['PHPSESSID'] session_start()

function example ($parameters) {
    // If this function uses the session, you MUST call session_start()
    // Don't do it conditionally.
    session_start(); 

    $response = new XajaxResponse();
    .....
    return $response;
}

すべての ajax ハンドラー関数がセッションを利用する場合は、session_start()それらを含むファイルの先頭で呼び出すこともできます。ajax 呼び出しの前にセッションをロードしたくない場合は、それらを独自の PHP スクリプトに分離し、session_start()最初にロードするメイン スクリプトを呼び出さないでください。

于 2012-10-09T13:24:21.717 に答える
3

セッションの場合、ページをリロードせずにページをリロードする必要がありますセッションは作成できません.....そして、Ajaxはページ全体をリロードせずに便利です。

于 2012-10-09T13:36:48.720 に答える