|
|||||||
| Programming, Coding, (Web)Design Discuss all your programming or design needs with likeminded people. |
![]() |
|
|
Thread Tools |
|
|
#1 |
|
Unbiased.
Join Date: Jun 2002
Posts: 4,812
Rep Power: 0 ![]() |
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>
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) |
|
|
|
|
|
#2 |
|
confutatis maledictis
|
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 |
|
|
|
|
|
|
|
Unbiased.
Join Date: Jun 2002
Posts: 4,812
Rep Power: 0 ![]() |
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) |
|
|
|
|
|
#4 |
|
confutatis maledictis
|
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 |
|
|
|
|
|
#5 |
|
DriverHeaven Junior Member
Join Date: Jul 2002
Location: Berlin, Germany
Posts: 70
Rep Power: 0 ![]() |
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! |
|
|
|
|
|
#6 |
|
Custom User Title
|
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"; |
|
|
|
|
|
|
|
Unbiased.
Join Date: Jun 2002
Posts: 4,812
Rep Power: 0 ![]() |
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) |
|
|
|
|
|
#8 |
|
Custom User Title
|
Oh yeah, this:
$g_bars_down = $_COOKIE['=g_bars'] - 1; should be: $g_bars_down = $_COOKIE['g_bars'] - 1; |
|
|
|
|
|
|
|
|
Unbiased.
Join Date: Jun 2002
Posts: 4,812
Rep Power: 0 ![]() |
Quote:
__________________
[img][/img] [color=White]Peace be with you, Joe.[/color] Driverheaven Staff Member (Supermoderator) |
|
|
|
|
|
|
|
|
Unbiased.
Join Date: Jun 2002
Posts: 4,812
Rep Power: 0 ![]() |
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) |
|
|
|
|
|
#11 | |
|
A Legend in Underwear
Join Date: May 2002
Location: Unknown
Posts: 5,255
Rep Power: 0 ![]() |
Quote:
__________________
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 |
|
|
|
|
![]() |
| Thread Tools | |
|
|