Page 1 of 1
Setting cookie with a class
Posted: Thu Nov 20, 2003 2:48 pm
by Etherguy
I am trying to set a cookie from within a class function, but keep getting the dreadful:
Warning: Cannot modify header information - headers already sent by (output started at /srv/www/htdocs/autotech-dev/billing.php:2) in /srv/www/htdocs/autotech-dev/function.class on line 27
I am calling the class file with a require statement. Is there something I am missing?
Any help appreciated.
Posted: Thu Nov 20, 2003 3:01 pm
by qads
you prolly are outputing something, may even be a space?
try only useing the cookie set function, ie
Code: Select all
<?php
require('classfile.php');
$class = &NEW class();
$class->cookie($vars);
?>
see if you still get any errors.
i uselly return data from classes, not output it, this helps when you need to set cookies or deal with sessions.
Posted: Thu Nov 20, 2003 7:50 pm
by Etherguy
Let me give some code example ( sorry I know better then to not post it in my original posting ).
Here is my PHP file (billing.php) which makes the call.
Code: Select all
<?php
<link href="dance.css" rel="stylesheet" type="text/css">
<?php
include('config.php');
require('function.class');
?>
<div id="topnav">
<ul>
<li><a href="index.php">Home</a></li>
<li><a href="#">Work Order</a></li>
<li><a href="#">Customers</a></li>
<li><a href="#">Cars</a></li>
<li><a href="#">Process</a></li>
<li><a href="#">Expertise</a></li>
<li><a href="#" class="here">Billing</a></li>
</ul><br />
</div>
<div id="subnav">
<ul>
<li><a href="#">Generate Invoice</a></li>
<li><a href="#">Generate Quote</a></li>
<li><a href="#">Past Due Report</a></li>
<li><a href="#">Profit Manager</a></li>
</ul><br />
</div>
<?
if (!isset($_GET['cid'])){
$cus = new customerClass;
$cus -> getcustomer();
print $cid;
}
if (isset($_POST['cid'])or ($_GET['cid'])){
$cid = $_POST['cid'];
$sql="select fname,address1,city,state,home_phone from customer where cid='$cid'";
$ms=mysql_query($sql) or die ("select failed");
while($m=mysql_fetch_array($ms)){
$fname=$m["fname"];
$address1=$m["address1"];
$city=$m["city"];
$state=$m["state"];
$home_phone=$m["home_phone"];
$plate=$m["plate"];
}
?>
<? if ($_GET["vis"] == "2") {
print "<div><span class=stathide><a href=?vis=1&cid=$cid>Show Customer Info</a></span></div>";
}
elseif ($_GET["vis"] == "1") {
print "
<div id=CUSINFO align="left"><span class=stattext>$fname</span> <span class=stattitle>CUSTOMER</span><br>
<span class=stattext>$plate</span><span class=stattitle>TAG ID</span><br>
<span class=stattext>Some Name</span><span class=stattitle>VECHILE</span><br>
<span class=stathide><a href=?vis=2&cid=$cid>Hide Customer Info</a></span></div>";}
}
?>
and here is the class file (function.class) :
Code: Select all
<?php
<?php
include('config.php');
class customerClass
{
function getcustomer()
{
if (!isset($_POST['cid'])){
$sql = "select DISTINCT cid, fname from customer";
$result = mysql_query($sql);
print "
<div class=getcust><center>Select Customer from List<br>
<form method="post" action="$PHP_SELF">
<select name="cid">";
while ($row = mysql_fetch_row($result)){
print ("<option value=$row[0]>$row[1]</option>");
}
?>
</select><input type=submit value=Lookup></form><br>
Click to add new customer</center>
</div>
<?
}
if (isset($_POST['cid'])){
$cid = $_POST['cid'];
setcookie ("cidcookie", $cid);
}
}}
?>
?>
I am basically trying to have a cid selected from a database, then set to a cookie, so that it will always be available to the scripts from reference.
Any ideas??
Thanks.
Posted: Thu Nov 20, 2003 8:05 pm
by DuFF
Have you read this?
viewtopic.php?t=1157
It will explain why and how to get around the Headers Already Sent problem.
Posted: Fri Nov 21, 2003 8:37 am
by Etherguy
Okay..Read the mini-howto.. understood the mini-howto. Still lost on this though. I have tried both the ob_start() function as well as @ini_set("output_buffering","On"); and still get the errors.
Perhaps I am doing something wrong with the form inside the class??
Any other ideas on this? Surely I can't be the only one using classes to assign values from a form.
Anyone ??