รบกวนท่านผู้รู้ช่วยดูโค้ด setcookie ผมไม่เข้าใจการทำงาน

สำหรับผู้ที่ เริ่มต้น Programming - PHP มีอะไร แนะนำ หรือข้อสงสัยต้องบอร์ด นี้ คนที่มีความรู้ แบ่งปันคนอื่นบ้างนะ ปัญหาการเขียนโปรแกรม แบบ OOP Session Cookies php network

Moderator: mindphp, ผู้ดูแลกระดาน

Dwight
PHP Jr. Member
PHP Jr. Member
โพสต์: 13
ลงทะเบียนเมื่อ: 09/12/2011 9:55 am

รบกวนท่านผู้รู้ช่วยดูโค้ด setcookie ผมไม่เข้าใจการทำงาน

โพสต์ที่ยังไม่ได้อ่าน โดย Dwight »

addbasket.php

โค้ด: เลือกทั้งหมด

<?
    if(!isset($numcd))
    {
        $numcd=0;
    }
    setcookie("cart[$numcd]","$musiccd"); //Warning: Cannot modify header information - headers already sent by (output started at D:\AppServ\www\BtheBrovs\sc\addbasket.php:1) in D:\AppServ\www\BtheBrovs\sc\addbasket.php on line 6
    switch($musiccd)
    {
        case "Green Day" : $pr=199; break;
        case "Linkin Park" : $pr=175; break;
        case "Sum 41" : $pr=150; break;
    }
    setcookie("price[$numcd]","$pr"); //Warning: Cannot modify header information - headers already sent by (output started at D:\AppServ\www\BtheBrovs\sc\addbasket.php:1) in D:\AppServ\www\BtheBrovs\sc\addbasket.php on line 13
    $numcd++;
    setcookie("$numcd","$numcd"); //Warning: Cannot modify header information - headers already sent by (output started at D:\AppServ\www\BtheBrovs\sc\addbasket.php:1) in D:\AppServ\www\BtheBrovs\sc\addbasket.php on line 15
    print ("Add CD $musiccd to Basket<br>");
?>
<hr>
<a href="productform.html">View other Product</a>
<a href="showcart.php">Show Product</a>
<a href="delcart.php">Cancel</a>
delcart.php

โค้ด: เลือกทั้งหมด

<?
    for($a=0;$a<$numcd;$a++)
    {
        setcookie("cart[$a]","");
        setcookie("price[$a]","");
    }
    setcookie("numcd",""); //Warning: Cannot modify header information - headers already sent by (output started at D:\AppServ\www\BtheBrovs\sc\delcart.php:1) in D:\AppServ\www\BtheBrovs\sc\delcart.php on line 7
    print ("You cancel all Product");
?>
<br>
<a href="productform.html">View other Product</a>
ภาพประจำตัวสมาชิก
secivon
PHP Full Member
PHP Full Member
โพสต์: 35
ลงทะเบียนเมื่อ: 24/03/2009 5:15 pm

Re: รบกวนท่านผู้รู้ช่วยดูโค้ด setcookie ผมไม่เข้าใจการทำงาน

โพสต์ที่ยังไม่ได้อ่าน โดย secivon »

HTTP header command ทั้งหลาย เช่น setcookie() , session_start() , header() ...
ต้องเรียกใช้ "ก่อนจะส่ง content อื่นๆ ของ page" ครับ

นั่นก็คือ ต้องเริ่มไฟล์ php ด้วย PHP tag เลย (<?php) (ในรูปมี space หรือ new line chracter) system ก็เลยแจ้ง error ครับ
ลบ space ต้นไฟล์ออก น่าจะใช้ได้แล้ว
"อย่าเอาแต่โทษเด็ก ถ้าเด็กมันจะห่วย ดูตัวเองบ้างว่าทำบ้านเมืองไว้เป็นยังไง"
ตอบกลับโพส

ผู้ใช้งานขณะนี้

สมาชิกกำลังดูบอร์ดนี้: Google Adsense [Bot] และบุคลทั่วไป 68