Problem with Webserver

The code's goal is to trigger a relay on a magnetic switch sequence. While showing the status of each switch to the local web server. Everything works great except for one problem. Eventually the webserver crashes and the arduino has to be reset. I dont know what causes this. Thoughts

It is most likely your use of the String class. It allocates memory as needed but Arduino boards have a very limited amount of memory and don't reclaim it after use. Better to use C strings (char arrays terminated by nul)

Wouldn't the memory for the String be reclaimed after it goes out of scope? Otherwise I can just declare it globally and reuse the same variable.

The String class uses malloc() and Arduinos do not have any garbage collection. You can mitigate this somewhat by declaring a global variable and pre-allocating a large chunk of memory for it, but still, it is better to bite the bullet and learn how to use strings (lower case 's') vs. Strings