Get Login Session from phpBB
นี่เป็นวิธีในการ get สถานะ login จาก phpBB คือจริงๆแล้วเรายังคงใช้การ login/logout ด้วย phpBB เหมือนเดิม เพียงแต่ถ้าหน้าจออื่นๆต้องการใช้ประโยชน์จากการ login ด้วย เช่นหน้านี้ต้อง login ก่อนเท่านั้นถึงเข้าดูได้ ซึ่งเป็นส่วนที่นอกเหนือจาก phpBB ให้เราใส่ code ต่อไปนี้ไว้ในหน้าที่ต้องการ เราก็จะรู้ว่า user คนนั้น login อยู่รึไม่
สำหรับ phpBB 3 ขึ้นไปเท่านั้น
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | <?php $phpEx = 'php'; // Added to define the variable as the appropriate extension, no extension.inc file in phpBB3 define('IN_PHPBB', true); $phpbb_root_path = './webboard/'; // set this as the path to your phpBB installation include($phpbb_root_path . 'common.'.$phpEx); include($phpbb_root_path . 'includes/functions_user.'.$phpEx); // // Start session management // $user->session_begin(); $nickname = $user->data['username']; ?> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html> <head> <title>Login Authentication</title> <meta http-equiv="content-type" content="text/html; charset=utf-8"> <style type="text/css"> html,body,iframe { border: 0; height: 100%; margin: 0; overflow: hidden; padding: 0; } iframe { height: 100%; width: 100%; } </style> </head> <body> <?php echo $nickname."<p/>"?> </body> </html> |
ตัวที่ถูก echo ออกมาคือ $nickname ถ้าไม่ได้ login ค่าก็จะกลายเป็น Anonymous ดังนั้นเราควรเช็คจากจุดนี้ว่า $nickname เท่ากับ Anonymous หรือไม่ จึงจะรู้ว่าตอนนี้สถานะการ login เป็นอย่างไร
หวังว่าจะมีประโยชน์กับทุกๆคนนะครับ