May. 08
1

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

Warning: Cannot modify header information – headers already sent by

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

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

1.

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

2.

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

3.

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

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

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

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

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

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

$complete = "true" ;

แล้วทีนี้ตรงระหว่างแท็ก <head></head> ใส่คำสั่งนี้เข้าไป

<?php if ($complete=="true"){ ?>
<script type="text/javascript">
	window.location="gbook.php";
</script>
<?php } ?>

บทความน่าสนใจ

  1. ob_start() กับคำกล่าวที่ว่า ใช้ได้แต่ไม่แนะนำ
  2. วิธีแก้ปัญหา Parse error: syntax error, unexpected ‘}’
  3. .note .success .warning css class
  4. php redirect สั่งเด้งฟ้าผ่า
  5. ดึงชื่อโฮสต์ออกจาก URL ด้วย javaScript
This entry was posted in PHP and tagged . Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

21 Comments

  1. napasool MSIE 7.0 Windows XP
    Posted 21/09/2009 at 23:13 | Permalink

    ผมทำตามทั้ง 3 วิธีแล้วยังแก้ไม่ได้เลย มันขึ้นอย่างนี้อ่ะครับ
    ‰PNG  IHDR<z4Uง PLTE๔๊€ศ–€ ฯ\

  2. administrator Firefox 3.5.3.NETCLR3.5.30729 Windows XP
    Posted 22/09/2009 at 08:15 | Permalink

    อันนี้มันไม่เกี่่ยวกับปัญหา redirect แล้วล่ะครับ

  3. giffany Firefox 3.5.3 Windows XP
    Posted 08/10/2009 at 10:39 | Permalink

    จากที่เขียนมาน่าจะเกิดจากการใช้ charset utf8 ค่ะ

  4. offering Chrome 3.0.195.27 Windows 7
    Posted 23/10/2009 at 05:51 | Permalink

    ขอบคุณคับ

  5. Posted 13/11/2009 at 11:34 | Permalink

    ขอบคุณมากครับ ผมใช้ Javascript แทน ครับ เรียบร้อย หายห่วง

  6. PN MSIE 8.0 Windows XP
    Posted 05/01/2010 at 09:38 | Permalink

    ให้เอา Tag <html> ออก ครับ

  7. Posted 08/06/2010 at 13:48 | Permalink

    ขอบคุณครับ ผมใช้ Javascript เช่นกัน ใช้ได้ครับ :)

  8. boy MSIE 6.0 Windows XP
    Posted 10/06/2010 at 17:29 | Permalink

    อีกกรณีของ utf-8 คือ เราต้องสั่ง save code แบบ utf-8 without BOM (BOM:Byte-Order Mark ) ไม่งั้นไล่โค้ดจนตายก็หาไม่เจอ

  9. Posted 03/08/2010 at 17:19 | Permalink

    ขอบคุณค่ะ กำลังหาวิธีแก้พอดี

  10. Ars_BeaL MSIE 8.0 Windows XP
    Posted 25/08/2010 at 10:43 | Permalink

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

    เพราะอะไรถึงไม่แนะนำครับ มันมีข้อเสียอย่างไรครับ รบกวนอธิบายหน่อยได้ไหมครับ

  11. Posted 19/12/2010 at 04:13 | Permalink

    ขอบคุณมากครับกำลังหาเลยครับ

  12. tong Firefox 3.6.13 Windows 7
    Posted 21/12/2010 at 19:05 | Permalink

    แหล่มๆ แท้งกิ้ว

  13. ao MSIE 8.0 Windows 7
    Posted 01/04/2011 at 00:11 | Permalink

    ขอบคุณมาก ๆ ค่ะ

  14. silentstart Firefox 4.0.1 Windows XP
    Posted 15/06/2011 at 10:52 | Permalink

    ขอบคุณครับ มีประโยชน์มาก

  15. Panuphan Chrome 15.0.861.0 Windows 7
    Posted 07/09/2011 at 16:37 | Permalink

    ขอบคุณมากครับ ช่วยได้เยอะเลย ^^ ผมใช้วิธีแทรก script เอาก็ได้เลย

  16. ohno!! MSIE 8.0 Windows XP
    Posted 08/09/2011 at 04:01 | Permalink

    ผมไม่ค่อยเข้าใจที่คุณอธิบายการแก้ปัญหาเลยครับ

  17. อุทัย MSIE 8.0 Windows XP
    Posted 13/10/2011 at 17:48 | Permalink

    สวัสดีครับ ท่านผู้รู้ทุกท่านครับ ผมเป็นช่างมัครเล่น ได้ประกอบวงจรภาคขยายเสียง เพาเวอร์แอมปลิไฟร์ (MODIFY)-ME-300 MEขึ้นมาใช้งานตัวหนึ่ง ใช้ได้ดีมากครับ ใช้งานมาแล้ว2-3ปีแล้ว แต่เวลานี้เกิดปัญหาขึ้นกับเครื่องแก้ไม่ตก มันคล้ายๆไฟรั่ว
    ออกที่ลำโพง ผมก็คิดว่าcคอนเดนเซอร์ รั่วเลยทำการเปลี่ยนทั้งหมด รวมถึง ไดโอด และ ซีเนอร์ไดโอด10 V ด้วยแต่ก็ไม่หาย
    จึงเรียนท่านที่มีประสบการจะมีวิธีแก้บ้างครับ ขอขอบพระคุณอย่างสูง

  18. อุทัย MSIE 8.0 Windows XP
    Posted 13/10/2011 at 17:52 | Permalink

    มันเป็นเสียงที่ผิดปกตินะครับออกที่ลำโพง ทั้งๆที่ลดเสียงเพลงลงแล้ว ยังมีเสียง คล้ายๆฝนตกอยู่ ครับ

  19. อุทัย MSIE 8.0 Windows XP
    Posted 13/10/2011 at 17:56 | Permalink

    ถ้ามีวิธีแก้ไข ช่วยกรุณาเขียนข้อความไว้ให้ด้วยนะครับ แล้วคืนนี้ผมจะกลับมาอ่าน พอดีมีทุระข้างนอก ขอบคุณครับ อุทัย

  20. Posted 17/10/2011 at 16:02 | Permalink

    ขอบคุณมาก ๆครับ กำลังเจออยู่พอดีเลย
    ….

Post a Comment

You must be logged in to post a comment.