help:HTML/PHP how to add login authentication to my code

please anyone help me with adding a authentication like username and password before accessing the controls :confused:

see my attachment *.ino file

benarduino_v1.ino (14.9 KB)

benarduino:
please anyone help me with adding a authentication like username and password before accessing the controls

Here I had posted some sample code doing "HTTP Basic Authentication" which is complient to RFC 2617, so it works with any webbrowser:

Username and password is not encrypted, but only BASE64 encoded, so the level of security is much less than with SSL/https encryption. But at least, Arduino can do it easily.

thank you for the help :wink: but i don't know where to paste that in my code :cry:

benarduino:
thank you for the help :wink: but i don't know where to paste that in my code :cry:

Did you test my webserver code?
Does the username/password login in my example work as you want it?
Do you know how to configure your own username and your own password as you like it?

I'd have to look it up first before I can tell whether it would be easier to include your programming logic into my example code, or if it would be easier the other way round and to include the authentication code in your code.

im having a lot of error when i tried to merge your code to mine :frowning: , i click verify on your code and it has no errors at all. it seems pasting it directly sections by sections giving me the problem and i don't know what to delete or what to leave as is. .it produce an error with ethernet.h and with expected '}' thus adding the '}' won't help, i know a little bit about programming but i can't understand long loop statements. . btw sir can you help me with getting a status on pin 2? it's a relay and i don't know if it is on or off state. . God bless you :wink: thanks

benarduino:
im having a lot of error when i tried to merge your code to mine :frowning:

I’ve tried to integrate HTTP Basic Authentication to the HTML page you are sending.

Can you make this run?

#include <SPI.h>
#include <Ethernet.h>
#include <DHT.h>
#define DHTPIN 2     // what pin we're connected to
#define DHTTYPE DHT11   // DHT 11
DHT dht(DHTPIN, DHTTYPE);
float temperature, humidity;

// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = { 
  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192, 168, 1, 178);

// Initialize the Ethernet server library
// with the IP address and port you want to use 
// (port 80 is default for HTTP):
EthernetServer server(80);

void setup() {
// Open serial communications and wait for port to open:
  Serial.begin(9600);
   while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }

  // start the Ethernet connection and the server:
  Ethernet.begin(mac, ip);
  server.begin();
  Serial.print("server is at ");
  Serial.println(Ethernet.localIP());
}


void SendOKpage(EthernetClient &client)
{
           client.println("HTTP/1.1 200 OK"); //send new page
           client.println("Content-Type: text/html");
   client.println("Refresh: 5");  // refresh the page automatically every 5 sec
          client.println();
           client.println("<HTML>");
           client.println("<HEAD>");
           client.println("<meta name='apple-mobile-web-app-capable' content='yes' />");
           client.println("<meta name='apple-mobile-web-app-status-bar-style' content='black-translucent' />");
           client.println("<TITLE>Home Automation</TITLE>");
           client.println("</HEAD><center>");
           client.println("<BODY>");
           client.println("<H1>Benarduino ver 1</H1>");
           client.println("<hr />");
           client.println("
");  
           client.println("<H2>Arduino with Ethernet Shield</H2>");
           client.println("
");  
           client.println("<H1>");
           client.println("<a href=\"/?button1on\"\"><font color='green'>On CO1</a></font>");
            client.println("             ");
           client.println("<a href=\"/?button1off\"\"><font color='red'>Off CO1</font></a>
");   
           client.println("</H1>");
           client.println("
"); 
           // output the value of the DHT-11
         client.println("<H2>");
            client.print("<font color='blue'>Humidity: ");
            client.println("</H2>");
            client.println("<p />");
            client.println("<H1>");
            client.print(humidity);
            client.print("</font>");
            client.print(" %\t");
            client.println("</H1>");
            client.println("<p />"); 
            client.println("<H2>");
            client.print("Temperature: ");
            client.println("</H2>");
            client.println("<H1>");
            
            client.print(temperature*1.8+32);
 ¬† ¬† ¬† ¬† ¬† ¬†client.println(" ¬į");
            client.println("F");
            
            client.println("
"); 
            client.print(temperature);
 ¬† ¬† ¬† ¬† ¬† ¬†client.println(" ¬į");
            client.println("C");
            client.println("
"); 
            client.println("</H1>");    
           client.println("
"); 
           client.println("</center></BODY>");
           client.println("</HTML>");
}


void SendAuthentificationpage(EthernetClient &client)
{
          client.println("HTTP/1.1 401 Authorization Required");
          client.println("WWW-Authenticate: Basic realm=\"Secure Area\"");
          client.println("Content-Type: text/html");
          client.println("Connnection: close");
          client.println();
          client.println("<!DOCTYPE HTML>");
          client.println("<HTML>  <HEAD>   <TITLE>Error</TITLE>");
          client.println(" </HEAD> <BODY><H1>401 Unauthorized.</H1></BODY> </HTML>");
}

char linebuf[80];
int charcount=0;
boolean authentificated=false;

unsigned long lastDHTreadTime;
void loop() {
  if (millis()-lastDHTreadTime>=5000) // read sensor each 5 seconds
  {
    lastDHTreadTime=millis();
    humidity = dht.readHumidity();
    temperature = dht.readTemperature();
    if (isnan(temperature) || isnan(humidity)) {
      Serial.println("Failed to read from DHT");
    } else {
      Serial.print("Humidity: ");
      Serial.print(humidity);
      Serial.print(" %\t");
      Serial.print("Temperature: ");
      Serial.print(temperature);
      Serial.println(" *C");
    }
  }
  
  // listen for incoming clients
  EthernetClient client = server.available();
  if (client) {
    Serial.println("new client");
    memset(linebuf,0,sizeof(linebuf));
    charcount=0;
    authentificated=false;
    // an http request ends with a blank line
    boolean currentLineIsBlank = true;
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();
        linebuf[charcount]=c;
        if (charcount<sizeof(linebuf)-1) charcount++;
        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) {
          if (authentificated)
            SendOKpage(client);
          else
            SendAuthentificationpage(client);  
          break;
        }
        if (c == '\n') {
          // you're starting a new line
          currentLineIsBlank = true;
          if (strstr(linebuf,"Authorization: Basic")>0 && strstr(linebuf,"c2F0YXJ1bjpwYXNzd29yZA==")>0)
            authentificated=true;
          memset(linebuf,0,sizeof(linebuf));
          charcount=0;
        } 
        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");
  }
}

I think then you’d have to work something out for the switching logic and have a different reactions on different requests.