Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the best way to handle sessions for a PHP site on multiple hosts? [closed]

Tags:

People also ask

What is session management PHP?

A session is a way to store information (in variables) to be used across multiple pages. Unlike a cookie, the information is not stored on the users computer.

How PHP session is created and destroyed explain with an example?

Destroying a PHP SessionA PHP session can be destroyed by session_destroy() function. This function does not need any argument and a single call can destroy all the session variables. If you want to destroy a single session variable then you can use unset() function to unset a session variable.

Where are sessions stored PHP?

PHP Session Start By default, session data is stored in the server's /tmp directory in files that are named sess_ followed by a unique alphanumeric string (the session identifier). By itself, the session_start() function doesn't add much functionality to a web page.

How can we retrieve data from database using session in PHP?

php session_start(); include "dbconnect. php"; $email = $_SESSION['email']; $query = "SELECT uid FROM master WHERE emailid = '". $email. "'"; $result = mysql_query($query); if(mysql_num_rows($result)>0) { $row = mysql_fetch_array($result); $uid = $row["uid"]; echo $uid; } else { echo "No record found"; } ?>


PHP stores its session information on the file system of the host of the server establishing that session. In a multiple-host PHP environment, where load is unintelligently distributed amongst each host, PHP session variables are not available to each request (unless by chance the request is assigned to the same host -- assume we have no control over the load balancer).

This site, dubbed "The Hitchhikers Guide to PHP Load Balancing" suggests overriding PHPs session handler and storing session information in the shared database.

What, in your humble opinion, is the best way to maintain session information in a multiple PHP host environment?

UPDATE: Thanks for the great feedback. For anyone looking for example code, we found a useful tutorial on writing a Session Manager class for MySQL which I recommend checking out.