troubleshooting Question

How to only call function once

Avatar of dynamicOne
dynamicOne asked on
PHP
14 Comments1 Solution613 ViewsLast Modified:
In the code below. I have a function named notification(). While the condition is true I only what to call it once. I have set_time_limit(1) set to 1, but I continue to get multiple emails. The echo statement needs to keep working indefinitely, but the function should only be called once. How can I accomplish this?

<html>
<head>
<title>Switch</title>
</head>
<body>

<div style="text-align: center">
<?php

require '/usr/share/php/Mail.php';

function notification (){
$mail = new Mail();

$headers = array(
      'From' => 'noreply@localhost',
      'Subject' => 'Garage Door'
      
);

$auth = array(
      'auth'             =>  true,
      'host'             =>  'smtp.gmail.com',
      'username'         =>  'redacted+gmail.com',
      'password'         =>  'redacted'
      
);

$smtp = $mail->factory('smtp', $auth);
$smtp->send('redacted@txt.att.net', $headers, 'The garage door has been activated.');

}
?>

<?php

$switch = exec('/usr/local/bin/gpio read 1');

if ($switch == "1"){
            echo "<img src='garage-door-open.jpeg' width='497px' height='448px'/>";
            notification();
            set_time_limit(1);      
}
if ($switch == "0"){
            echo "<img src='garage-door-close.jpeg' width='497px' height='448px'/>";
      }

?>
</div>
</body>
</html> 

(Edited to use the code snippet for the code ~Ray)
(Edited to redact the code snippet ~Ray)
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 14 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 14 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros