Jump to content

Timestamp - Setting itself on load and again on submit


TripleDES

Recommended Posts

I'm using the following as part of a unique ID:

 

$timestamp = date('Ymd.His');

 

However, I'm noticing the timestamp is getting set twice.  First when the page loads and second when the is submitted.  What can I do to only use the timestamp generated at page load?

Hi,

 

I presume you are using a form which self submits (e.g. has a blank action and submits back to the same script), and so the timestamp is set 1) When the form loads 2) When the form is submitted.

The solution to this is to check if the form has been submitted. If it has, don't bother setting the timestamp.

 

For example:

 

1) Add a hidden field to your form which you will check in order to ascertain if the form has been submitted or not:

 

<input type="hidden" name="postback" id="postback" value="1" />

 

2) Then in your script check for the existence of the postback variable and only set the timestamp if the variable does not exist (e.g. the form has not been posted yet).

 

if(!isset($_POST['postback']))
   $timestamp = date('Ymd.His');

 

Please not I am assuming you are using a post form and not a get form.

In my opinion, in most situations, Unix timestamps are better (in the sense of more usability).

 

They are numeric, and are the number of seconds since the Unix epoch (1/1/1970 00:00 GMT).

 

Then, you can format the number into a human readable date later.

 

http://php.net/time

http://php.net/date

 

 

Sorry for the offtopic-ness of this post, but I've noticed that people who use timestamps like you're doing often regret it later.

Thanks.  I've tried the hidden input and the session to no avail.  Still does this:

 

20080709.100331

 

and then:

 

20080709.100335

 

Very odd.  Maybe too many moving parts.  Here's what I have so far.  The sflow function calls cURL to a web site.

 

if (!isset($_SESSION['tstamp'])) {
session_start();
   $_SESSION['tstamp'] = date('Ymd.His');
}

$ordnum = $_POST['ordernumber'];
$serialnum = $_POST['serialnumber'];

if (isset($ordnum)) {

$flowname = "SRP";
$fresult = sflow($flowname);
}

 

Then I have a AJAX reading the file:

 

var fileName = myWan + '\-' + myDate; 

function status()
{
var myAjax = new Ajax.PeriodicalUpdater('status', 'readfile.php?fname=' + fileName, {method: 'get', frequency: 5.0, decay: 1});
}

Replace:

 

if (!isset($_SESSION['tstamp'])) {
session_start();
   $_SESSION['tstamp'] = date('Ymd.His');
}

 

with:

 

session_start();
if (!isset($_SESSION['tstamp'])) {
   $_SESSION['tstamp'] = date('Ymd.His');
}

 

You need to start the session before trying to see if the timestamp is set in it.

 

Robin

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.