Hi.baidu.comkeepnetblogitemada25d1caad26d144134255c.html single-point logon is a required function for large-capacity systems. There are several expensive commercial systems on the market. I am afraid I cannot afford it if it is not rich. How can this function be implemented easily and economically? We will discuss a feasible solution here. Currently developed Web applications
Http://hi.baidu.com/keepnet/blog/item/ada25d1caad26d144134175c.html Single Sign-on is a large capacity system essential function, there are several expensive commercial systems on the market, if not rich, I am afraid can not afford. How can this function be implemented easily and economically? We will discuss a feasible solution here. Currently developed Web applications
Http://hi.baidu.com/keepnet/blog/item/ada25d1caad26d144134175c.html
Single Sign-on is a must-have function for large-capacity systems. There are several expensive commercial systems on the market. I am afraid I will not be able to afford it if it is not rich.
How can this function be implemented easily and economically? We will discuss a feasible solution here.
In the development of Web applications, Apache + PHP + MySQL is a required architecture for small and medium-sized enterprises to reduce costs. Here we will implement Single-point login for PHP, this economic architecture can be extended to the cluster server layer.
Our idea is to store the PHP Session data in a centralized manner. In this way, for PHP running on different servers, there is only one common Session database, the Session data generated by A login on server A can be shared on server B, server C, server D, and other servers, thus avoiding multiple logins. However, because PHP sessions require cookies, and cookies are related to domain names, each server using this scheme needs to have the same domain name (at least the same second-level domain name ), for example:
1. the domain names of all servers are www.766.com, which can be implemented through DNS round robin. In this case, set the Cookie domain name to www.766.com in PHP;
2. All server domain names are at the end of .766.com, such as a.766com and B .766.com. In this case, set the Cookie domain name to .whybsd.com in PHP to share the Cookie.
We have solved the prerequisites. Now let's take a look at the PHP Session storage method. In the instructions in the PHP manual, there is a function named session_set_save_handler, this function is used to register the User-Defined Session data storage interface.
The following example is provided in the PHP manual:
function open($save_path, $session_name)
{
global $sess_save_path;
$sess_save_path = $save_path;
return(true);
}
function close()
{
return(true);
}
function read($id)
{
global $sess_save_path;
$sess_file = "$sess_save_path/sess_$id";
return (string) @file_get_contents($sess_file);
}
function write($id, $sess_data)
{
global $sess_save_path;
$sess_file = "$sess_save_path/sess_$id";
if ($fp = @fopen($sess_file, "w")) {
$return = fwrite($fp, $sess_data);
fclose($fp);
return $return;
} else {
return(false);
}
}
function destroy($id)
{
global $sess_save_path;
$sess_file = "$sess_save_path/sess_$id";
return(@unlink($sess_file));
}
function gc($maxlifetime)
{
global $sess_save_path;
foreach (glob("$sess_save_path/sess_*") as $filename) {
if (filemtime($filename) + $maxlifetime < time()) {
@unlink($filename);
}
}
return true;
}
session_set_save_handler("open", "close", "read", "write", "destroy", "gc");
session_start();
// proceed to use sessions normally
?>
According to this idea, we only need to write our own processing functions and register them accordingly, so we can implement custom storage of PHP Session data.
You can use multiple solutions for specific implementation. For example, you can use NFS to store Session data to a unified network device, or you can save Session data to a database, connect all servers to the shared database (such as MySQL.
Well, it's simple and economical.
More considerations:
1. performance needs to be considered, especially when the number of servers (causing resource usage) and the number of users (causing Session volume) are very large.