Jump to content

session-related question, noob here, don't shoot!


stolzussen

Recommended Posts

                Hi everyone this is my first post. I have just recently entered the world of PHP coding and as anyone would expect i am having some problems. Cutting to the chase, here is my misunderstanding
[hr]
I have this very simple script:

[b]<!doctype html public "-//W3C//DTD HTML 4.0//EN">
<html>
<head>
  <title>Untitled web-page</title>
</head>
<body>
<?php

// initialize a session
session_start();

// increment a session counter
$_SESSION['counter']++;

// print value
echo "You have viewed this page " . $_SESSION['counter'] . " times";

?>
</body>
</html>[/b]

In the php.ini file i have the session_savepath to a local folder on my computer. I need to use a specific destination related to the webserver in order for the script to work properly?

My problem is that i always get the same value each time i load the page, namely 1, and when i check the sessions folder i see a new session file is created each time i load the page. What should i change?

I am running Apache 2 webserver, PHP 5. I've tried browsing with Firefox and IE but i have the same problem.

Thanks you.
[code]
<?php
session_start();
?>
<!doctype html public "-//W3C//DTD HTML 4.0//EN">
<html>
<head>
  <title>Untitled web-page</title>
</head>
<body>
<?php
// increment a session counter
$_SESSION['counter']++;

// print value
echo "You have viewed this page " . $_SESSION['counter'] . " times";

?>
</body>
</html>
[/code]

If you didn't get any errors loading your code, you should set error_reporting = E_ALL & ~E_NOTICE in php.ini, it would have told you what the problem was.
Good suggestion. I get the error:

Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at C:\NETSERVER\www\counter.php:7) in C:\NETSERVER\www\counter.php on line 10

Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at C:\NETSERVER\www\counter.php:7) in C:\NETSERVER\www\counter.php on line 10
You have viewed this page 1 times

I'll look that up in the FAQ, i noticed many people get this error.
[code]<!doctype html public "-//W3C//DTD HTML 4.0//EN">
<html>
<head>
  <title>Untitled web-page</title>
</head>
<body><?php

// initialize a session
session_start();

// increment a session counter
$_SESSION['counter']++;

// print value
echo "You have viewed this page " . $_SESSION['counter'] . " times";

?>
</body>
</html>[/code]


Tis is exactly how my php file looks like. Should i also omit the <body> tags?
You can have HTML.

You just cant have any BEFORE the session_start();

Why? Well its really because its part of the header, and when you output something else, it closes the header. But you really dont need to know that - just know that the session_start() needs to be above any output (i.e. HTML).

So as long as you do the session start at the top - it should all work fine. Such as the following.

[code]
<?php

// initialize a session
session_start();

?>
<!doctype html public "-//W3C//DTD HTML 4.0//EN">
<html>
<head>
  <title>Untitled web-page</title>
</head>
<body><?php

// increment a session counter
$_SESSION['counter']++;

// print value
echo "You have viewed this page " . $_SESSION['counter'] . " times";

?>
</body>
</html>
[/code]

Which works fine on my server.

JP

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.