20i Domains

Different logo depending on URL

Discussion in 'Website Design' started by SecNam, Dec 5, 2020.

  1. SecNam

    SecNam Moderator Staff Member

    Joined:
    Jul 2004
    Posts:
    5,315
    Likes Received:
    57
    Hi .

    I am hoping someone can help.

    I am looking for a way to show a differenrt logo depending on what URL is in the address bar

    I am using a php built website and not wordpress.

    thanks

    Paul
     
  2. Domain Forum

    Acorn Domains Elite Member

    Joined:
    1999
    Messages:
    Many
    Likes Received:
    Lots
    articles.co.uk
     
  3. dee

    dee Well-Known Member Acorn Supporter

    Joined:
    May 2013
    Posts:
    2,222
    Likes Received:
    315
    php url parameter ?
     
  4. bensd United Kingdom

    bensd Well-Known Member

    Joined:
    Jan 2007
    Posts:
    5,110
    Likes Received:
    69
  5. SecNam

    SecNam Moderator Staff Member

    Joined:
    Jul 2004
    Posts:
    5,315
    Likes Received:
    57
    Hi

    this is what i have added to the top of my index.php file which selects which file to load when a certain domain is in the URL, what i now need is what a certain url is in the address bar to show that specific logo.


    switch($_SERVER["HTTP_HOST"])
    {
    case "domain1.co.uk":
    require("domain1.php");
    exit();
    break;

    case "domain2.co.uk":
    require("domain2.php");
    exit();
    break;

    }
     
  6. Ben Thomas

    Ben Thomas Well-Known Member

    Joined:
    Mar 2018
    Posts:
    1,729
    Likes Received:
    219
    Would this not be more efficient in an array with a simple foreach loop?
     
  7. Skinner

    Skinner Well-Known Member

    Joined:
    Jul 2008
    Posts:
    4,662
    Likes Received:
    140
    Look inside $_SERVER['HTTP_HOST'].

    The easiest way to have all sub domains inc www redirect to pure http://domain.ext, then explode/split the result into an array and [0] will the domain name without the ext, then use result[0].jpg to display it.
     
  8. SecNam

    SecNam Moderator Staff Member

    Joined:
    Jul 2004
    Posts:
    5,315
    Likes Received:
    57
    If anyone has a guide on this that woukd be great as it is something i dont have experiwnce with.
     
  9. Skinner

    Skinner Well-Known Member

    Joined:
    Jul 2008
    Posts:
    4,662
    Likes Received:
    140
    Your existing code would work, just include a "$logo" variable with the require

    switch($_SERVER["HTTP_HOST"])
    {
    case "domain1.co.uk":
    require_once("domain1.php");
    $logo = "domain1.jpg";
    exit();
    break;

    case "domain2.co.uk":
    require_once("domain2.php");
    $logo = "domain2.jpg";
    exit();
    break;

    }

    Then where you want to use it just use it like "img src=$logo", or use a second switch at the top of the code to just handle the logo.

    switch($_SERVER["HTTP_HOST"])
    {
    case "domain1.co.uk":
    $logo = "domain1.jpg";
    break;

    case "domain2.co.uk":
    $logo = "domain2.jpg";
    break;
    }

    If the logo is inside domaion1.php, then put the switch inside that file for the logo.

    There are loads of ways to do it, but if you already have switches probably better to use similar code.
     
    • Like Like x 1
  10. lazarus

    lazarus Well-Known Member Exclusive Member

    Joined:
    Feb 2013
    Posts:
    1,268
    Likes Received:
    283
    A very easy way without using any loops, if's / switches. etc

    My pseudo code would be.

    Get last part of url or any part of it that is a unique identifier. For example https://www.mydomain.co.uk/mypage2 (mypage2)
    Append ".png" to the string to make mypage2.png

    Upload your given logo image and call it mypage2.png.

    This should be about 4 lines of code and you will not need to add any more going forward when you add more logos / pages.

    Maybe have one if statement in there to display a default image if dynamic one is not found.
     
    Last edited: Dec 6, 2020
  11. jonoweb United Kingdom

    jonoweb New Member

    Joined:
    Dec 2020
    Posts:
    3
    Likes Received:
    2
    Is it main domain or sub domain?

    You could do this for main domain:

    Code:
    $url = $_SERVER['SERVER_NAME'];
    // or $_SERVER['HTTP_HOST'];
    
    $logo = "default_logo.png";
    
    if ($url == "www.example.com")
    {
        $logo = "example1.png";
    }
    elseif ($url == "www.example2.com")
    {
        $logo = "example2.png";
    }
    
    <img src="<?php echo $logo;">">
     
    • Like Like x 1
  12. jonoweb United Kingdom

    jonoweb New Member

    Joined:
    Dec 2020
    Posts:
    3
    Likes Received:
    2
    If it were sub domain based you could do this:

    PHP:
      $logo "default";

      
    $sub_domain explode('.'$_SERVER['HTTP_HOST']);

      
    $sub_domain str_replace("https://"""$sub_domain); // Remove the https (or http)
     
      
    if (count($sub_domain) > 2) {
        if (
    $sub_domain[0] == "www") { // if non sub eg (www.maindomain.com)
          
    $sub_domain "main";
        } else {
         
    $sub_domain $sub_domain[0];
        }

        
    $logo $sub_domain;
      }
    Usage:

    Name your logo for the relevant case eg default,main,uk,fr,de etc

    For every case where the rules aren't met it will display 'default', non-sub domain will show 'main' and every sub domain will display the sub domain (just add the logo extension).

    HTML:
      <img src="<?php echo $logo;?>.png">
     
    • Like Like x 1
  13. Hay

    Hay Active Member Exclusive Member

    Joined:
    Jul 2019
    Posts:
    389
    Likes Received:
    99
    This sounds like you are making it way too complicated, including another php script just to show a logo is overkill.

    Depending on how many logo's you are talking about would dictate the best option, for example if you are only dealing with a few logo's then use a function which contains a switch statement for each case.. However, if you have like 20+ logo's then simply use a database query which fetches the record / logo based on the domain or domain_id.
     
    • Like Like x 1