How to display information (domain name) using PHP?

Asked

Viewed 958 times

3

How I can display information on my website 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 in links.

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

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

And inside that file domain.php had only one text: http://meusite.com

I mean, at the end of the day, it was like I was using:

href="http://meusite.com"

It would be possible to pull more than one information from a given file?

Something like that:

$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:

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


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

*I am not an expert in PHP xD

I do not know what is called this function, just know that it is commonly used to make translations of websites (which NAY is my case)

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

I’m not saying this is how it has to be done and I’m not saying it’s right :)

In short: A file will have to be created, and within that file will contain the information, and depending on that information, I will set the domain within my site (putting a code that will pull only certain information, in case the correct domain). In this case, each version a different domain, which is why I need this.

4 answers

2

I believe that the simplest solution would be to use the link starting with a bar /.

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. Tag a, the attribute href would look like this:

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

That way, by clicking it would 'take' the root of the site and add the /contato in the end.

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

Did you understand?

  • Thanks for the tip Thiago :) Only I’m using inside a folder, why it’s not working. I will use in another project ;) hug!

  • 1

    Following this logic, you can use the full link path, for example http://meusite.com.br/contato/formulario/enviar would look like this <a href="/contato/formulario/enviar">Contato</a>

1

There are many ways to solve it.. Here’s a simple example:

php file with domains (dominios.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>
  • 2

    It’s not exactly what he did?

  • ._. lol was just an example, not funfa asdasddad

  • 1

    @Papacharlie, if you take a good look, the author’s example of the question lacks echo. That’s the difference.. hehehehehehh

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 to another domain.

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

Just one example using a very simple function. Increment 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 use example keeping the url pointing to the current domain:

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

Force url to Mexico domain:

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

NOTE: In my opinion, URL could be a class because there are other relevant methods, but if kept as a function, note that on this line $domain = explode( '.' , $_SERVER['SERVER_NAME'] ) the variable $domain can be static type to avoid always running the explode.

  • Genius!........

  • 1

    I had doubts if this is what you needed. I forgot to comment on some lines, then I give a up.

  • There is only one problem. This echo before the <a>. echo within the href= ?

  • 1

    No, because echo '<a ...' will print the link concatenated with the function. You can also use a variable $link = '<a href="' . url() . '/contato.html' . '">link</a>'. If echo is inside the function it will print the url before creating the link.

  • 1

    I’ll run some tests :D

  • 1

    I was gonna play ideone, but there it does not rotate $_SERVER.

Show 1 more comment

0

Here is an object-oriented suggestion.

php domains.

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;
  }

}

php page.

<?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 rephrase your question, because you can understand these answers, both mine and others, solve your question.

  • I think that’s exactly what it is! I’m going to test :D

  • Thank you very much! That’s what I needed :)

  • 1

    Wonder, good luck and ;)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.