Using URL variables in PHP code

Asked

Viewed 68 times

-1

Imagine that I have a tagged URL, for example: https://meusite.com.br?utm_source=StackOverflow.

This would be the basic tagging, it also has the parameters utm_source, utm_medium, utm_campaign, utm_term and finally utm_content.

I made a code to pull these parameters and create variables (because I will use them elsewhere). The problem is that if the URL is with ALL parameters the code works normally. Now, if one is missing for example it gives error. I would need him to simply ignore the variable that he doesn’t have.

<?php
    $url_atual = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
    $url_source = $_COOKIE['utm_source'] = $_GET['utm_source'];
    $url_medium = $_COOKIE['utm_medium'] = $_GET['utm_medium'];
    $url_campaign = $_COOKIE['utm_campaign'] = $_GET['utm_campaign'];
    $url_term = $_COOKIE['utm_term'] = $_GET['utm_term'];
    $url_content = $_COOKIE['utm_content'] = $_GET['utm_content'];
    if(empty($url_content)) {
        echo 'A variável está vazia';
    }
    else {
        echo 'O valor da variável é: '.$url_content;
    }
?>
<!DOCTYPE html>
<html lang="pt-BR">
<head>
    <meta charset="UTF-8">
    <title>Meu site</title>
</head>
<body>
    <form action="enviar-lead.php" method="post">
        <input type="text" name="nome" id="nome" placeholder="Nome:">
        <input type="text" name="email" id="email" placeholder="E-mail:">
        <input type="text" name="telefone" id="telefone" placeholder="Telefone:">
        <textarea name="mensagem" id="mensagem" placeholder="Mensagem:"></textarea>
        <input type="submit" class="btnContactDefault" name="btnEnviar" value="Enviar">

        <input type="hidden" name="source" value="<?=($url_source)?>">
        <input type="hidden" name="medium" value="<?=($url_medium)?>">
        <input type="hidden" name="campaign" value="<?=($url_campaign)?>">
        <input type="hidden" name="term" value="<?=($url_term)?>">
        <input type="hidden" name="content" value="<?=($url_content)?>">
    </form>
</body>
</html>
  • Use if and isset() with every $_GET, this is the basics of "current" programming, the "Ifs"

  • If one is missing, it would go as the variable value, empty ?

  • Or you could use a suit to return the null value.

  • @Pedrohenrique, it could be empty. I’ve done it. Thank you.

  • Thanks for the tip, @Guilhermenascimento

1 answer

2


It is good practice to validate variables before using them in production. In your case, just put one if and Else identifying if there is a value to be received by $_GET.

Example with ternary by simplified syntax.

    $url_atual = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
    $url_source = (isset($_GET['utm_source'])) ? $_COOKIE['utm_source'] = $_GET['utm_source'] : null;
    $url_medium = (isset($_GET['utm_medium'])) ? $_COOKIE['utm_medium'] = $_GET['utm_medium'] : null;
    $url_campaign = (isset($_GET['utm_campaign'])) ? $_COOKIE['utm_campaign'] = $_GET['utm_campaign'] : null;
    $url_term = (isset($_GET['utm_term'])) ? $_COOKIE['utm_term'] = $_GET['utm_term'] : null;
    $url_content = (isset($_GET['utm_content'])) ? $_COOKIE['utm_content'] = $_GET['utm_content'] : null;

Syntax of the ternary:

$variavel = (condição) ? valor caso a condição seja verdadeira : valor se for falso;

Note that parentheses represent the condition of IF, the question (?) represents the true answer, and finally the two points (:) that represents IF when the condition is false.

  • Excellent. Thank you, Glenys +1

Browser other questions tagged

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