Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

PHP AntiSpam Timer into the Email

Featured Replies

I'm trying to implement an anti-bot timer on my contact form as suggested by @BrowserBugs here

But I'd also like to include the time in the email so I can check what I'm doing and because it would be useful. 

If I simply put the posted value 'sent' into a variable and include that in the email I get the complete php code from the form input not the date-time. And if I use strtotime on 'sent' first I don't get anything in the resulting variable.

What am I missing?

TIA

Paul

Ahoy. If you could post the form i can take a look. If you just want to know the date and time the email was sent use;

$date_sent = date('Y-m-d H:i:s');

this gets the current server date and time at the point of process. If you want a different format look at date format.

  • Author

Thanks, my php is not great so apologies if this is obvious. What I was specifically trying to do was get the value of 'sent' from your 

<input name="sent" type="hidden" value="<?php echo date('Y-m-d H:i:s');?>">

but when I do

$submitted = $_POST['sent'];

the variable $submitted contains

<?php echo date('Y-m-d H:i:s');?>

not the date string I was expecting. Which makes me think I'm missing something. I tried combinations of strtotime and dateformat but can't get it to work and although my php skills may be letting me down I wondered again if I'm missing something more fundamental.

Ah! The $submitted is already a date time value, e.g. 2018-03-29 10:28:16. To change it's format to 23/03/2018 @ 10:28 would be;

<?php echo date("d/m/Y @ H:i", strtotime($submitted)); ?>

What strtotime does is "parses an English textual datetime into a Unix timestamp (the number of seconds since January 1 1970 00:00:00 GMT)" (source). With date the first bit is the output format, the second value is the time which if left out defaults to now, but if you give it "2018-03-29 10:28:16" it turns that into the unix timestamp then turns it back into the format you wanted.

  • Author

Yes, I was expecting $submitted to be a date but currently it contains the whole php code verbatim from the input section of the form and not 2018-03-29 10:28:16 as I was expecting. 

Thanks for clarification on strtotime - that explains why my time check doesn't work because it's not getting a date as input but the string intended to generate the date.

Do I need to put

$submitted = $_POST['sent'];

into a

if($_SERVER['REQUEST_METHOD']=='POST')
{
$submitted=$_POST('sent');
}

construct?

1 hour ago, DonkeyWorx said:

Yes, I was expecting $submitted to be a date but currently it contains the whole php code verbatim from the input section of the form and not 2018-03-29 10:28:16 as I was expecting. 

Sounds like something is processing all fields, what else is the code doing? A quick test is a clean sheet, try;

<?php
$msg = "";
if($_SERVER['REQUEST_METHOD']=="POST") {
	$msg .= "I have posted";
	$date_sent = trim($_POST['sent']);
	if($date_sent!="") { $msg .= " at " . $date_sent . " also known as " . date("jS F, Y", strtotime($date_sent)) . " or " . date("d/m/Y @ H:i", strtotime($date_sent)); }
}
?>
<!DOCTYPE html>
<html lang="en">
  <head>
  <title>Test Post</title>
  </head>
  <body>
<?php if($msg!="") { echo "<p>" . $msg . "</p>"; } ?>
    <form method="post">
      <input type="hidden" name="sent" value="<?php echo date('Y-m-d H:i:s'); ?>">
      <input name="submit" type="submit" value="1">
    </form>
  </body>
</html>

 

  • Author

Many thanks - that successfully highlighted my stupidity. :blush1:  The form was in an html page not a php file so the php wasn't executing. Knew I was missing something obvious. 

Create an account or sign in to comment

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.