ฟังก์ชั่น session_start()
ความหมายของฟังก์ชั่นและการใช้งาน
การโหลดข้อมูล session มาเก็บไว้ที่หน่วยความจำ สำหรับ Web Page ที่ต้องการใช้ session จะต้องมีคำสั่งนี้เสมอ ด้วยฟังก์ชั่น session_start()
รูปแบบการเขียน (Syntax)
- การใช้คำสั่งนี้จะต้องอยู่คำสั่งแรกสุดของการประมวลผล
bool session_start ([ array $options = [] ] )
ตัวอย่างที่ 1
<?php
// page1.php
session_start();
echo 'Welcome to page #1';
$_SESSION['favcolor'] = 'green';
$_SESSION['animal'] = 'cat';
$_SESSION['time'] = time();
// Works if session cookie was accepted
echo '<br /><a href="/page2.php">page 2</a>';
// Or maybe pass along the session id, if needed
echo '<br /><a href="/page2.php?' . SID . '">page 2</a>';
?>
ตัวอย่างที่ 2
<?php
// page2.php
session_start();
echo 'Welcome to page #2<br />';
echo $_SESSION['favcolor']; // green
echo $_SESSION['animal']; // cat
echo date('Y m d H:i:s', $_SESSION['time']);
// You may want to use SID here, like we did in page1.php
echo '<br /><a href="/page1.php">page 1</a>';
?>