473,406 Members | 2,343 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

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
Jun 2 '08 #1
9 1445
..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
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
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: renster | last post by:
I was wondering if someone could help me with a problem that I'm having in retrieving session variables on a site I am working on for a friend. I have made the site more developer friendly by...
1
by: Ann Leland | last post by:
I have been using session variables to pass a user name from one ASP page to another inside framesets for 9 months and it stopped working this week. I have made no code changes but there was a...
3
by: Rob | last post by:
I'm trying to use session variables to store datasets when moving from page to page (and returning). The datasets are small (I know some may comment on performance issues). It seems simple to use....
6
by: Nedu N | last post by:
Hi, I am trying to design a Home page for my applicatiion in which i want show the links for for some itms... I tried to put the following <td> <font face="Arial, Helvetica, sans-serif" ...
1
by: Werner | last post by:
Hi Patrick! Can you give an example of how to use a frameset inside an aspx-file? When I create a new frameset in Visual Studio.Net it just gives me a htm-File. Or give me a link where I can...
2
by: Aryan | last post by:
Hi, I have put my session related parameter in web.config under system.web, given below is the code for same. <system.web> <sessionState timeout ="1440" mode ="InProc" /> </system.web> this...
4
by: Parrot | last post by:
For some reason my Session state stopped working in my webpages. I cannot pass a variable from one webpage to another using the Session. For example: Web page 1: Session = dbname;...
25
by: =?Utf-8?B?RGF2aWQgVGhpZWxlbg==?= | last post by:
I tried: <sessionState timeout="1"> </sessionState> bounced IIS, and after 1 minute still had a session. ??? -- thanks - dave
2
by: fniles | last post by:
I am using Visual Studio 2005. In my ASPX page, when I try to use Session, I got the error "Session state can only be used when enableSessionState is set to true, either in a configuration file or...
6
by: ChrisAtWokingham | last post by:
I have been struggling with unexpected error messages on an ASP.NET system, using SQL and C#. The application draws organisation charts, based on data stored in the SQL database. Some of the chart...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.