PDA

View Full Version : PHP Session Variables


waynew
03-01-2002, 05:23 PM
Unable to use session variables. Any help? I tried (below)

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html><head><title>Test of PHP</title></head><body>
<?php
$count=3;
if (!session_is_registered('count')) {
session_register("count");
$count = 0;
}
else { $count++; }
echo "count $count";
?>
<h1>123</h1>
</body>
</html>


Warning: Cannot send session cookie - headers already sent by (output started at /home/xxxx/public_html/test3.php:8) in /home/xxxx/public_html/test3.php on line 12

Warning: Cannot send session cache limiter - headers already sent (output started at /home/xxxx/public_html/test3.php:8) in /home/xxxx/public_html/test3.php on line 12
count 0
123

-------- and ------------
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html><head><title>Test of PHP</title></head><body>
<?php
$count=4;
if (isset($HTTP_SESSION_VARS['count'])) {
$HTTP_SESSION_VARS['count']++;
}
else { $HTTP_SESSION_VARS['count'] = 0;
}
echo "count $count";
?>
<h1>124</h1>
</body>
</html>

count 4
124
--Wayne

JoeF
03-01-2002, 05:43 PM
You need to run session_start and session_register before you have any output from php
ie. you need to put it before the <html> etc..

waynew
03-01-2002, 06:04 PM
Appears to have worked but still get warning error ??

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<?
session_start();
session_register("HiThere");
$HiThere="Hello world!";
if (session_is_registered('HiThere'))
{
echo "The content of HiThere is $HiThere<br>";
}else{ echo "Not registered"; }
?>
<html>
<head><title>Test of PHP</title></head>
<body>
test 5
</body></html>

Warning: Cannot send session cookie - headers already sent by (output started at /home/consult/public_html/test5.php:2) in /home/consult/public_html/test5.php on line 3

Warning: Cannot send session cache limiter - headers already sent (output started at /home/consult/public_html/test5.php:2) in /home/consult/public_html/test5.php on line 3
The content of HiThere is Hello world!
test 5

alan92rttt
03-04-2002, 04:02 PM
You session start must be the first command.

Try changing the code from :
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<?
session_start();
session_register("HiThere");
$HiThere="Hello world!";

to:

<?php session_start(); ?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<?php
session_register("HiThere");
$HiThere="Hello world!";