session not working on a php page

session not working on a php page

am 13.04.2008 14:15:56 von suman

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

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

Re: session not working on a php page

am 13.04.2008 14:43:13 von Michael Fesser

..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
>
> >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

Re: session not working on a php page

am 13.04.2008 14:47:10 von suman

Thanks for the quick answer Micha.

I tried simply using

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

Re: session not working on a php page

am 13.04.2008 15:48:22 von Jerry Stuckle

Suman wrote:
> Thanks for the quick answer Micha.
>
> I tried simply using
>
> > 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:

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
==================

Re: session not working on a php page

am 13.04.2008 22:19:11 von colin.mckinnon

On 13 Apr, 13:15, Suman wrote:
> 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
>
> > 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.

Re: session not working on a php page

am 14.04.2008 04:09:50 von suman

I have tried the new code


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

Re: session not working on a php page

am 14.04.2008 04:47:37 von Jerry Stuckle

Suman wrote:
> I have tried the new code
>
>
> > 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
==================

Re: session not working on a php page

am 14.04.2008 10:18:28 von alvaroNOSPAMTHANKS

Suman escribió:
> > 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
--

Re: session not working on a php page

am 14.04.2008 13:22:12 von colin.mckinnon

On 14 Apr, 03:09, Suman wrote:
> I have tried the new code
>
> > 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 )

C.

Re: session not working on a php page

am 14.04.2008 15:57:12 von google

On Apr 14, 7:22=A0am, "C. (http://symcbean.blogspot.com/)"
wrote:
> On 14 Apr, 03:09, Suman wrote:
>
> > I have tried the new code
>
> > > > session_start();
> > if (isset($_SESSION['counter']))
> > =A0 =A0$_SESSION['counter']++;
> > else
> > =A0 =A0$_SESSION['counter'] =3D 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 )
>
> 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