ยินดีต้อนรับ กรุณา สมัครสมาชิก หรือเข้าสู่ระบบ

Home » webboard » PHP
เข้าชม : 2901

วิธีแก้ปัญหา Warning: Cannot modify header information – headers already sent by

โพสต์เมื่อ: วันพุธ 5 มกราคม 2554  00:39 น.

Warning: Cannot modify header information – headers already sent by

ปัญหานี้เกิดจาก เรียกใช้คำสั่ง header() หลังจากมี output ส่งไปยัง client แล้ว

ดูตัวอย่างที่มีปัญหานะครับ

1.


echo "Some text" ;
header("Location:index.php") ;
?>

2.


header("Location:index.php") ; ?>

3.


$variable = 10 ;
?>
 

header("Location:index.php") ;
?>
ถูกส่งออกไปแล้ว
  1. มีคำว่า Some text ถูกส่งออกไปแล้ว
  2. มีคำว่า
  3. มี white space ถูกส่งออกไปแล้ว ระหว่างบล็อก php ทั้งสอง ( white space ได้แก่ ขึ้นบรรทัดใหม่,เคาะสเปชบาร์,แท็บ โดยรวมคือช่องว่างที่มองไม่เห็น)

มีวิธีแก้ 3 วิธี

1.หาให้เจอว่ามีเอ้าพุตอะไรออกไปในบรรทัดที่ php ฟ้อง

2.ใส่คำสั่ง ob_start() ใว้บนบรรทัดแรกสุดๆ ของไฟล์ (ไม่แนะนำ)

3.เอาคำสั่งจาวาสคริปเข้าช่วยในการ redirect

หลังจากที่คุณกระทำการอะไรบางอย่างเสร็จและต้องการใช้คำสั่ง header() redirect ไปเพจอื่น ให้เปลี่ยนจากคำสั่ง header() เป็นเซตค่าตัวแปรสักตัว

$complete = "true" ;

แล้วทีนี้ตรงระหว่างแท็ก

ใส่คำสั่งนี้เข้าไป


<script type="text/javascript">
	window.location="gbook.php";
script>

แทน
[ ความเห็นที่ 1]
RE : วิธีแก้ปัญหา Warning: Cannot modify header information – headers already sent by
โพสต์เมื่อ: วันพุธ 5 มกราคม 2554  01:53 น.

คำสั่ง redirect ใช้อันนี้ ก็ ได้

 print("<script LANGUAGE=\"javascript\">window.location.href=index.php</script>"); 


แทน
[ ความเห็นที่ 2]
RE : วิธีแก้ปัญหา Warning: Cannot modify header information – headers already sent by
โพสต์เมื่อ: วันจันทร์ 9 เมษายน 2555  00:20 น.

ใช้ wordpress-3.3.1-th + photoria Theme+ http://www.dekpress.com/%E0%B8%A7%E0%B8 ... B9%86.html

ผลลัพธ์แสดงข้อผิดพลาดตอน login เข้าหน้า wp-admin ครับ:
Warning: Cannot modify header information - headers already sent by (output started at /home/domains/***.com/public_html/wp-includes/formatting.php:1) in /home/domains/***.com/public_html/wp-includes/pluggable.php on line 866

ต้องแก้อย่างไรครับ ลองหลายวิธีแล้วไม่ได้ ท่านเทพช่วยแนะมาด้วยครับ (ถ้ามีรูปภาพ จะขอบคุณอย่างมากครับ)


wp error
[ ความเห็นที่ 3]
RE : วิธีแก้ปัญหา Warning: Cannot modify header information – headers already sent by
โพสต์เมื่อ: วันพุธ 11 เมษายน 2555  23:25 น.

1. ลองใช้  Notepad++ แก้ไขข้อความด้านบน และด้านล่าง อย่าให้มีบรรทัดว่างของไฟล์ทั้ง 2 ไฟล์ (formatting.php+ pluggable.php)
2. แล้ว endcoding UTF-8 With out Bom
3. แล้ว Save As... ทับชื่อไฟล์เดิมไปใหม่อีกครั้ง ครับ

บางที มันอาจจะเป็นคอมเม้น ที่พอช่วยท่านได้ครับ

รับซ่อมคอม ซ่อมคอม
081-374-2808
ซ่อมคอมพิวเตอร์


คุณต้องสมัครสมาชิก ถึงจะโพสกระทู้ได้

ปิดรับสมัครสมาชิกแล้ว


คำยอดฮิต :- Cannot modify header