Hi, how I can use PHP with Ethernet shield?
I've read a cookie with ethernet shield, but is it possible?
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println();
client.println("<html><head>");
//refresh per le temperature
client.print("<meta http-equiv='refresh' content='2'>");
client.println("<title>Arduino Home</title></head><body>");
client.println("<?php if (!isset($_COOKIE['nome_utente'])){");
client.println(" print('Pagina scaduta
'); ");
client.println(" print('Se vuoi ritornare alla pagina di login clicca '); ");
client.println(" print('<a href=""http://192.168.0.10/tesina/index.php"">qui</a>');");
client.println(" die(); ?>");
client.println("<table border=""2"" width=""100%"" height=""100%""><tr><td align=""center"">");
etc....
You can't execute PHP on your arduino and serve it up over ethernet the way you can with a regular webserver; however, if what you are asking is can you call a php cgi script on another webserver from your ethernet equipped Arduino the answer is yes. Here is an example I have that sends some sensor data to my web server (Apache).
#include <Time.h>
#include <Wire.h>
#include <SPI.h>
#include <Ethernet.h>
#include <Adafruit_BMP085.h>
#include "DHT.h"
#include "Adafruit_MCP9808.h"
#include <MCP79412RTC.h>
#include <SFE_TSL2561.h>
#define DHTTYPE DHT22
const int DHTPIN=5;
const int ssPin = 53;
const int http_port = 80;
byte mac[] = { 0xAC, 0x03, 0xFB, 0xB4, 0xEE, 0x00 };
char memoria[] = "192.168.0.100";
char location[] = "INDOOR";
//char location[] = "OUTDOOR";
unsigned char gain; // Gain setting, 0 = X1, 1 = X16;
unsigned int ms; // Integration ("shutter") time in milliseconds
static long lastPrint=0;
IPAddress ip(192,168,0,199);
IPAddress myDns(192,168,0,1);
DHT dht(DHTPIN, DHTTYPE);
Adafruit_BMP085 bmp;
Adafruit_MCP9808 tempsensor = Adafruit_MCP9808();
SFE_TSL2561 light;
EthernetClient client;
void httpRequest(char *query) {
// if there's a successful connection:
if (client.connect(memoria, http_port)) {
// send the HTTP GET request:
client.println(query);
Serial.println(query);
client.println("Host: 192.168.0.100");
client.println("User-Agent: arduino");
client.println("Connection: close");
client.println();
delay(50);
client.stop();
}
else {
// if you couldn't make a connection:
Serial.println("connection failed");
Serial.println("disconnecting.");
client.stop();
}
}
void setup() {
Serial.begin(115200);
delay(1000);
// start the Ethernet connection using a fixed IP address and DNS server:
Ethernet.begin(mac, ip, myDns);
setSyncProvider(RTC.get);
if (timeStatus() != timeSet)
Serial.println("Unable to sync with the RTC");
if (!tempsensor.begin()) {
Serial.println("Couldn't find MCP9808!");
while (1);
}
if (!bmp.begin()) {
Serial.println("Could not find a valid BMP085 sensor, check wiring!");
while (1) {}
}
dht.begin();
light.begin();
gain = 0;
unsigned char time = 2;
light.setTiming(gain,time,ms);
light.setPowerUp();
}
void loop()
{
if ((millis() - lastPrint) > 30000L)
{
lastPrint = millis();
char buffer[250];
long dht_h = dht.readHumidity() * 10;
long mcp_c = tempsensor.readTempC() * 100;
long mcp_f = ((float) (tempsensor.readTempC() * 9.0 / 5.0 + 32.0))*100;
long bmp_p = ((bmp.readPressure() / 1000.0) * 0.3)*100;
unsigned int data0;
unsigned int data1;
light.getData(data0, data1);
double lux;
boolean good = light.getLux(gain,ms,data0,data1,lux);
long llux = lux*100;
sprintf(buffer, "GET /apps/add_env.php?rec_date=%04d-%02d-%02d%c20%02d:%02d:%02d&location=%s&temperature=%ld&pressure=%ld&humidity=%ld&illumination=%ld HTTP/1.1",
year(), month(), day(), '%', hour(), minute(), second(), location, mcp_f, bmp_p, dht_h, llux);
Serial.println(buffer);
httpRequest(buffer);
}
}