Hack Forums
[PHP] Different <div> based on which domain is being used. - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Web Development (https://hackforums.net/forumdisplay.php?fid=183)
+---- Thread: [PHP] Different <div> based on which domain is being used. (/showthread.php?tid=5872036)



Different <div> based on which domain is being used. - LinK - Aug 9, 2018

I have about 14 domains pointed to a primary hosting account, some of them are different by name completely.

I have different divs calling different logos based on what nav is called up.

PHP Code

<?php 
        
if ($_GET['nav'] == 'test' || $_GET['nav'] == 'test1' || $_GET['nav'] == 'test2' || $_GET['nav'] == 'test3') {
 
                echo "<div id=\"logo-test\"></div>";
 
        }
 
        else if ($_GET['nav'] == 'testy'|| $_GET['nav'] == 'testy1' || $_GET['nav'] == 'testy2') {
 
                echo "<div id=\"logo-testy\"></div>";
 
        }
 
        else {
 
                echo "<div id=\"logo\"></div>";
         } 

That script works fine, and if the http://domain1.tld/testy and http://domain2.tld/testy loads the proper logo on the designated page no problem.


What I Want to do now, is enable it so if I access a page from

http://domain1.tld <-- It will show the Domain 1 Logo
http://domain2.tld <-- It will show the Domain 2 Logo

I didnt know if there was a GET for HTTP address.

Thank you for your help


RE: Different <div> based on which domain is being used. - Don Vito Corleone - Aug 9, 2018

I haven't personally done this before, but to check what site a user came from you probably need the $_SERVER superglobal.

This is a question very similar to yours that has been answered:

https://stackoverflow.com/questions/10613025/how-can-i-use-serverhttp-referer-to-find-that-user-came-from-google

http://php.net/manual/en/reserved.variables.server.php


RE: Different <div> based on which domain is being used. - Zexture - Aug 10, 2018

The best way is to create a configuration file for each domain and create a variable where you can identify each domain. The most common way is to use $_SERVER['HTTP_HOST'] or $_SERVER['SERVER_NAME'] but I don't recommend that since it can be generally unsafe.
Here's why transfer.sh/3Xbiz/file.mp4
I recommend seeing this too stackoverflow.com/questions/6474783/which-server-variables-are-safe.
Here's an example for both using $_SERVER and using configuration file.

using configuration

config.php
PHP Code

<?php
       $domain 
= "domain1.tld";
 
      /* Other Configurations */ 

PHP Code

<?php 
        require 
"config.php";
 
        if ($domain == "domain1.tld") {
 
                echo "<div id=\"logo-test\"></div>";
 
        }
 
        else if ($domain == "domain2.tld") {
 
                echo "<div id=\"logo-testy\"></div>";
 
        }
 
        else {
 
                echo "<div id=\"logo\"></div>";
 
        } 

and if you don't care about security
using $_SERVER
PHP Code

<?php 
        $domain 
= $_SERVER['HTTP_HOST'];
 
        if ($domain == "domain1.tld") {
 
                echo "<div id=\"logo-test\"></div>";
 
        }
 
        else if ($domain == "domain2.tld") {
 
                echo "<div id=\"logo-testy\"></div>";
 
        }
 
        else {
 
                echo "<div id=\"logo\"></div>";
 
        } 

or even better
PHP Code

<?php 
        $myDomains 
= array("domain1.tld", "domain2.tld");
 
        $domain = $_SERVER['HTTP_HOST'];
 
        if( in_array(strtolower($domain), $myDomains) )
 
                 switch ($domain) {
 
                     case "domain1.tld":
 
                         echo "<div id=\"logo-test\"></div>";
 
                         break;
 
                     case "domain2.tld":
 
                         echo "<div id=\"logo-testy\"></div>";
 
                         break;
 
                     default:
 
                         echo "<div id=\"logo\"></div>";
 
                 }
 
        } 



RE: Different <div> based on which domain is being used. - Quad - Aug 10, 2018

$_SERVER['HTTP_HOST'] is the key. It contains the hostname that was used to navigate to your server.


RE: Different <div> based on which domain is being used. - LinK - Aug 10, 2018

Does, HTTP_HOST, reflect if someone visits from http://domain1.tld and http://www.domain1.tld?


RE: Different <div> based on which domain is being used. - Zexture - Aug 10, 2018

if you visited from domain1.tld, HTTP_HOST will be domain1.tld but if you visited from w w w.domain1.tld it'll be w w w.domain1.tld. If you want it to always be only either one you can add a .htaccess rule to do that.
Redirect non-www to www: stackoverflow.com/questions/12050590/redirect-non-www-to-www-in-htaccess/36807965
Redirect www to non-www: stackoverflow.com/questions/234723/generic-htaccess-redirect-www-to-non-www
However if you want to use both this is a little trickier. You can obviously add the www domain to the accepted Domains but for a better approach you need to get what is after www there are many ways to do this. Here's one.

PHP Code

<?php 
$domain 
= explode('.', $_SERVER['HTTP_HOST'], 2);
if( 
$domain[0] == "www" && in_array(strtolower($domain), $domain[1]) ) {
 
       //Add lcon
}
// Other checks 



RE: Different <div> based on which domain is being used. - LinK - Aug 10, 2018

Right on, thanks! You are a gentleman and a scholar.