How to display an information (domain name) using PHP?

3

How can I display information on my site using PHP ?

I am creating a personal website, and I want to make versions of this site in other languages.

What happens is that I need to set the domain of my site to be able to put links.

I was using a include and in the href="" links, I would put:

href="<?php include 'dominio.php'; ?>"

And within this domain.php file I only had one text: http://meusite.com

That is, in the end, it was like I was using:

href="http://meusite.com"

Would it be possible to pull more than one information through a particular file?

Something like this:

$dominio['brasil']=http://meusite.com.br;
$dominio['mexico']=http://meusite.com.mx;
$dominio['argentina']=http://meusite.com.ar;

And in each version I used the correct option.

Examples:

  

In the Brazilian site:    href="<?php $dominio['brasil']; ?>" that is equal to href="http://meusite.com.br"

     

On the Mexican site:    href="<?php $dominio['mexico']; ?>" that is equal to href="http://meusite.com.mx"

* I'm not an expert on PHP xD

I do not know what this function is called, I just know it's commonly used to do site translations (which is NOT my case)

It's just kind of like I believe it to be.

I'm not saying that this is how it has to be done and that it's not right, I just imagine it to be something like this:)

  

In short: A file will have to be created, and within that file it will contain the information, and depending on that information, I'll set the domain inside my site a code that will pull only certain information, in this case the correct domain). In case, each version a different domain, so I need it.

    
asked by anonymous 18.12.2014 / 06:25

4 answers

2

I think the simplest solution would be to use the link starting with a / .

For example, if on your homepage you have a link to the contact page. The path would be http://meusite.com.br/contato or http://meusite.com.mx/contato . In the a tag, the href attribute would look like this:

<a href="/contato">Contato</a>

In this way, clicking it would 'take' the root of the site and add /contato to the end.

If the user is in the Brazilian version, the link would automatically be http://meusite.com.br/contato . If the user is in the version of Mexico, the link would be http://meusite.com.mx/contato .

Did you understand?

    
18.12.2014 / 12:05
1

There are many ways to solve .. Here is a simple example:

Php file with domains (domains.php):

<?php
$dominio['brasil']=http://meusite.com.br;
$dominio['mexico']=http://meusite.com.mx;
$dominio['argentina']=http://meusite.com.ar;
?>

Link page:

<?php
include 'dominios.php';
?>

<a href="<?php echo $dominio['brasil']; ?>">link do site</a>
    
18.12.2014 / 07:14
1

My proposal is something more dynamic using SERVER_NAME to identify the URL TLD and keep the links in the same domain or force it to another domain.

Basically, if you want to point the link to another domain, simply use the indexes of the $dominios array, otherwise keep the parameter blank so that the url matches the current domain TLD.

Only one example using a very simple function. Increase it as needed.

function url( $domain = null )
{
    $dominios['br'] = 'http://meusite.com.br';
    $dominios['mx'] = 'http://meusite.com.mx';
    $dominios['ar'] = 'http://meusite.com.ar';

    if( ! $domain )
    {
        $domain = explode( '.' , $_SERVER['SERVER_NAME'] );
        return $domains[ end( $domain ) ];
    }
    else
    {
        return $domains[ $domain ];
    }
}

Dynamic usage example keeping the url pointing to the current domain:

echo '<a href="' . url() . '/contato.html' . '">link</a>';

Force url for Mexico domain:

echo '<a href="' . url('mx') . '/contato.html' . '">link</a>';
  In my opinion, URL could be a class as there are other relevant methods, but if you keep as a function, note that in this line $domain = explode( '.' , $_SERVER['SERVER_NAME'] ) the variable $domain can be static type to prevent it from ever executing explode .

    
18.12.2014 / 20:26
0

Here is an object-oriented suggestion.

domains.php

class Dominios
{

  const BRASIL = "http://meusite.com.br";
  const MEXICO = "http://meusite.com.mx";
  const ARGENTINA = "http://meusite.com.ar";

  public static function getBrasil()
  {
    return self::BRASIL;
  }

  public static function getMexico()
  {
    return self::MEXICO;
  }

   public static function getArgentina()
  {
    return self::ARGENTINA;
  }

}

page.php

<?php include 'dominios.php'; ?>

<a href="<?php echo Dominios::BRASIL; ?>">link do site</a>
// OU
<a href="<?php echo Dominios::getBrasil(); ?>">link do site</a>

If you see that the answers are not solving the problem, please reformulate your question, because by what you understand the answers, both mine and others answer your question.

    
18.12.2014 / 15:02