Link to home
Start Free TrialLog in
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)
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

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
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Avatar of gr8gonzo
gr8gonzo
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of dynamicOne
dynamicOne

ASKER

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.
...  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?
@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.
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.
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!
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.
The more I think about this I think I am going to have run this directly on the Pi.
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?
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
@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.