Foros del Web » Programando para Internet » PHP »

Validar URL

Estas en el tema de Validar URL en el foro de PHP en Foros del Web. Buenas noches. Pues como dice el titulo, necesito ayuda para validar los enlaces que suben los usuarios a mi sitio a través de un input ...
  #1 (permalink)  
Antiguo 24/01/2016, 23:07
 
Fecha de Ingreso: enero-2016
Mensajes: 19
Antigüedad: 8 años, 3 meses
Puntos: 0
Validar URL

Buenas noches.

Pues como dice el titulo, necesito ayuda para validar los enlaces que suben los usuarios a mi sitio a través de un input text y asegurarme que la url existe y es correcta antes de ser publicada.

Trabajo con wordpress y dejo el código de funciones del script, por si alguien puede ayudarme por que estoy volviéndome loco.

Código PHP:
$nombre_servidor = array('1.png''2.png''3.png');
$tipo_audio = array('Spain.png','Spain-sub.png','Portugal.png','Argentina.png'); // aqui el idioma del audio
$tipo_calidad = array('calidad1','calidad2','calidad3'); // la calidad del video
        
$enlace_roto = array('ok''caido'); // esto es para informar si el enlace esta ok o caido, pueden poner lo que quieran
//en todas pueden poner todas las opciones que quieran.



    
    
function Print_peliculas_fileds($cnt$p null){
    global 
$nombre_servidor;
    global 
$tipo_audio;
    global 
$tipo_calidad;
    global 
$enlace_roto;
    
    if (
$p === null) {
        
$enlace $user $servidor $audio $calidad $roto '';
    } else {
        
$servidor         $p['servidor'];
        
$nombre_servidors $p['nombre_servidors'];
        
$audio            $p['audio'];
        
$calidad          $p['calidad'];
        
$enlace           $p['enlace'];
        
$user             $p['user'];
        
$roto             $p['roto'];
    }
    
$retu '<p style="margin-bottom: 22px;">';
    
$retu .= '<label>Servidor</label> ';
    
$retu .= '<select name="peliculas_data[' $cnt '][servidor]" id="servidor"> ';
    foreach (
$nombre_servidor as $nombre_servidores) {
        if (
$nombre_servidores == $servidor) {
            
$retu .= '<option value="' $servidor '"  selected="slelected"  >' $servidor '</option>';
        } else {
            
$retu .= '<option value="' $nombre_servidores '">' $nombre_servidores '</option>';
        }
    }
    
$retu .= '</select>';
    
$retu .= '<label>Audio :</label> ';
    
$retu .= '<select name="peliculas_data[' $cnt '][audio]" id="audio"> ';
    foreach (
$tipo_audio as $tipo_audios) {
        if (
$tipo_audios == $audio) {
            
$retu .= '<option value="' $audio '"  selected="slelected"  >' $audio '</option>';
        } else {
            
$retu .= '<option value="' $tipo_audios '">' $tipo_audios '</option>';
        }
    }
    
$retu .= '</select>';
    
$retu .= '    <label>Calidad :</label>';
    
$retu .= '<select name="peliculas_data[' $cnt '][calidad]" id="audio"> ';
    foreach (
$tipo_calidad as $tipo_calidades) {
        if (
$tipo_calidades == $calidad) {
            
$retu .= '<option value="' $calidad '"  selected="slelected"  >' $calidad '</option>';
        } else {
            
$retu .= '<option value="' $tipo_calidades '">' $tipo_calidades '</option>';
        }
    }
    
$retu .= '</select>';
    
$retu .= '    <label>Enlace Roto :</label>';
    
$retu .= '<select name="peliculas_data[' $cnt '][roto]" id="audio"> ';
    foreach (
$enlace_roto as $enlace_rotos) {
        if (
$enlace_rotos == $roto) {
            
$retu .= '<option value="' $roto '"  selected="slelected"  >' $roto '</option>';
        } else {
            
$retu .= '<option value="' $enlace_rotos '">' $enlace_rotos '</option>';
        }
    }
    
$retu .= '</select>';
    
$retu .= '</br>';
    
$retu .= '     <label>Enlace :</label>';
    
$retu .= '</br>';
    
$retu .= '     <input type="text" style="width: 80%;" name="peliculas_data[' $cnt '][enlace]" value="' htmlentities($enlaceENT_QUOTES) . '"/>';
    
$retu .= '</br>';
    
$retu .= '     <label>Usuario :</label>';
    
$retu .= '</br>';
    
$retu .= '     <input type="text" style="width: 80%;" name="peliculas_data[' $cnt '][user]" value="' htmlentities($userENT_QUOTES) . '"/>';
    
$retu .= '     <input type="hidden" name="peliculas_data[' $cnt '][new]" value="0"/>';
    
$retu .= '     <input type="hidden" name="peliculas_data[' $cnt '][removed]" class="removeinput" value="0"/>';
    
$retu .= '     <input type="hidden" name="peliculas_data[' $cnt '][idlink]" value="' $cnt '"/>';
    
$retu .= '</br>';
    
$retu .= '</br>';
    
$retu .= '  <span style="border-radius: 3px; cursor: pointer; background: #f7f7f7; box-shadow: 0 1px 0 #ccc;border: solid 1px #ccc; padding: 5px;"class="remove">Eliminar</span>';
    
$retu .= '</p>';
    return 
$retu;
}

//add custom field - peliculas
add_action("add_meta_boxes""object_init");

function 
object_init(){
    
add_meta_box("peliculas_meta_id""Peliculas:""peliculas_meta""pelicula""normal""low");

Gracias.
  #2 (permalink)  
Antiguo 24/01/2016, 23:08
 
Fecha de Ingreso: enero-2016
Mensajes: 19
Antigüedad: 8 años, 3 meses
Puntos: 0
Respuesta: AYUDA!Validar URL

Código PHP:
function peliculas_meta(){
    global $nombre_servidor;
    global $tipo_audio;
    global $tipo_calidad;
    global $enlace_roto;
    global $post;
    $data = get_post_meta($post->ID, "peliculas_data", true);
    echo '<div id="peliculas_items">';
    if (!empty($data)) {
        foreach((array)$data as $c => $p ){  
            if (isset($p['calidad']) || isset($p['audio']) || isset($p['servidor']) || isset($p['enlace']) || isset($p['user']) || isset($p['roto'])){
                echo Print_peliculas_fileds($c, $p);
            }
        }
    }
    echo '</div>';
?>
        <span id="here"></span>
        <span class="add"><?php
    
echo __('Nueva Pelicula');
    
    
$add_to_ID get_post_meta($post->ID,"_linksAutoincrement",true);
    if ( empty( 
$add_to_ID ) )
        
$add_to_ID 0;
    
$add_to_ID $add_to_ID 1;
?></span>
        <script>
            var $ =jQuery.noConflict();
                $(document).ready(function() {
                var count = <?php echo $add_to_ID;?>;
                $(".add").click(function() {
                    count = count + 1;
                    $('#peliculas_items').append('<p style="margin-bottom: 22px;"><label>Servidor</label><select name="peliculas_data[' + count + '][servidor] id="servidor"><?php
    
foreach ($nombre_servidor as $nombre_servidores) {
        echo 
'<option value="' $nombre_servidores '">' $nombre_servidores '</option>';
    }
?></select><label>Audio</label><select name="peliculas_data[' + count + '][audio] id="audio"><?php
    
foreach ($tipo_audio as $tipo_audios) {
        echo 
'<option value="' $tipo_audios '">' $tipo_audios '</option>';
    }
?></select><label>Calidad</label><select name="peliculas_data[' + count + '][calidad] id="calidad"><?php
    
foreach ($tipo_calidad as $tipo_calidades) {
        echo 
'<option value="' $tipo_calidades '">' $tipo_calidades '</option>';
    }
?></select><label>Enlace roto:</label><select name="peliculas_data[' + count + '][roto] id="roto"><?php
    
foreach ($enlace_roto as $enlace_rotos) {
        echo 
'<option value="' $enlace_rotos '">' $enlace_rotos '</option>';
    }
?></select></br><label>enlace :</label><br><input type="text" style="width: 80%;" name="peliculas_data[' + count + '][enlace]" value=""/><br>    <label>usuario:</label><br><input type="text" style="width: 80%;" name="peliculas_data[' + count + '][user]" value=""/><br> <input type="hidden" name="peliculas_data[' + count + '][new]" value="1"/>  <input type="hidden" name="peliculas_data[' + count + '][removed]" class="removeinput" value="0"/>  <input type="hidden" name="peliculas_data[' + count + '][idlink]" value="' + count + '"/>  <span class="remove">Remove</span></p>');
                   return false;
                });
                $(".remove").live('click', function() {
                    $(this).parent().find(".removeinput").val("1").parent().hide();
                });
            });
        </script>
        <style>#peliculas_items {list-style: none;}</style>
    <?php
}

//Save product peliculas
add_action('save_post''guardar_peliculas');

function 
guardar_peliculas($post_id){
    global 
$post;
    
// to prevent metadata or custom fields from disappearing... 
    
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
        return 
$post_id;
    
// OK, we're authenticated: we need to find and save the data
    
if (isset($_POST['peliculas_data'])) {
        
$data $_POST['peliculas_data']; 
        
    
$existing_list get_post_meta($post->ID,"peliculas_data",true); 
    if (empty(
$existing_list))
        
$existing_list = array();
    
    
$add_to_ID get_post_meta($post->ID,"_linksAutoincrement",true);
    if ( empty( 
$add_to_ID ) )
        
$add_to_ID 0;
        
    foreach((array)
$data as $p ){ //proceso cada link de la peticion editando el array de links de la base de datos por si alguien agrego un link mientras editaba
        
if ( ! is_numeric$p["idlink"] ) )
            die(
"Wrong ID!");
        
$p["idlink"] = abs(intval($p["idlink"]));
        if ( ! 
$p["idlink"])
            die(
"Wrong ID!");
        if (
$p["new"]=="1"){ //link nuevo
            
if ($p["removed"]=="0"){ //si la agrego y elimino en la misma edicion no se hace nada
                
$add_to_ID $add_to_ID 1;
                if (isset(
$existing_list[$add_to_ID]))
                    die(
"Ya existe el ID: ".$add_to_ID);
                unset(
$p["removed"]);
                unset(
$p["new"]);
                unset(
$p["idlink"]);
                if (
validarLink($p)==false)
                    die(
"Un campo no esta entre los permitidos");
                if (
filter_var($p['enlace'], FILTER_VALIDATE_URL) !== false){
                    
$existing_list[$add_to_ID] = $p;
                }else{
                    die(
"enlace no valido: " htmlspecialchars($p['enlace']));
                }
            }
        }else if (
$p["removed"]=="1"){ //link removido
            
if (isset($existing_list[$p["idlink"]]))
                unset(
$existing_list[$p["idlink"]]); //elimino el link (no re-indexa)
        
}else if (isset($existing_list[$p["idlink"]])){
            unset(
$p["removed"]);
            unset(
$p["new"]);
            
$id $p["idlink"];
            unset(
$p["idlink"]);
            if (
validarLink($p)==false)
                die(
"Un campo no esta entre los permitidos");
            if (
filter_var($p['enlace'], FILTER_VALIDATE_URL) !== false){
                
$existing_list[$id] = $p;
            }else{
                die(
"enlace no valido: " htmlspecialchars($p['enlace']));
            }
        }
    }
    
update_post_meta($post_id'peliculas_data'$existing_list);
    
update_post_meta($post->ID'_linksAutoincrement'$add_to_ID);
    }
}


function 
validarLink($p){
    global 
$nombre_servidor;
    global 
$tipo_audio;
    global 
$tipo_calidad;
    global 
$enlace_roto;
    if (
in_array($p['servidor'],$nombre_servidor)){
        if (
in_array($p['audio'],$tipo_audio)){
            if (
in_array($p['calidad'],$tipo_calidad)){
                if (
in_array($p['roto'],$enlace_roto)){
                    return 
true;
                }
            }
        }
    }
    return 
false;
}
?>
  #3 (permalink)  
Antiguo 25/01/2016, 06:52
 
Fecha de Ingreso: enero-2016
Mensajes: 19
Antigüedad: 8 años, 3 meses
Puntos: 0
Respuesta: Validar URL

Puede ayudarme alguien?

Etiquetas: html, select, url, usuarios
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 05:02.