01 | วิธีแก้ปัญหา 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") ; ?>
- มีคำว่า Some text ถูกส่งออกไปแล้ว
- มีคำว่า <html> ถูกส่งออกไปแล้ว
- มี 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 } ?>บทความน่าสนใจ
- php redirect สั่งเด้งฟ้าผ่า
- ob_start() กับคำกล่าวที่ว่า ใช้ได้แต่ไม่แนะนำ
- .note .success .warning css class
- วิธีแก้ปัญหา Parse error: syntax error, unexpected T_VARIABLE
- วิธีแก้ปัญหา Parse error: syntax error, unexpected ‘}’
Tags: PHP
มี 6 ความเห็น ในบทความเรื่อง “วิธีแก้ปัญหา Warning: Cannot modify header information – headers already sent by…”
แสดงความเห็น
Categories
-
Recent Posts
- เครื่องฟ้อง error จอฟ้า STOP: c0000221 {Bad Image Checksum}
- The application failed to initialize properly(000000)
- อยากเปิดร้านคอม ทำยังไงดี?
- Wordpress Theme PhotoMadness ธีมสำหรับคนชอบถ่ายภาพ
- mail server : ติดตั้ง argosoft mail server
- mail server สร้างเมล์เซิฟเวอร์ทดสอบโค้ดส่งอีเมล์ ด้วย hmailserver
- php redirect สั่งเด้งฟ้าผ่า
- javascript ห้ามคลิกขวา
- CSS IE Bugs แมลงร้ายในไออี 6
- PHP round ปัดเศษ
- PHP ceil() ปัดเศษขึ้น
- Gravatar สร้างร่างใหม่สู่โลกออนไลน์
- PHP floor() ปัดเศษลง
- แจกโค้ด php คำนวณอายุ
- PHP date() ฟังก์ชั่นจัดการวัน-เวลา
Tags
Array Back to the basic calendar CAPTCHA chart CSS Dreamveawer error firefox firefox plugin FPDF General gmail google hotmail html icon icons javascript jQuery jquery plugin jquery slide jquery tooltip jQuery tutorial jQuery UI loop Mac MySQL Other PHP plugin program programming slide slide show SMF smf themes smf themes download smf themes free smf themes thai SQL sql function tooltip Wordpress wordpress plugin
ผมทำตามทั้ง 3 วิธีแล้วยังแก้ไม่ได้เลย มันขึ้นอย่างนี้อ่ะครับ
PNG IHDR<z4Uง PLTE๔๊€ศ–€ ฯ\
อันนี้มันไม่เกี่่ยวกับปัญหา redirect แล้วล่ะครับ
จากที่เขียนมาน่าจะเกิดจากการใช้ charset utf8 ค่ะ
ขอบคุณคับ
ขอบคุณมากครับ ผมใช้ Javascript แทน ครับ เรียบร้อย หายห่วง
ให้เอา Tag <html> ออก ครับ