making a webserver that will run all the time

when I use the example of web server
it's only working when I connect to it ,
I want to make it work even when no one is connected to it.

now it will only show me data when I connect to it using the ethernet cable (my code is to count how many people move in front of my PIR)
I want it to always run ,and when I connect it will continue normally.

what do I need to change?

#include <SPI.h>
#include <Ethernet.h>
int david;
int directtion_switch=7;//????? ????? ????? 
byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };//mac address of the device
IPAddress ip(10,0,0,25);// ip address of the device 
EthernetServer server(80);


void setup()
{//start of setup
  Serial.begin(9600);
  delay (1000);
  pinMode(9,INPUT);
  pinMode(8,INPUT);
  pinMode(directtion_switch,INPUT);
  Ethernet.begin(mac, ip);
  delay(1000);
  server.begin();
  Serial.print("server is at ");
  Serial.println(Ethernet.localIP());
  Serial.println("please waite while run startup...");
  for (int i=0;i<10;i++)
  {
    Serial.print("**");
    delay (1000);
  }
  Serial.println("");
  Serial.println("Thank you");
  Serial.println("Sensor is now active");
  
}//end of setup


void loop() {
  // listen for incoming clients
  EthernetClient client = server.available();
  if (client) {
    Serial.println("new client");
    // an http request ends with a blank line
    boolean currentLineIsBlank = true;
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();
        Serial.write(c);
        // if you've gotten to the end of the line (received a newline
        // character) and the line is blank, the http request has ended,
        // so you can send a reply
        if (c == '\n' && currentLineIsBlank) {
          // send a standard http response header
          client.println("HTTP/1.1 200 OK");
          client.println("Content-Type: text/html");
          client.println("Refresh: 1");  // refresh the page automatically every 1 sec
          client.println();
          client.println("<!DOCTYPE HTML>");
          client.println("<html>");
          
          //******************************************* the part of the code*******************//////////
          
          david=work();
          int Possition_switch=poss();
          Serial.println(Possition_switch);
          if  (Possition_switch==1)
          {
            client.println("The entry is from left side  ");
            client.print(">>>>>>>>>>>>>>>>>>>>>>>>>>>>");
            client.println("
"); 

          }
          else if (Possition_switch==0)
          {
            client.println("The entry is from right side  ");
            client.print("<<<<<<<<<<<<<<<<<<<<<<<<<<<<");
            client.println("
"); 
          }

          if (david==0)
          {
            client.print("NO MOVE  ");
            client.println("
");       
          }
          else 
          {
            if (david==1)
            {
              client.print("move right-->left ");
              client.println("
"); 
            }
            else 
            {
              if (david==2)
              {
                client.print("move left-->right ");
                client.println("
");
              }
            }
          }
          
          //**************** end part of the code ***************
          
          
          client.println("</html>");
          break;
        }
        if (c == '\n') {
          // you're starting a new line
          currentLineIsBlank = true;
        } 
        else if (c != '\r') {
          // you've gotten a character on the current line
          currentLineIsBlank = false;
        }
      }
    }
    // give the web browser time to receive the data
    delay(1);
    // close the connection:
    client.stop();
    Serial.println("client disonnected");
  }
}

int work()//   ???? ???? ????? ???? ???? 
{
  int PIR;
  int right=digitalRead(9);
  int left=digitalRead(8);
  if ((left==HIGH)&(right==HIGH))
  {
    Serial.println("no move ");
    PIR=0;
  }

  else 
  {//start of move 
    if ((left==LOW)&(right==HIGH))
    {//fist move 1
      Serial.println("move1");
      PIR=1;
    }//end move1
    else 
    {
      if ((left==HIGH)&&(right==LOW))
      {
        Serial.println("move2");
        PIR=2;
      }
    }
  }//end of move



  return PIR; 
  
}//end work

int poss()// ????? ????? ??? ??? 
{
  int poss_temp;
  if (digitalRead(directtion_switch==HIGH))
  {
    poss_temp=0;
    Serial.println("entry from right side -->>>> ");     
  }
  else if (digitalRead(directtion_switch==LOW))
  {
    poss_temp=1;
    Serial.println("entry from left side -----<<< ");
  }

  return poss_temp;

}//end of poss

when I use the example of web server
it's only working when I connect to it ,
I want to make it work even when no one is connected to it.

I'm not sure what you mean, but most "work" when there are no clients. Here is my basic server code.
http://playground.arduino.cc/Code/WebServerST
Here is one of my posts with a server that does a few things like dns and ntp, and reports the status of the sockets while doing it.
http://forum.arduino.cc/index.php?topic=167184.msg1364572#msg1364572

let me explain what I want to do -

I have a PIR sensor and the code is counting how many time someone moved in front of it.

my problem is that - the program begin to work only when I connect to the ip at the first time .(
so if I don't enter the site(10.0.0.25) it will not begin running the code and I won't know how many people have moved,
(even when I don continually ping to the IP - 10.0.0.25 )

You should have taken a look at that second link I posted for you. It does that.

void loop() {
  // check for movement here

  // now listen for incoming clients
  EthernetClient client = server.available();
  if (client) {
// rest of your server code

if I understand you correctly
I need to change the order of my code?
at the start of the loop to do the code I want and what\how to count
and in the end of the loop "publish " it?

Check movement every iteration of loop(). When you get a client, then publish your movement results.

edit: In other words, yes. The sketch in that second link checks the time (millis), and if it is time for a dns and ntp update, it does a dns request for Google, then an NTP request for the time, then checks for a server client.

O.k. ,
now it's good - thanks for this

now I have another question - when I close the serial monitor and then open it again - the counter reset to 0 .
it there a way to keep the value ? until the next reset of the device or until I disconnect it from power?

Thank ,

The usb port triggers a reset when you connect to it. There are two solutions.

  1. Install a capacitor on the reset line from the usb chip to the Arduino.
  2. Cut the "RESET EN" trace on the Arduino.

I don't use #1. I get the feeling that may overload the reset line when it tries to pull that cap low.

edit: If you cut the "RESET EN" trace, you must press the reset button to upload new code to the Arduino. You can enable it again by a small solder jumper across the "RESET EN" pads.

In the past I've defeated the arduino reset by putting a 100 ohm resistor between the reset pin and +5v pin. Easy to remove/replace. YMMV

I will try it - and tell if its work

Thank (for the 2 suggestions )