Avatar of dynamicOne
dynamicOne asked on

How to only call function once

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> 

Open in new window


(Edited to use the code snippet for the code ~Ray)
(Edited to redact the code snippet ~Ray)
PHP

Avatar of undefined
Last Comment
Member_2_248744

8/22/2022 - Mon
Ray Paseur

Are you saying that you want to run this script over and over, but only fire the notification() function once?  If so, you can probably store the signal in the PHP session.
SOLUTION
Ray Paseur

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
ASKER CERTIFIED SOLUTION
Log in to continue reading
Log In
Sign up - Free for 7 days
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
SOLUTION
Log in to continue reading
Log In
Sign up - Free for 7 days
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
ASKER
dynamicOne

Thank you everyone for your input. I have put in a request to the moderator to mark the question private or remove the sensitive information.

I am having trouble with multiple notifications being sent from one script call so I will try what was suggested.
Ray Paseur

...  multiple notifications being sent from one script call ...
That is puzzling to me because the script as written will only send one notification.  But it would potentially send one notification each time it was called.  Any chance there is something in the process that is calling it repeatedly?
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
Member_2_248744

@dynamicOne  - if you can give information about the Lasting time-line for this end of notifications, if you use SESSION, then it will last just until you close the browser, (or some session time limit), if you write a file like I have suggested, this file will be there untill it is deleted, so it will always Stop notifications, even if you close browser or restart Linux-apatche -php.
ASKER
dynamicOne

Ray, yes there is. I am using some jquery to reload the div that is echoing whether the door is open or closed. After I click the button, there is javascript that is appended to the url. If I  refresh the page to check the state of the door with the appended javascript in the url, it will trigger the door.
Ray Paseur

Hmm... You might try the session-related idea.  I'm not sure of what would happen in a repeated-call design, but it's an interesting question!
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
ASKER
dynamicOne

That is what I need to figure out because it will only check the status of the pins when a page is loaded and constantly refreshing. I keep a page up in a browser on the Raspberry Pi so that is no big deal.

Right now I can have an email send to me whenever the app is used to open or close the door. I want to write the code in such a way that an email is generated each time the door is opened or closed.
ASKER
dynamicOne

The more I think about this I think I am going to have run this directly on the Pi.
ASKER
dynamicOne

Ray, quick question. The code you suggested, if I keep a page open in a browser on the Pi to keep the script running, will it send notification each time the conditional statement notices a change from 1 to 0 and vice-versa?
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
Ray Paseur

Here is that script, isolated so that it tests with "mock objects."  You can install it and run it to see the effect of repeated calls to the notification(), or just test it here on my server.
http://www.laprbass.com/RAY_temp_dynamicone.php

To understand what the PHP session is doing here, have a look at this article.
https://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_11909-PHP-Sessions-Simpler-Than-You-May-Think.html
Member_2_248744

@dynamicOne, you have taken what seems to me to be a round-about approach to doing what you so-far have told us, I say so-far, because you have now added important factors as -
"send notification each time the conditional statement notices a change from 1 to 0"
and that this page is refreshed on a timer basis as you say -"constantly refreshing"

you then say -"Right now I can have an email send to me whenever the app is used to open or close the door. I want to write the code in such a way that an email is generated each time the door is opened or closed.", , I can not get any meaning from this, but it sort of says that during all of the "constantly refreshing" page loads, you only want the email when the gpio generated $switch changes from it's binary value. I will tell you the programming Logic I would use for this, First to recognize a Change you will need to record the Value as 1 or 0,  alternately as true or false, some where, in session, or in file, or in a pin value of the gpio, as the Previous value, NOT the current value in the $switch, then you will need to retrieve or read the Previous value, and if NOT EQUAL to the current $switch value, then call the notification email. when the notification is sent CHANGE the recorded Previous value in session-file-gpio. .
But if there are More Factors that you have failed to mention, we can not read your mind.