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

session not working on a php page

P: n/a
Hi All,

I am trying to experiment with sessions and they seem to be resetting
everytime i visit the page.

here is the code of the page

<?php
session_start();
$count = $_SESSION["counter"] + 1;
$_SESSION["counter"] = $count;
echo $_SESSION["counter"];
?>

if I refresh the page, the count doesn't increase. Whast should i do
to fix this?

any help is much appreciated

cheers
Suman
Jun 2 '08 #1
Share this Question
Share on Google+
9 Replies


P: n/a
..oO(Suman)
>I am trying to experiment with sessions and they seem to be resetting
everytime i visit the page.

here is the code of the page

<?php
session_start();
$count = $_SESSION["counter"] + 1;
$_SESSION["counter"] = $count;
The above two lines could also be written as

$_SESSION['counter'] = $_SESSION['counter'] + 1;

or

$_SESSION['counter'] += 1;

or simply

$_SESSION['counter']++;
>echo $_SESSION["counter"];
?>

if I refresh the page, the count doesn't increase. Whast should i do
to fix this?
Do you use session cookies? Does your browser accept it?

Micha
Jun 2 '08 #2

P: n/a
Thanks for the quick answer Micha.

I tried simply using

<?php
session_start();
$_SESSION['counter']++;
echo $_SESSION['counter'];
?>

and it only displays 1 no matter how many times i refresh so still not
working. The browser is set to accept cookies as well.

suman
Jun 2 '08 #3

P: n/a
Suman wrote:
Thanks for the quick answer Micha.

I tried simply using

<?php
session_start();
$_SESSION['counter']++;
echo $_SESSION['counter'];
?>

and it only displays 1 no matter how many times i refresh so still not
working. The browser is set to accept cookies as well.

suman
If you enable all errors and display them, you'll find you get a notice
on the first run because $_SESSION['counter'] is not defined.

This should be:

<?php
session_start();
if (isset($_SESSION['counter']))
$_SESSION['counter']++;
else
$_SESSION['counter'] = 1;
echo $_SESSION['counter'];
?>

But other than the original notice, it should work OK.

You need to enable all errors and display them to find out what your
real problem is. In your php.ini file, you should have:

error_reporting=E_ALL
display_errors=on

These should be on for all development systems (but display_errors
should be off on production systems).

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================

Jun 2 '08 #4

P: n/a
On 13 Apr, 13:15, Suman <YoS...@gmail.comwrote:
Hi All,

I am trying to experiment with sessions and they seem to be resetting
everytime i visit the page.

here is the code of the page

<?php
session_start();
$count = $_SESSION["counter"] + 1;
$_SESSION["counter"] = $count;
echo $_SESSION["counter"];
?>

if I refresh the page, the count doesn't increase. Whast should i do
to fix this?

any help is much appreciated
Find the problem - the code is fine. But the most common cause of
cookie related problems is output before a header or cookie realted
statement - PHP is very good at intercepting and reporting this - why
have you not checked your logs? Do you know how errors are recorded/
reported?

Even if that turns up nothing - you should look at the HTTP data
passing to and from the browser - you can use firebug or tamperdata
for Firefox, or iehttpheaders for MSIE. Or just sniff the traffic with
wireshark. Then you can verify if the server is presenting a cookie,
and if the browsers retruns it in subsequent requests.

C.
Jun 2 '08 #5

P: n/a
I have tried the new code
<?php
session_start();
if (isset($_SESSION['counter']))
$_SESSION['counter']++;
else
$_SESSION['counter'] = 1;
echo $_SESSION['counter'];
?>

enabled the error trapping and there is no errors.

There is no output before the session_start, the code is as is on the
php page. I have had a loook through firebug and the cookie is there
too.

Can someone use this code on a webpage and refresh the page to see if
it works. If it does can you please send me a link. Would be great
help, this is killing me :)

Kind regards
Suman
Jun 2 '08 #6

P: n/a
Suman wrote:
I have tried the new code
<?php
session_start();
if (isset($_SESSION['counter']))
$_SESSION['counter']++;
else
$_SESSION['counter'] = 1;
echo $_SESSION['counter'];
?>

enabled the error trapping and there is no errors.

There is no output before the session_start, the code is as is on the
php page. I have had a loook through firebug and the cookie is there
too.

Can someone use this code on a webpage and refresh the page to see if
it works. If it does can you please send me a link. Would be great
help, this is killing me :)

Kind regards
Suman
Suman,

I've done it - as have others who confirmed it works.

Can't send you a link - it's on my development system, which has
restricted access.

When you say you "enabled error trapping" - did you add the lines I
recommended to your php.ini file?

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================

Jun 2 '08 #7

P: n/a
Suman escribió:
<?php
session_start();
$count = $_SESSION["counter"] + 1;
$_SESSION["counter"] = $count;
echo $_SESSION["counter"];
?>

if I refresh the page, the count doesn't increase. Whast should i do
to fix this?
From what I've read in the thread it looks like the temporary directory
for sessions does not exist or is not writeable by the web server. Check
the "session.save_path" directive in your php.ini file.

--
-- http://alvaro.es - Álvaro G. Vicario - Burgos, Spain
-- Mi sitio sobre programación web: http://bits.demogracia.com
-- Mi web de humor al baño María: http://www.demogracia.com
--
Jun 2 '08 #8

P: n/a
On 14 Apr, 03:09, Suman <YoS...@gmail.comwrote:
I have tried the new code

<?php
session_start();
if (isset($_SESSION['counter']))
$_SESSION['counter']++;
else
$_SESSION['counter'] = 1;
echo $_SESSION['counter'];
?>

enabled the error trapping and there is no errors.
How do you know? have you deliberately inserted a fatal run-time error
(like call_non_existant_fn()) and confirmed it gets logged where you
expected it?
>
There is no output before the session_start, the code is as is on the
php page.
How do you know? A BOM would not show in your text editor but causes
output from the script.
I have had a loook through firebug and the cookie is there
too.
This suggests that there is no output before the script. But is the
cookie there in both request and reply?

Have you checked if the session file is created on the webservers
filesystem? Is it getting updated each time?

Have you checked that you're not inadvertently caching content?

(try <?php print rand(100); ?)

C.
Jun 2 '08 #9

P: n/a
On Apr 14, 7:22*am, "C. (http://symcbean.blogspot.com/)"
<colin.mckin...@gmail.comwrote:
On 14 Apr, 03:09, Suman <YoS...@gmail.comwrote:
I have tried the new code
<?php
session_start();
if (isset($_SESSION['counter']))
* *$_SESSION['counter']++;
else
* *$_SESSION['counter'] = 1;
echo $_SESSION['counter'];
?>
enabled the error trapping and there is no errors.

How do you know? have you deliberately inserted a fatal run-time error
(like call_non_existant_fn()) and confirmed it gets logged where you
expected it?
There is no output before the session_start, the code is as is on the
php page.

How do you know? A BOM would not show in your text editor but causes
output from the script.
I have had a loook through firebug and the cookie is there
too.

This suggests that there is no output before the script. But is the
cookie there in both request and reply?

Have you checked if the session file is created on the webservers
filesystem? Is it getting updated each time?

Have you checked that you're not inadvertently caching content?

(try <?php print rand(100); ?)

C.

Some web servers also require you to declare the session_save_path.
So check that to make sure it is actually saving the sessions.

BDP
Down Pillow
ShopDownLite.com
Jun 2 '08 #10

This discussion thread is closed

Replies have been disabled for this discussion.