Send text via form and transform in link via PHP

Asked

Viewed 468 times

0

I would like the text written in a form to be added to a link and when clicking on the "button" it directs to the generated link.

For example.

In the form the user puts "Tobias" and when clicking the button it is directed to http://tobias.dominio.com.br.

I don’t know if I can explain it better.

  • 1

    Have you tried any code? Post the code for us to analyze.

  • then actually not yet pq I have no idea where to start. If it were the opposite I would make a "cat". For example, I would send $url via get ($url=$_GET['url'];) and the other place header("Location: redirects.php? url=$url");

  • 1

    Just use JS. By pressing the button you take the INPUT content, assemble a URL and redirect it.

  • But how would you do it in js? I know how to redirect but take the content and add the url I already know. .

  • and for example, if the url does not exist this way it will load the wrong page the same way, correct?

  • It really needs to be done via php? a simple redirect can be done in javascript. So avoid traffic and server processes. Independent of being a very small process.

  • It could be any language but only in JS would be better, but I still wouldn’t know how to do it. For example, if you could leave the form always fixed on the page and click on an iframe would be perfect, but there would be more blacksmithing. Since I don’t know how to do I think it would be too much to ask for so much.

Show 2 more comments

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>
  • It worked, Tks! Is there any way (I don’t think q is possible) to know if the URL is existing? If not, does it report that the subdomain is wrong? Ex: subdominioexiste.dominio.com.br -> it goes to the page subdominioinexistent.dominio.com.br -> it stays on the page but warns that the domain does not exist.

  • Or rather, it open in an iframe, so I can keep a bar always active to return.

  • I added the checkUrl function that checks if the url exists, checks to see if it solves your problem.

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

Browser other questions tagged

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