Validate PHP URL for sending to Database

Asked

Viewed 156 times

-2

Guys I have this code that does the processing, however I wanted to add a url validator, I tried to insert

  if ( ( ! isset( $site ) || ! filter_var( $site, FILTER_VALIDATE_URL ) ) && !$erro ) {
        $erro = 'Envie um site válido.';
    } 

before the empty field validator (Facebook) however gives error, someone help me please.

<?php
session_start();
$btnCadUsuario = filter_input(INPUT_POST, 'btnCadUsuario', FILTER_SANITIZE_STRING);
if($btnCadUsuario){
    include_once 'conexao.php';
    include_once 'header.php';
    $dados_rc = filter_input_array(INPUT_POST, FILTER_DEFAULT);

    $erro = false;

    $dados_st = array_map('strip_tags', $dados_rc);
    $dados = array_map('trim', $dados_st);


        if(in_array('',$dados)){
        $erro = true ;

                $_SESSION['msg'] = '<div class="alert alert-warning" role="alert">Preencha todos os campos</div>';

        header("Location: index.php");

    }else{



        $result_usuario = "SELECT id FROM usuarios WHERE nome='". $dados['nome'] ."'";
        $resultado_usuario = mysqli_query($conn, $result_usuario);
        if(($resultado_usuario) AND ($resultado_usuario->num_rows != 0)){
            $erro = true;
            $_SESSION['msg'] = '<div class="alert alert-warning" role="alert">RG já está cadastrado </div>';
            header("Location: index.php");
        }
    }


    /// NOME ///

    if(empty($_POST['nome'])){
        $_SESSION['vazio_nome'] = '<div class="alert alert-warning" role="alert"> Preencha!</div>'; 
    }else{
        $_SESSION['value_nome'] = $_POST['nome'];   
    }

    /// IDADE ///

    if(empty($_POST['idade'])){
        $_SESSION['vazio_idade'] = "Campo obrigatório";     
    }else{
        $_SESSION['value_idade'] = $_POST['idade'];
    }

    /// DOCUMENTO ///


    if(empty($_POST['documento'])){
        $_SESSION['vazio_documento'] = "RG é obrigatório";  
    }else{
        $_SESSION['value_documento'] = $_POST['documento'];
    }

    /// FACEBOOK ///


    if (empty($_POST['facebook'])){
        $_SESSION['vazio_facebook'] = "Campo obrigatório";  
    }

    else{
        $_SESSION['value_facebook'] = $_POST['facebook'];
    }





    //var_dump($dados);
    if(!$erro){


        $result_usuario = "INSERT INTO usuarios (nome, idade, facebook, documento,  created) VALUES (
        '" . $dados['nome'] . "',
        '" . $dados['idade'] . "',
        '" . $dados['facebook'] . "',
        '" . $dados['documento'] . "',

        NOW())";


        $resultado_usario = mysqli_query($conn, $result_usuario);

        if(mysqli_insert_id($conn)){
            $_SESSION['msg'] = '<div class="alert alert-success" role="alert">Muito bem, Boa Sorte!!!</div>';
            unset($_SESSION['value_nome']);
            unset($_SESSION['value_idade']);
            unset($_SESSION['value_documento']);
            unset($_SESSION['value_facebook']);

            header("Location: index.php");

        }else{
            $_SESSION['msg'] = '<div class="alert alert-danger" role="alert">Erro</div>';
            header("Location: index.php");
        }
    }

}

?>
  • Explain it better, I didn’t get it right, you want to validate what in the url?

  • Sorry for the lack of information, I want to validate the URL itself whether it is a URL or not, <?php $url = 'http://com.br'; if(filter_var($url, FILTER_VALIDATE_URL) === FALSE) { echo 'URL is invalid'; } Else { echo 'Is the URL valid'; } ? > that way, only in my project.

  • ok found, you want the rest of the code do work if it goes through the url?

1 answer

-2


I am editing the answer because I have misunderstood the question, in your case you or the user who has access to the form will enter a url is this? then the answer is HTML5 and not PHP

Your input needs to be like this:

<input type="url" name="facebook" class="form-control required" placeholder="Facebook">

note that I changed the type field from text to url, html itself will do this check.

  • Please avoid long discussions in the comments; your talk was moved to the chat, just click on the link if you have something to add.

Browser other questions tagged

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