Send text via form and translate it via PHP link

0

I would like to have the text written in a form added to a link and clicking on the button it would direct to the generated link.

For example.

In the form the user places "tobias" and clicking on the button it is directed to link .

I do not know if I can explain better.

    
asked by anonymous 05.11.2015 / 15:15

2 answers

3
<?php

/**
 * Verifica se a url passada existe fazendo uma requisição a ela e caso ela retorne http code 200, significa que ela existe.
 */
function checkUrl($url) {

  $curl = curl_init($url);
  curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
  $response = curl_exec($curl);
  $httpCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
  curl_close($curl);
  return $httpCode == 200;

}

if (isset($_REQUEST['txt_url'])) {

  $link = 'http://' . $_REQUEST['txt_url'] . '.dominio.com.br';

  if (checkUrl($link)) {
      header('Location: ' . $link);
  } else {
      echo 'Pagina nao existe.';
  }

}

?>

<form action="" method="post">

  <label for="txt_url">Seu texto:</label>
  <input type="text" name="txt_url" value="">

  <input type="submit" value="Ok">

</form>
    
05.11.2015 / 15:45
2
<form action="" method="post">
    <input type="text" name"subdomain" value="">
    <input type="submit" name"ok" value="Enviar">
</form>

<?php
    if($_POST["ok"]) { // Se clicar no botão submit...
        $subdomain = $_POST["subdomain"]; // Pega valor via post e passa para variável.
        if($subdomain != "") { // Se variável não for vazia...
            header('Location: http://'.$subdomain.'.dominio.com.br'); // Redireciona.
        } else { // Se variável for campo vazio...
            echo "Campo vazio!"; // Imprime na tela.
        }
    }
    
05.11.2015 / 15:36