esp8266 PIR trigger help

My goal is to have the relay turn on while the sensor is HIGH/ detecting motion but only if the button on the webpage is set to “on” (When R1 == “on”)
I cant figure out why its not working that way. I dont usually have more than the setup and loop voids so I think thats it

/*********
RedSpade 
*********/
#include <ESP8266WiFi.h>

const char* ssid     = "WIRELESS NAME";
const char* password = "PASSWORD";
WiFiServer server(80);
String header;

// Auxiliar variables to store the current output state
String R1State = "off";
String R2State = "off";
String R3State = "off";
String R4State = "off";

// Assign output variables to GPIO pins
const int R1 = D1;
const int R2 = D2;
const int R3 = D3;
const int R4 = D4;
const int S1 = D5;

void setup() {
  Serial.begin(115200);
  pinMode(R1, OUTPUT);
  pinMode(R2, OUTPUT);
  pinMode(R3, OUTPUT);
  pinMode(R4, OUTPUT);
  pinMode(S1, INPUT);
  digitalWrite(R1, LOW);
  digitalWrite(R2, LOW);
  digitalWrite(R3, LOW);
  digitalWrite(R4, LOW);
  // Connect to Wi-Fi network with SSID and password
  Serial.print("Connecting to ");
  Serial.println(ssid);
  WiFi.hostname("Barrel1");
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.println("WiFi connected.");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
  Serial.println(WiFi.hostname());
  server.begin();
}

void loop() {
  web();
}

void web(){
  WiFiClient client = server.available();
  if (client) {
    Serial.println("New Client.");
    String currentLine = "";
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();
        Serial.write(c);
        header += c;
        if (c == '\n') {
          if (currentLine.length() == 0) {
            client.println("HTTP/1.1 200 OK");
            client.println("Content-type:text/html");
            client.println("Connection: close");
            client.println();
            
            // turns the GPIOs on and off
            if (header.indexOf("GET /Skeleton/on") >= 0) {
              Serial.println("Skeleton on");
              R1State = "on";
              sensor();
              // digitalWrite(R1, HIGH);
            } else if (header.indexOf("GET /Skeleton/off") >= 0) {
              Serial.println("Skeleton off");
              R1State = "off";
              sensor();
              // digitalWrite(R1, LOW);
            } else if (header.indexOf("GET /Siren/on") >= 0) {
              Serial.println("Siren on");
              R2State = "on";
              sensor();
              // digitalWrite(R2, HIGH);
            } else if (header.indexOf("GET /Siren/off") >= 0) {
              Serial.println("Siren off");
              R2State = "off";
              sensor();
              // digitalWrite(R2, LOW);
            } else if (header.indexOf("GET /Lights/on") >= 0) {
              Serial.println("Siren on");
              R3State = "on";
              sensor();
              // digitalWrite(R3, HIGH);
            } else if (header.indexOf("GET /Lights/off") >= 0) {
              Serial.println("Siren off");
              R3State = "off";
              sensor();
              // digitalWrite(R3, LOW);
            } else if (header.indexOf("GET /Extra/on") >= 0) {
              Serial.println("Siren on");
              R4State = "on";
              sensor();
              // digitalWrite(R4, HIGH);
            } else if (header.indexOf("GET /Extra/off") >= 0) {
              Serial.println("Siren off");
              R4State = "off";
              sensor();
              // digitalWrite(R4, LOW);
            }
            
            // Display the HTML web page
            //html and css
            client.println("<!DOCTYPE html><html>");
            client.println("<head>");
            client.println("<meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">");
            client.println("<link rel=\"icon\" href=\"data:,\">");
            client.println("<style>html { font-family: OCR A Std; font-size: 40px; color: #42b3f4; display: inline-block; margin: 0px auto; text-align: center; display: inline-block;}");
            client.println(".btn-group button { background-color: #0069b5; border: 1px #002642; color: white; padding: 18px 35px; cursor: pointer; float: center; font-family: Courier New;font-size: 17px;}");
            client.println(".btn-group button:hover { background-color: #002642;}</style></head>");
            client.println("<body style=\"background-color: #61686d;\"><h1>DEVICE NAME</h1><h2 style=\"font-size: 30px;\">Barrel #1</h2>");
            client.println("<div class=\"btn-group column\">");
            // Display current state of Skeleton
            if (R1State=="on") {
              client.println("<a href=\"/Skeleton/off\"><button class=\"btn-group\" style=\"background-color: #63799b\">Turn Skeleton On</button></a>");
            } else {
              client.println("<p><a href=\"/Skeleton/on\"><button class=\"btn-group\">Turn Skeleton Off</button></a>");
            } 
               
            // Display current state of Siren
            if (R2State=="on") {
              client.println("<a href=\"/Siren/off\"><button class=\"btn-group\" style=\"background-color: #63799b\">Turn Siren On</button></a>");
            } else {
              client.println("<a href=\"/Siren/on\"><button class=\"btn-group\">Turn Siren Off</button></a>");
            }

            // Display current state of Lights
            if (R3State=="on") {
              client.println("<a href=\"/Lights/off\"><button class=\"btn-group\" style=\"background-color: #63799b\">Turn Lights On</button></a>");
            } else {
              client.println("<a href=\"/Lights/on\"><button class=\"btn-group\">Turn Lights Off</button></a>");
            }

            // Display current state of Extra
            if (R4State=="on") {
              client.println("<a href=\"/Extra/off\"><button class=\"btn-group\" style=\"background-color: #63799b\">Turn Extra On</button></a>");
            } else {
              client.println("<a href=\"/Extra/on\"><button class=\"btn-group\">Turn Extra Off</button></a>");
            }
            client.println("</div></body></html>");
            
            // The HTTP response ends with another blank line
            client.println();
            // Break out of the while loop
            break;
          } else { // if you got a newline, then clear currentLine
            currentLine = "";
          }
        } else if (c != '\r') {  // if you got anything else but a carriage return character,
          currentLine += c;      // add it to the end of the currentLine
        }
      }
    }
    // Clear the header variable
    header = "";
    // Close the connection
    client.stop();
    Serial.println("Client disconnected.");
    Serial.println("");
  }
}

void sensor(){
  int sensorval = digitalRead(S1);
  Serial.println(sensorval);
  if ((sensorval == HIGH) && (R1State == "on")) {
    digitalWrite(R1, HIGH);
    }
    else {
    digitalWrite(R1, LOW);
    }
}

Please provide a detailed description of the problem.

You have some helpful debug output that is printed to Serial Monitor. What does it say?

The Serial console is only telling me the state of the sensor, if Wifi is connected, and if theres a client. I have all the wifi and webpage stuff figured out. I just cant get the sensor to trigger the relay.
I think the code should look something like

int sensorval = digitalRead(S1);
Serial.println(sensorval);
if ((sensorval == HIGH) && (R1State == "on")) {
  digitalWrite(R1, HIGH);
  }
  else {
  digitalWrite(R1, LOW);
  }

S1 is the PIR Sensor
R1 is the 1st relay
R1State is the state of the 1st relay

This is the serial monitor without a client

WiFi connected.
IP address: 
192.168.1.38
Barrel1

This is with a client

WiFi connected.
IP address: 
192.168.1.38
Barrel1
New Client.
Client disconnected.

New Client.
GET / HTTP/1.1
Host: 192.168.1.38
Connection: keep-alive
Cache-Control: max-age=0
Upgrade-Insecure-Requests: 1
DNT: 1
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.86 Safari/537.36
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3
Accept-Encoding: gzip, deflate
Accept-Language: en-US,en;q=0.9,es;q=0.8

Client disconnected.

New Client.

And this is it when I press the "Skeleton Off" button

WiFi connected.
IP address: 
192.168.1.38
Barrel1
New Client.
Client disconnected.

New Client.
GET / HTTP/1.1
Host: 192.168.1.38
Connection: keep-alive
Cache-Control: max-age=0
Upgrade-Insecure-Requests: 1
DNT: 1
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.86 Safari/537.36
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3
Accept-Encoding: gzip, deflate
Accept-Language: en-US,en;q=0.9,es;q=0.8

Client disconnected.

New Client.
Client disconnected.

New Client.
GET /Skeleton/on HTTP/1.1
Host: 192.168.1.38
Connection: keep-alive
Upgrade-Insecure-Requests: 1
DNT: 1
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.86 Safari/537.36
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3
Referer: http://192.168.1.38/
Accept-Encoding: gzip, deflate
Accept-Language: en-US,en;q=0.9,es;q=0.8

Skeleton on
0
Client disconnected.

New Client.

The "0" between "Skeleton on" and "Client disconnected" Is the state of the PIR sensor at the moment
when it detects motion itll change to "1". So I know the sensor is working but the relay still isnt turning off when there is no motion and turning on when there is motion