HardwareHeaven.com

HardwareHeaven.com

Looking for the skin chooser?
 
 
  • Home

  • Hardware reviews

  • Articles

  • News

  • Tools

  • Gaming at HardwareHeaven

  • Forums

 

Go Back   HardwareHeaven.com > Forums > Software / Tools > Programming, Coding, (Web)Design


Programming, Coding, (Web)Design Discuss all your programming or design needs with likeminded people.

Reply
 
Thread Tools
Old Apr 13, 2003, 07:14 AM   #1
Unbiased.
 
Join Date: Jun 2002
Posts: 4,812
Rep Power: 0
ToshiroOC is on a distinguished road

PHP Question

I have the following code, and I keep getting errors on it I tried fixing it myself, but I haven't been able to get rid of these particular ones. I'm a complete beginner to PHP, so this is somewhat confusing. Can anyone help me? Thanks in advance to anyone who takes the time to look this over and help For some reason I get the feeling that there's something important that I'm trying to do here that I can't the way I'm trying to...

EDIT: I saw some errors, fixing now...

EDIT: New code here, new errors (new line #s, really)

Code:
<html>
 <head>
  <title>Inventory with Cookies!</title>
 </head>
 <body>
 <?php

 if (isset($_COOKIE['c_bars']))
  {
   echo "There are $_COOKIE['c_bars'] chocolate bars in inventory\n\n";
  }
  else
  {
   if (isset($_GET['c_bars']))
    {
     $c_bars = $_GET['c_bars'];
    }
   else
    {
     $c_bars = "0";
    }

   echo "There are $c_bars chocolate bars in inventory\n\n";

   setcookie ("c_bars", $c_bars);

  }

   $c_bars_up = $_COOKIE['c_bars'] + 1;

   $c_bars_down = $_COOKIE['c_bars'] - 1;


 if (isset($_COOKIE['g_bars']))
  {
   echo "There are $_COOKIE['g_bars'] granola bars in inventory\n\n";
  }
  else
  {
   if (isset($_GET['g_bars']))
    {
     $g_bars = $_GET['g_bars'];
    }
   else
    {
     $g_bars = "0";
    }

   echo "There are $g_bars granola bars in inventory\n\n";

   setcookie ("g_bars", $g_bars);

  }

   $g_bars_up = $_COOKIE['g_bars'] + 1;

   $g_bars_down = $_COOKIE['=g_bars'] - 1;

 echo "<a href=\"/cookietest.php?c_bars=$c_bars_up\">Add one chocolate bar</a><br />";

 echo "<a href=\"/cookietest.php?c_bars=$c_bars_down\">Remove one chocolate bar</a><br />";

 echo "<a href=\"/cookietest.php?g_bars=$g_bars_up\">Add one granola bar</a><br />";

 echo "<a href=\"/cookietest.php?g_bars=$g_bars_down\">Remove one granola bar</a><br />";
?> 


 </body>
</html>
ERRORS:

There are 0 chocolate bars in inventory
Warning: Cannot modify header information - headers already sent by (output started at C:\Program Files\Apache Group\Apache2\htdocs\cookietest.php:6) in C:\Program Files\Apache Group\Apache2\htdocs\cookietest.php on line 25

Notice: Undefined index: c_bars in C:\Program Files\Apache Group\Apache2\htdocs\cookietest.php on line 29

Notice: Undefined index: c_bars in C:\Program Files\Apache Group\Apache2\htdocs\cookietest.php on line 31
There are 0 granola bars in inventory
Warning: Cannot modify header information - headers already sent by (output started at C:\Program Files\Apache Group\Apache2\htdocs\cookietest.php:6) in C:\Program Files\Apache Group\Apache2\htdocs\cookietest.php on line 51

Notice: Undefined index: g_bars in C:\Program Files\Apache Group\Apache2\htdocs\cookietest.php on line 55

Notice: Undefined index: =g_bars in C:\Program Files\Apache Group\Apache2\htdocs\cookietest.php on line 57

When used with switches, like "cookietest.php?c_bars=10&g_bars=10", they display the proper number of bars in inventory but still give these exact same errors.
__________________
[img][/img]
[color=White]Peace be with you, Joe.[/color]
Driverheaven Staff Member (Supermoderator)
ToshiroOC is offline   Reply With Quote


Old Apr 13, 2003, 07:44 AM   #2
confutatis maledictis
 
Vampyromaniac's Avatar
 
Join Date: May 2002
Location: somewhere dark
Posts: 5,974
Rep Power: 77
Vampyromaniac is just really niceVampyromaniac is just really niceVampyromaniac is just really niceVampyromaniac is just really niceVampyromaniac is just really nice
System Specs

psst. . . use the [php] tag
__________________
Digitalis 3.3
Athlon 64 3000 // ASUS K8V SE Deluxe // 1024MB PC3200 (2-2-2-10 1T)
ATI All-In-Wonder 9700 Pro // 20" Dell 2005FPW (DVI)
M-Audio Revo 7.1 + Philips Acoustic Edge // Klipsch ProMedia 2.1
320/16 Western Digital WD3200KS + 120/8 Seagate 7200.7
NEC ND-3550A 16x DVD±RW + Lite-On 52x24x CD-RW
Antec Sonata case // 480W Antec TruePower

personal bests || Aq'3: 46796 | 3D'01: 20461 | 3D'03: 6336 | 3D'05: 2677 | PC'04: 4605 | PC'02: 7691,9092,1250

Vampyromaniac is offline   Reply With Quote
Old Apr 13, 2003, 07:46 AM Threadstarter Thread Starter   #3
Unbiased.
 
Join Date: Jun 2002
Posts: 4,812
Rep Power: 0
ToshiroOC is on a distinguished road

Ugh... the color formatting is killing my eyes. I think I'll just use the [code] tags
__________________
[img][/img]
[color=White]Peace be with you, Joe.[/color]
Driverheaven Staff Member (Supermoderator)
ToshiroOC is offline   Reply With Quote
Old Apr 13, 2003, 07:47 AM   #4
confutatis maledictis
 
Vampyromaniac's Avatar
 
Join Date: May 2002
Location: somewhere dark
Posts: 5,974
Rep Power: 77
Vampyromaniac is just really niceVampyromaniac is just really niceVampyromaniac is just really niceVampyromaniac is just really niceVampyromaniac is just really nice
System Specs

ugh indeed

nm, that was unsightly
__________________
Digitalis 3.3
Athlon 64 3000 // ASUS K8V SE Deluxe // 1024MB PC3200 (2-2-2-10 1T)
ATI All-In-Wonder 9700 Pro // 20" Dell 2005FPW (DVI)
M-Audio Revo 7.1 + Philips Acoustic Edge // Klipsch ProMedia 2.1
320/16 Western Digital WD3200KS + 120/8 Seagate 7200.7
NEC ND-3550A 16x DVD±RW + Lite-On 52x24x CD-RW
Antec Sonata case // 480W Antec TruePower

personal bests || Aq'3: 46796 | 3D'01: 20461 | 3D'03: 6336 | 3D'05: 2677 | PC'04: 4605 | PC'02: 7691,9092,1250

Vampyromaniac is offline   Reply With Quote
Old Apr 19, 2003, 10:02 AM   #5
DriverHeaven Junior Member
 
Adagio's Avatar
 
Join Date: Jul 2002
Location: Berlin, Germany
Posts: 70
Rep Power: 0
Adagio is on a distinguished road

The problem is that HTML-stuff before you try to use cookies. When you want to work with Sessions or Cookies, no output is allowed to be sent to the browser.
__________________
My software never has bugs. It just develops random features.

° Adagios hard- and softwareconfiguration

° 3DFusion - All news around ATi, Matrox, nVidia and more!
Adagio is offline   Reply With Quote
Old Apr 19, 2003, 03:02 PM   #6
Custom User Title
 
Join Date: May 2002
Location: Washington
Posts: 1,125
Rep Power: 0
Andrew275 will become famous soon enoughAndrew275 will become famous soon enough
System Specs

Inside strings, don't use quotes for array keys. For example, you need to change:

echo "There are $_COOKIE['c_bars'] chocolate bars in inventory\n\n";

to:

echo "There are $_COOKIE[c_bars] chocolate bars in inventory\n\n";
Andrew275 is offline   Reply With Quote
Old Apr 20, 2003, 02:51 AM Threadstarter Thread Starter   #7
Unbiased.
 
Join Date: Jun 2002
Posts: 4,812
Rep Power: 0
ToshiroOC is on a distinguished road

Well, doing those things, it still didn't completely fix it I think I'll try and find more detailed info on the implementation of these commands before I ask for more help, though
__________________
[img][/img]
[color=White]Peace be with you, Joe.[/color]
Driverheaven Staff Member (Supermoderator)
ToshiroOC is offline   Reply With Quote
Old Apr 20, 2003, 03:05 AM   #8
Custom User Title
 
Join Date: May 2002
Location: Washington
Posts: 1,125
Rep Power: 0
Andrew275 will become famous soon enoughAndrew275 will become famous soon enough
System Specs

Oh yeah, this:

$g_bars_down = $_COOKIE['=g_bars'] - 1;

should be:

$g_bars_down = $_COOKIE['g_bars'] - 1;
Andrew275 is offline   Reply With Quote
Old Apr 20, 2003, 06:20 AM Threadstarter Thread Starter   #9
Unbiased.
 
Join Date: Jun 2002
Posts: 4,812
Rep Power: 0
ToshiroOC is on a distinguished road

Quote:
Originally posted by Andrew275
Oh yeah, this:

$g_bars_down = $_COOKIE['=g_bars'] - 1;

should be:

$g_bars_down = $_COOKIE['g_bars'] - 1;
That was just a typo
__________________
[img][/img]
[color=White]Peace be with you, Joe.[/color]
Driverheaven Staff Member (Supermoderator)
ToshiroOC is offline   Reply With Quote
Old Apr 20, 2003, 06:56 AM Threadstarter Thread Starter   #10
Unbiased.
 
Join Date: Jun 2002
Posts: 4,812
Rep Power: 0
ToshiroOC is on a distinguished road

Fixed it... I'm sure there are more elegant ways to do this, but this works well enough for a script to try out cookies

Code:
<?php ob_start(); ?>
<html>
 <head>
  <title>Inventory with Cookies!</title>
 </head>
 <body>
 <?php

 if (isset($_COOKIE['c_bars']))
  {
   $c_bars = $_COOKIE['c_bars'];
  }

   if (isset($_GET['c_bars']))
    {
     $c_bars = $_GET['c_bars'];
    }
   else
    {
     $c_bars = "0";
    }


   echo "There are $c_bars chocolate bars in inventory\n\n";

   setcookie ("c_bars", $c_bars, time() + (365*24*3600));

   $c_bars_up = $c_bars + 1;

   $c_bars_down = $c_bars - 1;


 if (isset($_COOKIE['g_bars']))
  {
   $g_bars = $_COOKIE['g_bars'];
  }


   if (isset($_GET['g_bars']))
    {
     $g_bars = $_GET['g_bars'];
    }
   else
    {
     $g_bars = "0";
    }


   echo "There are $g_bars granola bars in inventory\n\n";

   setcookie ("g_bars", $g_bars, time() + (365*24*3600));

   $g_bars_up = $g_bars + 1;

   $g_bars_down = $g_bars - 1;


 echo "<a href=\"/cookietest.php?c_bars=$c_bars_up\">Add one chocolate bar</a><br />";

 echo "<a href=\"/cookietest.php?c_bars=$c_bars_down\">Remove one chocolate bar</a><br />";

 echo "<a href=\"/cookietest.php?g_bars=$g_bars_up\">Add one granola bar</a><br />";

 echo "<a href=\"/cookietest.php?g_bars=$g_bars_down\">Remove one granola bar</a><br />";
?> 


 </body>
</html>
__________________
[img][/img]
[color=White]Peace be with you, Joe.[/color]
Driverheaven Staff Member (Supermoderator)
ToshiroOC is offline   Reply With Quote
Old Apr 27, 2003, 03:50 PM   #11
A Legend in Underwear
 
UberLord's Avatar
 
Join Date: May 2002
Location: Unknown
Posts: 5,255
Rep Power: 0
UberLord will become famous soon enough

Big Grin

Quote:
Originally posted by ToshiroOC
That was just a typo
Most bugs are typos
__________________
Gentoo Linux - Developer (baselayout)
Read my blog

"I contend that we are both atheists. I just believe in one fewer god than you do. When you understand why you dismiss all the other possible gods, you will understand why I dismiss yours."
Stephen Roberts
UberLord is offline   Reply With Quote
Reply

Thread Tools