Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP Session_start is hanging

Tags:

php

session

Kind of a weird issue, ok here is my setup...

  1. domain.com calls reads from an Iframe on sub.domain.com
  2. sub.domain.com makes an ajax call to sub.domain.com/call.php
  3. sub.domain.com returns ajax call to domain.com

AKA long-polling

Now, everything works perfectly when there is no session data (I close the browser and restart the page). However, once I reload the page and their is session data, call.php does a start_session() and hangs there.

I have tried almost everything and can't figure this out. I've tried destroying the session, unsetting all the session variables, modifying some ini settings, and nothing has worked.

Here is the code of call.php where the session data is...

session_start();

        $sql = ("SELECT userid FROM status WHERE typing = '".mysql_real_escape_string($userid)."'");
        $result = mysql_query($sql);

        if ($result && mysql_num_rows($result) > 0) {
            $row = mysql_fetch_array($result);
            $typing_id = $row['userid'];
            if (!empty($typing_id)) {
                if (isset($_SESSION['typing2'])) {
                    unset($_SESSION['typing2']);
                }
            } else {
                $typing_id = "-1";
            }
        } else {
            $typing_id = "-1";
            if (isset($_SESSION['typing'])) {
                unset($_SESSION['typing']);
            }
        }

        if ($_SESSION['typing'] != $typing_id && !isset($_SESSION['typing2']) || $initialize == "1") {
            $typing = array('typing_id' => $typing_id);
        }

        if ($typing_id == "-1") {
            $_SESSION['typing2'] = "-1";
        } else {
            $_SESSION['typing'] = $typing_id;
        }

Does anyone have any ideas? I was thinking it might have to do with the domain but I'm not sure.

Thanks!

like image 431
Chris Avatar asked Feb 24 '10 04:02

Chris


People also ask

What does session_start () do in PHP?

session_start() creates a session or resumes the current one based on a session identifier passed via a GET or POST request, or passed via a cookie. When session_start() is called or when a session auto starts, PHP will call the open and read session save handlers.

Do I need to call session_start on every page?

It must be on every page you intend to use. The variables contained in the session—such as username and favorite color—are set with $_SESSION, a global variable. In this example, the session_start function is positioned after a non-printing comment but before any HTML.

What is PHP session_start () and Session_destroy () function?

session_destroy() destroys all of the data associated with the current session. It does not unset any of the global variables associated with the session, or unset the session cookie. To use the session variables again, session_start() has to be called. Note: You do not have to call session_destroy() from usual code.

How long do PHP sessions last?

By default, session variables last until the user closes the browser. So; Session variables hold information about one single user, and are available to all pages in one application.


1 Answers

I actually found out (after hours and hours of debugging and research) that the problem is being caused because the PHP session locks up. Then, when the new page loads, it won't work until the old session times out. A session_write_close() will fix it.

like image 105
Chris Avatar answered Sep 18 '22 16:09

Chris