19 ธันวาคม 2554

การใช้งาน Header php

รูปแบบ
header("ชื่อเฮดเดอร์:ค่าเฮดเดอร์") เช่น header("Location:http://shineji.blogspot.com");

การป้องกัน Page Caching
เพื่อป้องการไม่ให้หน้าเว็บเพจเรียก cache มาแสดง โดยกำหนดดังนี้
header("Cache-Control: max-age=0; no-cache; no-store; must-revalidate");

การย้ายเพจ Redirect
คือการย้ายหน้าเพจที่แสดงในขณะนี้ไปยังเพจอื่น
header("Location: URL");
เช่น
header("Location: webboard/php_board.php");
header("Location: http://shineji.blogspot.com");
ในทางปฏิบัติ header() ไม่ได้ทำการย้ายเพจทันที ต้องรอให้เฮดเดอร์ถูกส่งมาที่เบราเซอร์ก่อน
ดังนั้นควรวางคำสั่ง exit หรือ die ตามหลัง ฟังก์ชั่น header() เช่น
header("Location: signin.php");
exit;

แบบหน่วงเวลา
header("Refresh: เวลาเป็นวินาที; url=เพจปลายทาง");

แบบ refresh ไม่กำหนด URL
header("Refresh: 5");

เว็บไซต์รวบรวม header
http://www.jonasjohn.de/snippets/php/headers.htm

ไม่มีความคิดเห็น: