Detect Client Disconnect from Webpage

I have a ESP8266-12E and I’m needing a basic web server with a button to activate a pin that I will have a switch on to open/close a gate. Got the page up with a button that activates the default led pin for now. I’m using this from a phone, and the problem I’m having is after the button is pressed, if I hit the back button it connects as a new client, and if I press back again to close out of the browser the client stays connected and I have to reset the controller. I put in a controller reset for now as a work around, but I have a feeling it will get hung up again in just the right setting and won’t be able to control it when needed. I’m sure it is a simple fix, but just getting started as you can probably tell from my code. I was able to reverse engineer some examples I found online and piece together what I have. Also if there is anyway that after x number of seconds after displaying “Gate Button Pressed” it can go back to the initial page would be nice too. Thanks, and any help is appreciated.

#include <SPI.h>
#include <ESP8266WiFi.h>

const char* ssid = “SSID”;
const char* password = “Password”;
// Create an instance of the server
// specify the port to listen on as an argument
WiFiServer server(8080);

void setup() {
Serial.begin(115200);
delay(10);

// prepare LED_BUILTIN pin
pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(LED_BUILTIN, 0);

// Connect to WiFi network
Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
IPAddress ip(192,168,1,176);
IPAddress gateway(192,168,1,254);
IPAddress subnet(255,255,255,0);
WiFi.begin(ssid, password);
WiFi.config(ip,gateway,subnet);

while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println(“WiFi connected”);

// Start the server
server.begin();
Serial.println(“Server started”);

// Print the IP address
Serial.println(WiFi.localIP());
}
void loop() {
//Declare reset function @ address 0
void(* resetFunc) (void) = 0;

// Check if a client has connected
WiFiClient client = server.available();
if (!client) {
return;
}

// Wait until the client sends some data
Serial.println(“new client”);
while(!client.available()){
delay(1);
}

// Read the first line of the request
String req = client.readStringUntil(’\r’);
Serial.println(req);
client.flush();
String IP = WiFi.localIP().toString();
// client.print(“HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n\r\n\r\nGPIO is now \n”);
client.print("");
client.print("<FORM action=/gate"">");
client.print("<input type="“submit”" value="“Gate”" style="“height:300px;width:300px”">");
client.print("");
client.print("");
client.flush();
//client.stop();
// Match the request
int val;
if (req.indexOf("/gate")!=-1)
val=1;
else {
Serial.println(“invalid request”);
client.stop();
return;
}

// Set LED_BUILTIN On sleep 500 ms then LED_BUILTIN off
digitalWrite(LED_BUILTIN, HIGH);
delay(500);
digitalWrite(LED_BUILTIN, LOW);
client.flush();

//Prepare the response
client.print("\r\nGate Button Pressed! \n");
delay(1);
Serial.println(“Client disonnected”);
delay(5000);
resetFunc(); //call reset
}

HTTP cookies were invented to solve just this problem.

PS. If you use Tools->Autoformat and the code tags (</>) it makes your code heaps easier to read

Like this

I looked at HTTP Cookies as you suggested, but don’t understand how to apply with code or make use of them. Could you provide an example?