Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Decrease a variable by one when the user leaves the page?

Posted on 2004-10-01
8
Medium Priority
?
295 Views
Last Modified: 2008-02-26
Hi,

How can decrease a variable by one when the user leaves the page?
I.E:
$visfile=fopen("visitors1.txt", "r");
$visitors=fread($visfile, filesize("visitors1.txt"));
$visitors=$visitors + 1;
$visfile2=fopen("visitors1.txt", "w");
fwrite($visfile2, $visitors);
fclose($visfile2);
echo "Current Session Users: " . $visitors;
fclose($visfile);
Adds 1 to the file when the user comes to the page, but how do i set $visitors relative to -1? Any help would be much apreciated.

Thx,
Jamison
0
Comment
Question by:Gonavitch
  • 2
4 Comments
 
LVL 36

Accepted Solution

by:
Zyloch earned 268 total points
ID: 12203008
Hey, not sure what you're really talking about...

You can do this:

$visfile=fopen("visitors1.txt","r+");
$visitors=fread($visfile,filesize($visfile));
$visitors+=1;
reset($visfile);
fwrite($visfile,$visitors);
fclose($visfile);

echo "Current Session Users: " . $visitors;



If you want it relative to -1, start off by putting -1 inside visitors1.txt
0
 
LVL 4

Assisted Solution

by:cachedVB
cachedVB earned 268 total points
ID: 12203526
make it an onunload event in javascript
0
 
LVL 1

Author Comment

by:Gonavitch
ID: 12204947
never mind, i solved my problem.
0
 
LVL 4

Expert Comment

by:cachedVB
ID: 12220791
Doesn't he need to post his answer? I am curious on ohow he did it.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses

783 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question