|
 |
|
มีฟังก์ชัน PHP ที่แสดงค่า SESSION แบบ real time หรือเปล่าครับ |
|
 |
|
|
 |
 |
|
ใช้ Ajax กับ jQuery ช่วยครับ โดยตั้งเวลาไปดึงค่า Session มาจาก Server ครับ
<html>
<head>
<title>ThaiCreate.Com</title>
<script type="text/javascript" src="jquery-latest.js"></script>
</head>
<script type="text/javascript">
$(document).ready(function()
{
function getSession()
{
$.post("getSession.php", { },
function(result){
$("div1").text(result);
}
);
}
setInterval(getSession, 200); // 1000 = 1 second
getSession();
}
);
</script>
<body>
<div id="div1"></div>
</body>
</html>
เขียนเพิ่มที่ไหล์ getSession.php
|
 |
 |
 |
 |
Date :
2013-09-18 13:11:43 |
By :
mr.win |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ไฟล์ getSession.php คือไฟล์ที่เขียน session ขึ้นมาใช่มั้ยครับ
แล้วโค๊ดที่พี่เขียนให้นี่เอาไปใส่ใน a.php ใช่มั้ยครับ
แล้วหน้า a.php ผมสามารถใช้
Code (PHP)
<?PHP echo session['num'] ?>
เพื่อให้ echo session ปัจจุบันได้เลย ใช่มั้ยครับ
|
 |
 |
 |
 |
Date :
2013-09-18 13:19:42 |
By :
yukioo |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ประมาณนั้นครับ
|
 |
 |
 |
 |
Date :
2013-09-18 13:26:28 |
By :
mr.win |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ยังไม่ได้น่ะครับ ไม่มีการ update session แล้วนำมาแสดงเลยครับ
อันนี้ไฟล์ a.php ครับ
Code (PHP)
<html>
<head>
<title>ThaiCreate.Com</title>
<script type="text/javascript" src="jquery-latest.js"></script>
</head>
<script type="text/javascript">
$(document).ready(function()
{
function getSession()
{
$.post("getSession.php", { },
function(result){
$("div1").text(result);
}
);
}
setInterval(getSession, 200); // 1000 = 1 second
getSession();
}
);
</script>
<body>
<div id="div1"></div>
<?
echo "".$_SESSION['num']."<br>";
?>
</body>
</html>
อันนี้ไฟล์ getSession.php ครับ ผมทดสอบโดยการเปลี่ยนค่า session ด้วยตัวเองก่อนน่ะครับ
Code (PHP)
<?php
session_start();
$_SESSION['num'] = 7;
?>
|
 |
 |
 |
 |
Date :
2013-09-18 13:49:49 |
By :
yukioo |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
Code (index.php)
<html>
<head>
<title>ThaiCreate.Com</title>
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.js"></script>
</head>
<script type="text/javascript">
$(document).ready(function()
{
function getSession()
{
$.post("getSession.php", { },
function(result){
$("#div1").text(result);
}
);
}
setInterval(getSession, 1000); // 1000 = 1 second
getSession();
}
);
</script>
<body>
<div id="div1"></div>
</body>
</html>
Code (getSession.php)
<?php
session_start();
$_SESSION["time"] = date("Y-m-d H:i:s");
echo $_SESSION["time"];
?>
|
 |
 |
 |
 |
Date :
2013-09-18 14:38:01 |
By :
mr.win |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|
|

|
Load balance : Server 05
|