get time of when post is submitted php - php

I have a text box that submits text to an outside file. I am using
strftime("%I:%M:%S %p");
to get the time that it was sent, however, when the browser is refreshed the time changes. How do I make it so that it is the time of when the submit button is clicked and no the local time?

You probably have to store the time to the file so that when you display it content you read back the time as well.
Whether you use the client time or the server time is probably not really important, as long as you take this time when you write to the file and store this time with the data.

Related

How to display a dialog on an already open webpage whenever new data is available to be displayed?

I need two things to happen periodically in the background:
I need to check every 5 minutes if there is a new file in a directory on the server, and if there is, I need to load data from that file into the database and then delete the file from the directory.
Every time I load new data into the database, I need to check if the webpage which is displaying data from the database is open in users' browsers. If it is open, I want to display a dialog telling the user to reload the webpage because new data is added in the database.
For 1, I have thought that I should create a cron job to execute a PHP script every 5 minutes. And in this PHP script, I will check if there is a file in the directory and if there is, I will load data from that file into the database, and then delete the file.
But I am confused about 2. How should I go about it? How will I inform users who have already opened the web page, that new data is available, and they should reload the web page so that new data is pulled from the database and displayed.
Your solution to the first point seems logical to me. Your second point can be handled with periodical ajax requests. You use Javascript to call a page on the server to see if any new data has been added to the database.
I think a good way to do this is to add a variable to the user's session, this variable keeps a timestamp on which the check has been done for the last time. When you use AJAX to call the script, it'll check the database whether any new record has been created since then and update the variable with the current timestamp.
The script then returns whether there are new records and with Javascript you can add a message to your page. You can even return the data if you want, so the user doesn't have to reload the page.

send refresh request to page

I would like to refresh a page as soon as a processs on the server is completed. I wouldn't know where to begin or where to start my search.
I have to following scheme, which works perfectly:
A user uploads a file to the server via a website
The server checks whether the file is valid and copies the file to another folder.
In the other folder the file has to wait to be processed by a program (Filemaker Server) that is scheduled to run every 1 minute (or whatever interval).
After the program has finished processing the file it marks a MySQL database entry with "0 no error" or whatever was returned by the process.
So far so good. I would like the user of the website to know when the process is ended. I already have a timer that counts down to 0, which is the amount of seconds until the processing starts. The user is then presented with the text "processing started".
Now, I would like to send a refresh request to the client browser at the moment the processing ends. This should be either some batch file command or a mysql function; I really don't have a clue. I did try <META HTTP-EQUIV="REFRESH" CONTENT="1";URL=home.php">, but the refreshes (every 1 second or any other interval) are not acceptable, because the user has to upload a file through a form which is unfortunately cleared whenever a refresh takes place. (This is not a problem when a server request would refresh the page, because only one upload process can take place at a time.)
Thanks in advance for any suggestions on this matter!
Have you ever read on AJAX?
I think a jQuery Ajax Upload is everything you need of. In the server-side you get the script, treat it, upload it, insert into the database some information regarding it, and then you inform the client-side the upload is over and the insertion has been successfully.
use javascript:
<script rel=JavaScript>window.location="home.php";</script>
I finally settled for using AJAX as per Ivo's suggestion. It is fairly easy to use! Thanks!

Get Form data from html on page refresh(php)

I ran into a little problem with my college project again :(
<input type="text" name="timrem" value="" />
This is a hidden field that I want the data from.
if(isset($_REQUEST['timrem']))
{
$tim=$_REQUEST['timrem'];
}
This is the code used to get the value from timrem.
The variable timrem is used to initialize a countdown sequence on the page(implemented using javascript)
<span id="countdown-1" style="float:right"><?php echo $tim;?>
The value of the hidden field is dynamically changed via java script counter implemented.
sremtime=(parseInt(minRemain*60)) + (parseInt(secsRemain));
document.frmTest.timrem.value=sremtime;
this works fine when the page is submitted, ie, the time doesn't restart from the next page. But when the page is refreshed, the timer is restarted to the value of the $tim when the timer first started.
Is it possible to somehow get the value of the hidden input on page refresh?
And also, please comment on the approach taken to preserve the time elapsed to the next page. This is a project on online examination.
If you need your variable to persist across refreshes, you cannot rely on information passed back from client to server. The only information you can rely on is the information passed with the very first request, and that obviously doesn't include your time. The best solution I can think of is to store your timestamp in a session variable on the server:
session_start();
if(empty($_SESSION['timrem'])) $_SESSION['timrem'] = time(); // set this if it is the first time this page is loaded
$tim = time() - $_SESSION['timrem']; // $tim is now the number of seconds since the page was first loaded in this session
You do not need to pass the time back and forth between client and server to keep track of it, although your javascript will probably need it.
A good idea might be storing a timestamp either in your PHP page or in a database (for multiple users), and use it to generate $tim. Passing data on refresh doesn't seem possible as there is no request triggered.
How about you store a "target time" (what ever time remaining is counting down to) in your url. you could even store it in epoch time:
myurl.com/stuff.html?target_time=1359553896
that way when the page is refreshed you still have it with you and you can calculate time remaining from that. if you don't want it in your url, you could probably hide it on the page somewhere with a display:none style.
how about something like this:
http://jsfiddle.net/QpZcV/1/

php with javascript timer - what happen when user leaves the page before the timer is zero

i have a game whene the user can win something.
as soon as he wins he has 60 seconds to fill in his adress name etcotherwise he lose his price and someone else can take it.
when he not fills in the adress in 60 seconds then he gets redirected to a sorry too late page and he lose his price.
when he fills in his adress he gets redirected to a confirmation page and can claim his price.
i made this with php and the counter is in javascript.
the javascript will do the redirect when time = 0
how can i controll that the user either goes to the confirmation page or to the soory to late page.
i'm wondering how i can be sure that the javascript countdown is really counted to zero even wehn the users leaves the page.
thanks a lot
You can't be sure - your users can easily modify your javascript to increase the time limit or even disable the timer completely.
The time limit should be validated on the server. I would suggest that when the time limit starts the server stores a row in the database storing which user it is and when the time limit started. When the user submits the server should check the database to see how much time has passed.
I also think that having just 60 seconds to fill in your address is rather harsh, but that's your decision.
try not to trust only javascript for this job, use php to write the time in the session or an database when the user gets the form to fill in his data - check the difference (with php) between this starting-time and the end-time when the user submits the form to the server
and show him the confirmation when diff<=1min or show him sorry-to-late when diff>1min
you could do this, use javascript for user interfase, and when user leaves a filed execute ajax call to php script and save data that he just entered in field. And real counting make in php, so when user submits final data u should also check if time expired in php.
I think this is good solution for your problem.
If you do this in Javascript, anybody can read your script and find where the prize page is.
Instead, run your timer with PHP using microtime(). You can start when they see the "You Win" page. It will run in the background, and won't depend on any user interaction.

cache or not to cache

I have an ajax application where the client might lookup bunch of data frequently(say by key stroke), the data gets updated on the server side once or twice a day at fixed times by a demon process. To avoid visiting the server frequently, I store the data in a xml file, so the client downloads it once when the page first loads, then look the data up from local data file via javascript.
But the user might load the page shortly before the changes, then start using it without ever refreshing the page, so the data file never gets updated, hence keep telling user the new data is not available.
How do I solve this issue?
You should set the appropriate HTTP cache headers for that generated XML file so any client request past that time will get the new version and cache it locally, like any other static content.
If the data load is not very large... Include the data in the main document as an XML island. Either form it in document generation (aspx, php, whatever) or fill in (via ajax calls) a reserved document node upon loading. This way, your user always has the latest data, you do not have to worry about caching, and life is much simpler.
If it is large, fill in that node as needed via ajax calls.
One obvious option is to add some AJAX that polls the server every x minutes. If the data needs refreshing just show a non-blocking message somewhere obvious on the page notifying the user that fresh data is available and provide a link to refresh the page. As an extra you might want to provide a button for the user to click if they want to check for fresh data (rather than waiting for x minutes to elapse) themselves.
If you use a HEAD request you can just check the last-modified header.
You said that the update time is FIXED? So when the user visit your page SHORTLY before the update time to come, you can set a javascript variable to you page that indicate how many minutes, for example, until the next update, and run a client-side timer such as:
timer = {
run: function() {
if( now + minuteToUpdate > updateTime - startVisitTime ) {
// make ajax request here to update XML file
}
},
interval: //you can determine this since this will run in client-side
}
Do not you POLLING in this stiuation because it's waste and stressed to call server every time.
You can set some SESSION variable to help this run better and more exactly
Justin
What runtime said...
Or, since update times are fixed and infrequent, then when you serve your XML, also include a cache expiration time as an element or custom header. This way, if your user visits the site 1 minute before the XML update, you can code your client to expire its cache and update itself on the next request made after that 1 minute mark.

Categories