PHP with Raspberry Pi

I am controlling my garage door using a Raspberry Pi. I want to add a magnetic switch to tell if the door is open or closed.

Any idea where to start?

Here is what I have so far:

<!DOCTYPE html>

<html lang="en">
  <meta name="generator" content=
  "HTML Tidy for Linux (vers 6 November 2007), see">
  <meta charset="utf-8">

  <title>Garage Remote</title>
  <!--Begin code to open and close garage door-->

  <script type="text/javascript">

  window.onload = function(){

        buttonOff = document.getElementById('submitPressed');
        buttonOff.onClick = relayOff;

  function relayOff(){

        hidden = document.getElementById("total");
        hidden.value = "pressed";

        form = document.getElementById("relay");
        form.method = "GET";
        form.action = "remote.php";
  </script><!--End code to open and close garage door-->
  <!--Begin code to color button when pressed-->

  <script type="text/javascript">
function setColor(btn,color){
    var property=document.getElementById(btn);
   if (window.getComputedStyle(property).backgroundColor == 'rgb(244, 113, 33)') {;
    else { = "#2159F4";
  </script><!--End code to color button when pressed-->

  <form id="relay" action="remote.php" method="get">
    <input type="button" id="submitPressed" style=
    "border:1px solid #000; font-size:80px;" value="Open/Close"
    <input type="hidden" name="total" id="total" value="">
  </form><!--Begin code to open and close garage door-->

          exec("gpio -g mode 17 out");
          $state = $_GET["total"];

          if ($state == "off"){
                  exec("gpio -g write 17 1");
                  exec("gpio -g write 17 0");

  ?><!--End code to open and close garage door-->
  <!--Insert code for magnetic switch here.-->
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Dave BaldwinFixer of ProblemsCommented:
Where is the web server that is running PHP?
dynamicOneAuthor Commented:
It is running on the Pi.
Dave BaldwinFixer of ProblemsCommented:
Interesting, didn't realize that Raspberry Pi could run all that.  I have never used PHP to control hardware.  I suggest you click on "Request Attention" above to get others to look at your question.
You're just overengineering the heck out of this.  No need for a web server unless you are doing some fancy touch screen served by the pi as the human interface.

The right way to do this is by creating a shell script and running it as a daemon.

the /etc/init.d has some skeleton scripts to assist turning a working shell script into a service that autostarts, but the first problem is to write the shell script.  You don't have nearly enough information here on what bytes need to be read/written, so way to attack the problem is to create something like

where it returns 1 if opened, 0 if closed...

IsOpen=`/bin/gpio -g mode 17 out`
if [ $IsOpen == 'INSERTVALUEHERE' ] ; then
  echo 1
  echo 0

then split out the other code so you can get them to work with command-line.
greetings  dynamicOne, , not sure I can help you with this, but I wanted to clarify to you this may have very little to do with and WEB programming like PHP, it is true that it uses a web page to do the operational GUI, but the PHP code used is like -
      exec("gpio -g write 17 1");

this has NO PHP OPERATIONS, it uses the LINUX Operating System to use the linux software program of  gpio to use the Rasberry hardware to connect to the door opener via the rasberry gpio-connectors or the USB and operate it, but there is nothing that the PHP code does, except send text "shell script" commands to the linux OS.
I would guess that looking to a more specific info source for Raspberry Pi software controllers like  gpio  may get you informed help.
Not sure, but this video shows you how to set up an electrical switch to the gpio -

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Eddie ShipmanAll-around developerCommented:
On another note, please use the code tags when you post code in your posts as it makes it tons easier to read.
nociSoftware EngineerCommented:
start with 2 reed relais on the garagedoor. 1 for open signal, one for closed.

Then connect them to the rasp pi using some buffer. (Gertboard f.e.).
Then you can read the state of the relais.

Using 1 switch you get the problem that "door open"  != not "door closed"
i.e. the door can be half open.
There is a transition between open & closed. and you need to detect the stable conditions.
Reading the gpio state of both switches can indicate if the door is open/closed or in between (no signal) ...
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.