Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Warning: Cannot send session cookie

0 views
Skip to first unread message

David Jackson

unread,
Dec 26, 2001, 8:54:19 PM12/26/01
to php-g...@lists.php.net
Greetings --
PHP + MySQL + sessions?

I'm trying use the script( and html page) to below to set session
variables, but
keep getting the follow errors:
NOTE: I seen several posting re: using $HTTP_SESSION_VAR is another case
where
that would apply?

Thanks in advance,
David Jackson


---------------- Browser Error Messages ---------------------
Warning: Cannot send session cookie - headers already sent by (output
started at
/home/sites/site13/web/connect.php:13) in
/home/sites/site13/web/frontdesk/sales/login.php on line 15

Warning: Cannot send session cache limiter - headers already sent (output
started at
/home/sites/site13/web/connect.php:13) in
/home/sites/site13/web/frontdesk/sales/login.php on line 15.


------------------- login.php ---------------------------------


<?php include("/home/sites/www.pickledbeans.com/web/connect.php"); ?>
<?php
$result = mysql_query("SELECT uid,uname,uperms FROM guestusers
WHERE uname = '$uname'
AND upass = PASSWORD('$upass')");

if (mysql_num_rows($result)!=1) {
echo "Dis ain't good !!\n";

mysql_free_result ($result);
mysql_close($link);

} else {
// start session
session_start();
session_register("Uid");
session_register("Uname");
session_register("Uperms");

// parse out query output and resign to session vars
list($uid,$uname,$uperms) = mysql_fetch_row($result);
$Uid = $uid;
$Uname = $uname;
$Uperms = $uperms;

// redirect to main menu page
mysql_free_result ($result);
mysql_close($link);


----------------- login.html ------------------------
<html>
<head>
<title>Generic Time Tracker</title>
</head>
<h3>Sales Entry Login screen:</h3>
<table border="0" cellspacing="5" cellpadding="5">
<form action="login.php" method="post">
<tr>
<td>Username:</td>
<td><input type="Text" name="uname" size="15"></td>
</tr>
<tr>
<td>Password</td>
<td><input type="password" name="upass" size="15"></td>
</tr>
<tr>
<td colspan="2" align="CENTER"><input type="Submit" name="submit"
value="Enter"></td>
</tr>
</form>
</table>
</body>
</html>

--


Jim Lucas

unread,
Dec 26, 2001, 11:39:32 PM12/26/01
to David Jackson, php-g...@lists.php.net
take the closing php tag from the end of the first line plus the openning
tag from the begining of the second line. that is sending a line feed to
the browser.
Jim

> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, e-mail: php-general...@lists.php.net
> For additional commands, e-mail: php-gene...@lists.php.net
> To contact the list administrators, e-mail: php-lis...@lists.php.net
>
>

Michael Sims

unread,
Dec 26, 2001, 9:03:33 PM12/26/01
to David Jackson, php-g...@lists.php.net
At 08:54 PM 12/26/2001 -0500, David Jackson wrote:
>I'm trying use the script( and html page) to below to set session
>variables, but
>keep getting the follow errors:
[...]

>---------------- Browser Error Messages ---------------------
>Warning: Cannot send session cookie - headers already sent by (output
>started at
[...]

Cookies are set and read via HTTP headers, therefore this has to occur
before any output is sent to the client. You can work around this with the
ob_start() and ob_end_flush() functions, but normally you'd want to move
the session_start() or session_register() calls up to the top of your
script. See below:

"Note: If you are using cookie-based sessions, you must call
session_start() before anything is output to the browser."

http://www.php.net/manual/en/function.session-start.php

"Note: In PHP 4, you can use output buffering to get around this problem,
with the overhead of all of your output to the browser being buffered in
the server until you send it. You can do this by calling ob_start() and
ob_end_flush() in your script, or setting the output_buffering
configuration directive on in your php.ini or server configuration files."

http://www.php.net/manual/en/function.header.php

Fred

unread,
Dec 27, 2001, 1:04:45 AM12/27/01
to php-g...@lists.php.net
This guy has some great modular php rpms that will do the trick:
http://rpms.arvin.dk/php/

Fred

Duane Douglas <ddou...@mindspring.com> wrote in message
news:4.2.0.58.2001122...@europa.your-site.com...
> hello,
>
> i have an apache rpm installed. stuff will break if i uninstall it. how
> do i install php to work with it?
>
> tia
>
> Duane Douglas
> ASP / SQL Server Tutoring and Training
>


Duane Douglas

unread,
Dec 27, 2001, 12:33:23 AM12/27/01
to php-g...@lists.php.net

Duane Douglas

unread,
Dec 27, 2001, 12:31:03 PM12/27/01
to Fred, php-g...@lists.php.net
At 10:04 PM 12/26/2001 -0800, Fred wrote:
>This guy has some great modular php rpms that will do the trick:
>http://rpms.arvin.dk/php/
>
>Fred
>
>Duane Douglas <ddou...@mindspring.com> wrote in message
>news:4.2.0.58.2001122...@europa.your-site.com...
> > hello,
> >
> > i have an apache rpm installed. stuff will break if i uninstall it. how
> > do i install php to work with it?

thanks for the link. unfortunately, the rpm packages there don't work with
rh linux 7.2. does anyone have another suggestion?

David Jackson

unread,
Dec 27, 2001, 9:36:12 PM12/27/01
to mhs...@midsouth.rr.com, php-g...@lists.php.net
Michael (and list) --
I've made the adjustment suggested the scripts are below.
What I'm trying todo is pass the "uid" and "mid" to the sales.php script.
It works the first time!! But if I reload sales.html the next record( w/
same uid) isn't added or if I login in as a different id the records aren't
added?
Scripts: login.php and sales.php.

Thanks for your time and knowledge,
David Jackson

----- login.php -----------
<?php


// start session
session_start();
session_register("Uid");

session_register("Umid");
// Connect to db
include("/home/sites/www.pickledbeans.com/web/connect.php");

// insert new user
$result = mysql_query("SELECT id,mid,name,perms FROM staff
WHERE name = '$frm_name'");

if (mysql_num_rows($result)!=1) {
echo "Dis ain't good !!\n";

echo "<html><body>";
echo "<meta http-equiv=\"refresh\" content=\"5;
url=http://backoffice.pickledbeans.com/index.html\">";
echo "</body></html>";
mysql_free_result ($result);
mysql_close($link);

} else {
echo "We have winner:\n";

// parse out query output and resign to session vars

list($id,$mid,$name) = mysql_fetch_row($result);
echo "<p>$id</p>";
echo "<p>$name</p>";
$Uid = $id;
$Umid = $mid;
$Uname = $name;


// redirect to main menu page
mysql_free_result ($result);
mysql_close($link);

echo "<html><body>";
echo "<meta http-equiv=\"refresh\" content=\"5;
url=http://backoffice.pickledbeans.com/sales.html\">";
echo "</body></html>";

}
?>

---------- sales.php ---------------
<?php
// If sesssion vars don't exist, goto login form
// else display sales data entry form
session_start();
// if(!session_is_registered("Uid")) {
// echo "<p>We need to goto login screen!!</p>";
// } else {
echo "<p>Session uid set to:$Uid</p>\n";
echo "<p>Session uid set to:$Umid</p>\n";
echo "<p>Session date: $frm_date</p>\n";
echo "<p>Session amount set to: $frm_amount</p>\n";
//
include("/home/sites/www.pickledbeans.com/web/connect.php");
// Insert Sales data into sales table
mysql_query("INSERT INTO sales(id,mid,date,amount)
VALUES('$Uid','$Umid','$frm_date','$frm_amount')") ;
mysql_close($link);

echo "<html><body>";
echo "<meta http-equiv=\"refresh\" content=\"5;
url=http://backoffice.pickledbeans.com/sales.html\">";
echo "</body></html>";
?>

David Jackson

unread,
Dec 27, 2001, 10:09:29 PM12/27/01
to mhs...@midsouth.rr.com, php-g...@lists.php.net
All --
It appears that the problem was a table design problems,
my primary index wasn't set to auto-increment? So I was trying to
insert records with duplicate keys !!
This of course leads to another question, how do I return SQL error messages
to the browser?

Thanks,
David

--


Daniel Grace

unread,
Dec 28, 2001, 4:56:33 AM12/28/01
to php-g...@lists.php.net

"David Jackson" <david.j...@pickledbeans.com> wrote in message
news:3558.63.165.192.13...@webmail.cqhost.com...

Out of habit, I abstract all of the MySQL functions to handle this. My
current setup for http://hosting.venura.net/ (still under construction).
outputs error messages like this:

function sqlQuery($query, $secure = 0) {
$result = mysql_query($query);
if(!$result) {
if($secure) {
$query = '*** hidden ***';
}
else {
/* This is just an oddity from how I construct some queries, primarily
INSERTs. If all of your queries are on a single line of text with no
leading/trailing whitespace or you don't care about appearance, you can
delete this section of the code */

$query = explode("\n", $query);
foreach($query as $line => $text) {
$query[$line] = trim($text);
}
$query = implode("\n", $query);
}

die('<h1>Oops...</h1><p><b>Sorry, but something seems to have gone
terribly wrong...</b></p>
<pre>
MySQL Error Number: ' . htmlspecialchars(mysql_errno()) . '
MySQL Error Description: ' . htmlspecialchars(mysql_error()) . '

Query Text:
' . htmlspecialchars($query) . '

</pre>
<p>(Yes, you probably have found a bug.)</p>
');
} else return ($result);
}


This will dump the query, error number and error description whenever a SQL
query fails. You can set the optional second parameter ($secure) to true if
your query involves anything that you don't want the end-user to see, such
as updating a password or something like that.

disclaimer: I didn't check to see what database engine you're using. If
you're using MySQL, this should work, otherwise use something similiar.

0 new messages