php使用MySQL保存session會話的方法

本文實例講述了php使用MySQL保存session會話的方法。分享給你們供你們參考。具體分析以下:

在不少大的系統中通常都有這個功能,可是要分離出來分析,網上的資料也不太多 這裏我整理了一篇發出來與你們分享

使用MySQL保存session會話較files有不少優勢:

1) 有利於分佈式系統,files只能保存在一臺機器上

2) 有利於大訪問量的系統,使用files時每一個session保存在一個文件中,目錄會超級大,查找session文件會比較困難。

使用MySQL保存會話首先要建立session表

<?php
$hostname_login = "localhost"; // Server address
$username_login = "root"; // User name 
$password_login = ""; // Password
//
$data_name = "session"; // Database name
$login = mysql_pconnect($hostname_login, $username_login, $password_login) or trigger_error(mysql_error(),E_USER_ERROR); 
$sql="SHOW DATABASES LIKE '".$data_name."'"; // If it is exist
if($rs_table=mysql_query($sql,$login)) { 
  if($rs_value=mysql_fetch_array($rs_table)) { 
   echo "數據庫已經存在!\n!";
   exit(); 
  } 
}
$sql="CREATE DATABASE $data_name";   
mysql_query($sql); // Crate database
echo "數據庫建立成功!\n"; 
mysql_select_db($data_name, $login);
$sql="CREATE TABLE `sessions` ( 
`SessionKey` varchar(32) NOT NULL default '', 
`SessionArray` blob NOT NULL, 
`SessionExpTime` int(20) unsigned NOT NULL default '0', 
PRIMARY KEY (`SessionKey`), 
KEY `SessionKey` (`SessionKey`) 
) ENGINE=MyISAM DEFAULT CHARSET=utf8"; //新建數據庫 sql語句
mysql_query($sql);
echo "成功新建數據庫表!";
?>
<?php
$hostname_login = "localhost"; // Server address
$username_login = "root"; // User name 
$password_login = ""; // Password
//
$data_name = "session"; // Database name
$login = mysql_pconnect($hostname_login, $username_login, $password_login) or trigger_error(mysql_error(),E_USER_ERROR); 
$sql="SHOW DATABASES LIKE '".$data_name."'"; // If it is exist
if($rs_table=mysql_query($sql,$login)) { 
  if($rs_value=mysql_fetch_array($rs_table)) { 
   echo "數據庫已經存在!\n!";
   exit(); 
  } 
}
$sql="CREATE DATABASE $data_name";   
mysql_query($sql); // Crate database
echo "數據庫建立成功!\n"; 
mysql_select_db($data_name, $login);
$sql="CREATE TABLE `sessions` ( 
`SessionKey` varchar(32) NOT NULL default '', 
`SessionArray` blob NOT NULL, 
`SessionExpTime` int(20) unsigned NOT NULL default '0', 
PRIMARY KEY (`SessionKey`), 
KEY `SessionKey` (`SessionKey`) 
) ENGINE=MyISAM DEFAULT CHARSET=utf8"; //新建數據庫 sql語句
mysql_query($sql);
echo "成功新建數據庫表!";
?>

session_module_name

(PHP 4, PHP 5)php

session_module_name — 獲取/設置會話模塊名稱mysql

說明 ¶

string session_module_name ([ string $module ] )web

session_module_name() 獲取或設置會話模塊名稱。redis

參數 ¶

  • modulesql

  • 若是指定 module 參數,則使用 指定值做爲會話模塊。數據庫

返回值 ¶

返回當前所用的會話模塊名稱。session

add a note add a notesocket

User Contributed Notes 3 notes

up分佈式

downfetch

0

Anonymous ¶

2 months ago

was looking for a rather comprehensive list of modules, and foundhttp://stackoverflow.com/questions/8415962/what-exactly-phps-function-session-module-name-is-for but there are more.

up

down

0

raees at steelbrain dot com dot pk ¶

7 months ago

This function is used to set the Session Module at site or script level.

The global configuration can be done in php.ini under the [Session] section and with the name of "session.save_handler". The sessions are saved in files by default, like so:
session.save_handler = files

But with this configuration you set one of your websites to use some other session module (if you have them installed and extension loaded with PHP), like so:
<?php

// NOTE: You must use this function before starting session with session_start(); to make it work properly
session_module_name('memcache'); // or pgsql or redis etc

// You'll need to define a save path also, if the module is other than files, like so:
session_save_path('localhost:11211'); // memcache uses port 11211

// or you can use multiple for load balancing:
session_save_path('localhost:11211:41,otherhost:11211:60'// First part is hostname or path to socket, next is port and the last is the weight for that server

//The function also returns the value of the current session module.
echo session_module_name(); // will print memcache in our case

// or maybe a check
if(session_module_name() != 'memcache'){
  
// Do something, throw an exception maybe
}

up

down

-3

ts9904247885 at gmail dot com ¶

1 year ago

<?php
session_start
();

$_SESSION['name']="Tushar";
$n=$_SESSION['name'];
$_SESSION['Age']="23";
$_SESSION['city']="Tarapur";

//echo session_encode()."<br/>";//Prints all Session Data
//echo session_is_registered($n);
echo session_module_name();//it prints "files"

?>output:files

相關文章
相關標籤/搜索