Connecting Tech Pros Worldwide Help | Site Map

session not working on a php page

Suman
Guest
 
Posts: n/a
#2: Jun 2 '08

re: session not working on a php page


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
Suman
Guest
 
Posts: n/a
#1: Jun 2 '08
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
Michael Fesser
Guest
 
Posts: n/a
#3: Jun 2 '08

re: session not working on a php page


..oO(Suman)
Quote:
>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']++;
Quote:
>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
Jerry Stuckle
Guest
 
Posts: n/a
#4: Jun 2 '08

re: session not working on a php page


Suman wrote:
Quote:
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.
jstucklex@attglobal.net
==================

C. (http://symcbean.blogspot.com/)
Guest
 
Posts: n/a
#5: Jun 2 '08

re: session not working on a php page


On 13 Apr, 13:15, Suman <YoS...@gmail.comwrote:
Quote:
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.
Suman
Guest
 
Posts: n/a
#6: Jun 2 '08

re: session not working on a php page


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
Jerry Stuckle
Guest
 
Posts: n/a
#7: Jun 2 '08

re: session not working on a php page


Suman wrote:
Quote:
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.
jstucklex@attglobal.net
==================

=?ISO-8859-1?Q?=22=C1lvaro_G=2E_Vicario=22?=
Guest
 
Posts: n/a
#8: Jun 2 '08

re: session not working on a php page


Suman escribió:
Quote:
<?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
--
google@shopdownlite.com
Guest
 
Posts: n/a
#9: Jun 2 '08

re: session not working on a php page


On Apr 14, 7:22*am, "C. (http://symcbean.blogspot.com/)"
<colin.mckin...@gmail.comwrote:
Quote:
On 14 Apr, 03:09, Suman <YoS...@gmail.comwrote:
>
Quote:
I have tried the new code
>
Quote:
<?php
session_start();
if (isset($_SESSION['counter']))
* *$_SESSION['counter']++;
else
* *$_SESSION['counter'] = 1;
echo $_SESSION['counter'];
?>
>
Quote:
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?
>
>
>
Quote:
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.
>
Quote:
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
C. (http://symcbean.blogspot.com/)
Guest
 
Posts: n/a
#10: Jun 2 '08

re: session not working on a php page


On 14 Apr, 03:09, Suman <YoS...@gmail.comwrote:
Quote:
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?
Quote:
>
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.
Quote:
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.
Closed Thread