/    Sign up×
Community /Pin to ProfileBookmark

session issues (should be simple)

heres the code residing in a file on my server running apache 2 and php v 4.3.8:
(btw register globals is on, but as far as I understand it, that should not affect the code below)

[code=php]
<?php
session_start();
if(!isset($_SESSION[‘count’])) $_SESSION[‘count’]=1;
else $_SESSION[‘count’]++;
echo $_SESSION[‘count’];
?>
[/code]

The intent, I hope, is obvious – everytime I reload or revisit the page, it should display a number incremented by one.

However – all I get is “1”. everytime. one one one … argh! This is important to me. Can anybody give me suggestions on where to start finding the source of these troubles?
Thanks!

EDIT: I thought I also should mention that I have tried adding: echo session_id(); at the bottom of the code, and my session ID is always the same, so I assume I have a live session, but the variable just wont seem to stick!

to post a comment
PHP

4 Comments(s)

Copy linkTweet thisAlerts:
@NogDogFeb 22.2005 — I don't think sessions are what you want for this; it sounds more like a job for cookies.
[code=php]
<?php
$visits = 1;
if(isset($_COOKIE['count']))
{
$visits = $_COOKIE['count'] + 1
}
echo "<p>This is visit number $visits.</p>n";
if(set_cookie('count', $visits, 60*60*24*365) !== TRUE)
{
echo "<p><i>Note: your browser did not allow me to set a cookie, ".
"so number of visits will always say '1'.</i></p>n";
}
?>
[/code]
Copy linkTweet thisAlerts:
@zauberauthorFeb 22.2005 — Well, as far as I understand it, sessions are intended as a replacement possibility for cookies (like cookies stored on the server instead of the client). Secondly, I do need to use sessions - the reason is that in my system, the user will at several occasions submit data to the server (randomly) and only after certain critical pieces of data have been submitted may I process it. Therefore I need to buffer a users submissions on the server. The above code is really a piece of test code, to help me pinpoint why my real code isnt working properly.

After writing the above, I do realize that cookies could accomodate my needs in much the same way a session could. However cookies are unreliable at the time. Are you sure sessions cannot do what I posted above?
Copy linkTweet thisAlerts:
@NogDogFeb 22.2005 — The session ID gets transmitted from one page in the session to another. If you come into a session-controlled page from an "outside" page, a new browser session, etc.; then your session_start() will be creating a new session ID and will have no $_SESSION data associated with it yet. Also, session data normally has a relatively short lifespan, depending on you PHP installation settings (measured in a small number of hours).

So sessions let you share data between related PHP pages, but it is not intended for long-time, persistent data the way cookies can be.

The other alternative would be some sort of database solution based on the user's login ID (if you have one).
Copy linkTweet thisAlerts:
@zauberauthorFeb 22.2005 — Thanks for all the tips!


I did finally get it working the way I had it to begin with though. The problem was (as it so often is) with the permissions on the path where session data is stored.
×

Success!

Help @zauber spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.16,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...