Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

Detect RELOAD in cgi and how to figure out the date?

Posted on 1997-06-07
Last Modified: 2013-12-25
I want to know if someone is reloading my page(ie, click the
RELOAD button), can I  use some variables in cgi to do this?

How to get the date since there is no such environment variable available? please tell me in details. (say currentTime = date(...) or something like that)

And finally, I want to figure out who are currently on
my page, that is, to find out all the users who are currently on my page.(use REMOTE_ADDR can get only one
of them)

Question by:carrot

Expert Comment

ID: 1828190
I have an answer for the second question(date).  You need to use the time function(gives you seconds since 1970) then feed that into the localtime function.  It returns a list with the date:
($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst)= localtime(time);
$mday is the day of the month
$mon is the month
$year is the year
$wday is the day of the week


Expert Comment

ID: 1828191
Regarding the first question:

You could set up an invisible applet on your page. Whenever a page is loaded (or reloaded) Java's Applet.start() method is called. You could set up a counter that determines how many times the page has been loade. If > 1, and the time frame is less than say 5 or 10 seconds, the user is refreshing the page.

Re.: question 2, an applet can detect the IP of the user that loaded it, it could transmit this info, via a socket, to a remote server.

Re.: question 3, an applet can also obtain time information and save it on a file on the host.

Of course, you posted this question on the CGI topic section so I don't know if an applet solution is acceptable to you, thus I post as a comment and don't give additional details.

Your CGI code could read a file generated by an applet though, to obtain the info you require so, if no one can produce a better answer and/or a combination applet/CGI solution is acceptable I'd be glad to post an answer with additional details.

Accepted Solution

julio011597 earned 80 total points
ID: 1828192
Ok, my turn:)

1. hitting 'Reload' just makes your browser contact the web server and get the current page again; the only difference between submitting a url and hitting 'Reload' should be that, in the latter case, the browser wouldn't care of document's date and would override its own caching.

So, how to detect a 'Reload'? The only trusted method that comes to mind is to look at the web server access log file:
each request is logged, and you may check for the status code of the answer; each page send by the web server generates a 200 code (the OK code), but a page already send to a browser and unchanged in the meantime, generates a 304 code ('Not Modified')- anyway, check your server documentation for status codes.

This means that:
when a user asks for a page, the web server answers with a 200 code (the page is sent);
if the user goes to that page again (with a 'Go' menu command or entering the url in the 'Location' area), and the page is in the browser's cache, no request is submitted to the web server, and the browser reads its cache;
if the user hits 'Reload', the browser contacts the web server, which, at its turn, answers with a 304 code ('Not Modified').

This said, please note that, if the url actually points to a CGI, the server answer is _always_ 200 (and the CGI is run), so, in the CGI case, there seems to be NO way to check for a reload.

2. n3mtr's answer works fine if your language is Perl; here is the corrensponding C code:

#include <time.h>

char time_string[32];
time_t timer;

/* timer = secs since the Epoch (1 Jan 1970) */
timer = time(NULL);
/* time_string = something like: "Tue Nov  9 15:37:29 1993" */
strcpy(time_string, ctime(&timer));

Have a look at time(3) and ctime(3) for reference.

3. There is NO way to detect who are currently on your page; this is because of the concept underlying the HTTP protocol itself: open a connection, give the file, _close_ the connection; i.e., once you've given the page, you lose any contact with the client.
The only workaround would be a Server Push, or some Javascript, to keep the connection open; but note that this may dramatically increase your web server load.
Think that, usually, a web server keeps a connection open for a few seconds (maybe less); with server push (or such), each client needs his/her own connection for as long as (s)he stay on your page, and your web server may end up serving lots of sockets at the same time.

Hope this makes things a bit clearer,


P.S. IMHO, this answer is worth more than 80 points.

Author Comment

ID: 1828193
This answer helps a lot, thanks.
but I can't give more points since I have nearly none
left. Think I may want to ask one or more questions. ^.^

Thanks again.

Featured Post

How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

Question has a verified solution.

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

Suggested Solutions

In this tutorial I will aim to show you how simple is making a small application in WhizBase, how to add, remove and update data in the DB. I will make a small address book application where you can add, browse, update and remove addresses. I wi…
This tutorial will discuss the log-in process using WhizBase. In this article I assume you already know HTML. I will write the code using WhizBase Server Pages, so you need to know some basics in WBSP (you might look at some of my other articles abo…
Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

839 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