By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,890 Members | 1,050 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,890 IT Pros & Developers. It's quick & easy.

Cannot get a session counter to work

P: n/a
Hi
I cannot get sessions to work.
I have the following code
</body>

<?
session_start();
$counter++;
print "You have visited this page $counter times during this session";
session_register("counter");
?>
</html>

And get the following output
Warning: session_start() [function.session-start]: Cannot send session
cookie - headers already sent by (output started at /home/ted/
public_html/store/test/testsession.php:15) in /home/ted/public_html/
store/test/testsession.php on line 16

Warning: session_start() [function.session-start]: Cannot send session
cache limiter - headers already sent (output started at /home/ted/
public_html/store/test/testsession.php:15) in /home/ted/public_html/
store/test/testsession.php on line 16
You have visited this page 1 times during this session

What is going wrong????

-Ted
Oct 5 '08 #1
Share this Question
Share on Google+
3 Replies


P: n/a
..oO(te*******@gmail.com)
>Hi
I cannot get sessions to work.
I have the following code
</body>

<?
session_start();
$counter++;
print "You have visited this page $counter times during this session";
session_register("counter");
?>
</html>

And get the following output
Warning: session_start() [function.session-start]: Cannot send session
cookie - headers already sent by (output started at /home/ted/
public_html/store/test/testsession.php:15) in /home/ted/public_html/
store/test/testsession.php on line 16

Warning: session_start() [function.session-start]: Cannot send session
cache limiter - headers already sent (output started at /home/ted/
public_html/store/test/testsession.php:15) in /home/ted/public_html/
store/test/testsession.php on line 16
You have visited this page 1 times during this session

What is going wrong????
A lot:

* You can't have any output before the session_start() call. Move it to
the top of the page _before_ any HTML. This should fix the "headers
already sent" issue.

* Use <?php instead of <?. Short open tags are unreliable.

* Check your php.ini and make sure that error_reporting is set to E_ALL
and display_errors are on. Your code above should throw an E_NOTICE
error.

* You don't need session_register() anymore. Use something like this to
properly initialize and increase the counter:

session_start();
if (!isset($_SESSION['counter'])) {
$_SESSION['counter'] = 1;
} else {
$_SESSION['counter']++;
}

Later simply use

print $_SESSION['counter'];

to output it.

HTH
Micha
Oct 5 '08 #2

P: n/a
-Thank you, it works now!!!!
-Ted
On Oct 5, 1:26*pm, Michael Fesser <neti...@gmx.dewrote:
.oO(tedpot...@gmail.com)


Hi
I cannot get sessions to work.
I have the following code
</body>
<?
session_start();
$counter++;
print "You have visited this page $counter times during this session";
session_register("counter");
?>
</html>
And get the following output
Warning: session_start() [function.session-start]: Cannot send session
cookie - headers already sent by (output started at /home/ted/
public_html/store/test/testsession.php:15) in /home/ted/public_html/
store/test/testsession.php on line 16
Warning: session_start() [function.session-start]: Cannot send session
cache limiter - headers already sent (output started at /home/ted/
public_html/store/test/testsession.php:15) in /home/ted/public_html/
store/test/testsession.php on line 16
You have visited this page 1 times during this session
What is going wrong????

A lot:

* You can't have any output before the session_start() call. Move it to
the top of the page _before_ any HTML. This should fix the "headers
already sent" issue.

* Use <?php instead of <?. Short open tags are unreliable.

* Check your php.ini and make sure that error_reporting is set to E_ALL
and display_errors are on. Your code above should throw an E_NOTICE
error.

* You don't need session_register() anymore. Use something like this to
properly initialize and increase the counter:

session_start();
if (!isset($_SESSION['counter'])) {
* $_SESSION['counter'] = 1;} else {

* $_SESSION['counter']++;

}

Later simply use

print $_SESSION['counter'];

to output it.

HTH
Micha- Hide quoted text -

- Show quoted text -
Oct 6 '08 #3

P: n/a
..oO(te*******@gmail.com)
>-Thank you, it works now!!!!
You're welcome.

Micha
Oct 6 '08 #4

This discussion thread is closed

Replies have been disabled for this discussion.