TripleDES Posted July 9, 2008 Share Posted July 9, 2008 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? Quote Link to comment https://forums.phpfreaks.com/topic/113946-timestamp-setting-itself-on-load-and-again-on-submit/ Share on other sites More sharing options...
lemmin Posted July 9, 2008 Share Posted July 9, 2008 Put the timestamp in a session: if (!isset($_SESSION['timestamp'])) $_SESSION['timestamp'] = date('Ymd.His'); Quote Link to comment https://forums.phpfreaks.com/topic/113946-timestamp-setting-itself-on-load-and-again-on-submit/#findComment-585583 Share on other sites More sharing options...
rmbarnes82 Posted July 9, 2008 Share Posted July 9, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/113946-timestamp-setting-itself-on-load-and-again-on-submit/#findComment-585593 Share on other sites More sharing options...
revraz Posted July 9, 2008 Share Posted July 9, 2008 Put the timestamp in a session: if (!isset($_SESSION['timestamp'])) $_SESSION['timestamp'] = date('Ymd.His'); And if you don't use sessions, put it in a IF SUBMIT clause. Quote Link to comment https://forums.phpfreaks.com/topic/113946-timestamp-setting-itself-on-load-and-again-on-submit/#findComment-585595 Share on other sites More sharing options...
corbin Posted July 9, 2008 Share Posted July 9, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/113946-timestamp-setting-itself-on-load-and-again-on-submit/#findComment-585597 Share on other sites More sharing options...
TripleDES Posted July 9, 2008 Author Share Posted July 9, 2008 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}); } Quote Link to comment https://forums.phpfreaks.com/topic/113946-timestamp-setting-itself-on-load-and-again-on-submit/#findComment-585651 Share on other sites More sharing options...
rmbarnes82 Posted July 9, 2008 Share Posted July 9, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/113946-timestamp-setting-itself-on-load-and-again-on-submit/#findComment-585695 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.