mercredi 10 juin 2020

match string in if condition and execute the block in php

i've put this code together - working fine.

$a = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http") . "://" .  $_SERVER['HTTP_HOST'];

if ($a == (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http") . "://" . 'site1.com') {
  // code...
 include "site1.html";
}
 elseif ($a == (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http") . "://" . 'site2.com') {
  // code...
 include "site2.html";
}
 else {
  // code...
  echo "hop along, wrong domain";
}

then i tried adding (or - ||) in if condition - not working

 $a = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http") . "://" .  $_SERVER['HTTP_HOST'];

 if ($a == (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http") . "://" . 'site1.com' || 'www.site1.com') {
     // code...
    include "site1.html";
 }
    elseif ($a == (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http") . "://" . 'site2.com' || 'www.site2.com') {
     // code...
    include "site2.html";
 }
    else {
     // code...
     echo "hop along, wrong domain";
 }

what is wrong here?

is there a better a way you would do this to keep this code clean and simple?

please do not give .htaccess examples to achieve this - if there is a possibility, strictly looking for php code

Aucun commentaire:

Enregistrer un commentaire