|
 |
|
การใช้ session ร่วมกัน ของเว็บไซต์ 2 เว็บ ซึ่งอยู่บน host เดียวกัน |
|
 |
|
|
 |
 |
|
หลักการเดียวกับ Load Balance ครับ คือจะต้องเปลี่ยน Session Handler ไปจัดเก็บไว้ที่เดียวกันครับ ใช้พวก memcache หรือจะเก็บ session ลงใน mysql database ก็ได้ครับ ลองดูพวก session_set_save_handler()
หลักการก็คือ ปกติแล้ว เราเขียน php และกำหนด session
Code (PHP)
<?php
session_start();
วิธีก็คือ ก่อนทีจะ start ก็ชี้ path ของ session ไปยังที่เดียวกัน เช่น
Code
<?php
mysql_connect("localhost", "phpuser", "alm65z");
mysql_select_db("phpdb");
function sess_open($sess_path, $sess_name) {
return true;
}
function sess_close() {
return true;
}
function sess_read($sess_id) {
$result = mysql_query("SELECT Data FROM sessions WHERE SessionID = '$sess_id';");
if (!mysql_num_rows($result)) {
$CurrentTime = time();
mysql_query("INSERT INTO sessions (SessionID, DateTouched) VALUES ('$sess_id', $CurrentTime);");
return '';
} else {
extract(mysql_fetch_array($result), EXTR_PREFIX_ALL, 'sess');
mysql_query("UPDATE sessions SET DateTouched = $CurrentTime WHERE SessionID = '$sess_id';");
return $sess_Data;
}
}
function sess_write($sess_id, $data) {
$CurrentTime = time();
mysql_query("UPDATE sessions SET Data = '$data', DateTouched = $CurrentTime WHERE SessionID = '$sess_id';");
return true;
}
function sess_destroy($sess_id) {
mysql_query("DELETE FROM sessions WHERE SessionID = '$sess_id';");
return true;
}
function sess_gc($sess_maxlifetime) {
$CurrentTime = time();
mysql_query("DELETE FROM sessions WHERE DateTouched + $sess_maxlifetime < $CurrentTime;");
return true;
}
session_set_save_handler("sess_open", "sess_close", "sess_read", "sess_write", "sess_destroy", "sess_gc");
session_start();
$_SESSION['foo'] = "bar";
$_SESSION['baz'] = "wombat";
?>
ทั้ง 2 เว็บให้ใช้ Code นี้เดียวกัน ตัว Session ก็จะเหมือนกันทุกประการ
http://www.tuxradar.com/practicalphp/10/3/7
|
 |
 |
 |
 |
Date :
2014-02-06 16:19:57 |
By :
mr.win |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
เป็นประโยช์มากๆ ข้อมูลล่าสุดด้วย แล้ว
1.การเก็บ session ใน memcached กับเก็บใน db แบบไหนดีกว่ากันหรอครับ
2.แชร์โฮสจะใช้ memcached หรือแบบ db ได้ไหมครับ ไม่มีทุนเช่า vps
3.ถ้าต่างโฮส จะใช้โคดด้านบนได้ไหม
ตอบหน่อยนะ คาใจมานาน ^___^ ขอบคุณล่วงหน้าเลยนะครับ
|
 |
 |
 |
 |
Date :
2014-03-16 16:41:29 |
By :
phun |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
โอ้โห....คุณ itpcc เดี๋ยวขอไปแปลก่อนนะคร้าบบบพี่น้อง ขอบคุณมากมาย
|
 |
 |
 |
 |
Date :
2014-03-16 22:51:45 |
By :
phun |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ใช้ผ่าน MySQL อาจจะช้าครับ แต่ไม่ถึงกับช้ามาก และไม่เหมาะสำหรับ User ที่มีจำนวนมากหลัก 1,000 ในคราว ๆ เดียวกัน 
|
 |
 |
 |
 |
Date :
2014-03-17 06:15:36 |
By :
mr.win |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|
|

|
Load balance : Server 03
|