Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

session_start hangs

Tags:

php

session

since a few hours our server hangs every time you do a session_start.

For testing purposes i created a script which looks like this:

<?php
session_start();
?>

Calling it from the console hangs and it can't even be stopped with ctrl-c, only kill -9 works. The same for calling it via Apache. /var/lib/php/session/ stays empty but permissions are absolutely fine, www can write and also has read permissions for all parent folders.

According to the admins there were no changes made on the server and there is no special code registered for sessions. The Server is CentOS 4 or 5 and yesterday everything was working perfectly. We rebooted the server and updated PHP, but nothing changed.

I've ran out of ideas, any suggestions?

UPDATE

We solved this problem by moving the project to another server, so while the problem still exists on one server there is no immediate need for a solution anymore. I will keep the question open in case someone has an idea for others having a similar problem in the future, though.

like image 632
Morfildur Avatar asked Dec 02 '10 08:12

Morfildur


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 use 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 $_ session in PHP?

PHP $_SESSION is an associative array that contains all session variables. It is used to set and get session variable values.

What is PHP session and how it works?

PHP responds by sending a unique token that identifies the current session. This is known as the session ID. In all subsequent requests, the browser sends the session ID to say, "Hey, it's me again." All other data related to the session is stored on the web server. Only the session ID gets passed back and forth.


2 Answers

There are many reasons for that, here are a few of them:

A. The session file could be opened exclusively. When the file lock is not released properly for whatever reason, it is causing session_start() to hang infinitely on any future script executions. Workaround: use session_set_save_handler() and make sure the write function uses fopen($file, 'w') instead of fopen($file, 'x')

B. Never use the following in your php.ini file (entropie file to "/dev/random"), this will cause your session_start() to hang:

<?php
ini_set("session.entropy_file", "/dev/random");
ini_set("session.entropy_length", "512");
?>

C. session_start() needs a directory to write to.

You can get Apache plus PHP running in a normal user account. Apache will then of course have to listen to an other port than 80 (for instance, 8080).

Be sure to do the following things: - create a temporary directory PREFIX/tmp - put php.ini in PREFIX/lib - edit php.ini and set session.save_path to the directory you just created

Otherwise, your scripts will seem to 'hang' on session_start().

like image 113
Thariama Avatar answered Oct 12 '22 04:10

Thariama


If this helps:

In my scenario, session_start() was hanging at the same time I was using the XDebug debugger within PHPStorm, the IDE, on Windows. I found that there was a clear cause: Whenever I killed the debug session from within PHPStorm, the next time I tried to run a debug session, session_start() would hang.

The solution, if this is your scenario, is to make sure to restart Apache every time you kill an XDebug session within your IDE.

like image 6
Dan Nissenbaum Avatar answered Oct 12 '22 05:10

Dan Nissenbaum