Notice: Undefined index: CodUsua

0

I made a button which, when pressed, sends me to the top of the site and when I press it I get the following error

  

Notice: Undefined index: CodUsua in C: \ xampp \ htdocs \ LF \ html \ profile.php   on line 236

This is the code:

<?php
PRINT <<<HERE
<form method="get" action="#inicio">

<button type="submit">

 <b class="hola2">Ir arriba</b>
</button>
             </form>
             <br>
HERE;


 ?>

  <a name="inicio"></a>

<?php if($_GET['CodUsua'] != $_SESSION['CodUsua']): ?>
        <?php if(empty($verificar_amigos)): ?>
            <li> <a class=" icon-user-plus " href="perfil.php?CodUsua=<?php echo $_GET['CodUsua']; ?>&&agregar=<?php echo $_GET['CodUsua']; ?>"> Agregar </a></li>
        <?php elseif($verificar_amigos[0]['status'] == true): ?>
            <li><a class="icon-users" href="#"> Amigos </a></li>
            <li> <a class=" icon-user-minus " href="perfil.php?CodUsua=<?php echo $_GET['CodUsua']; ?>&&Eliminar=<?php echo $_GET['CodUsua']; ?>"> Eliminar amigo </a></li>
            <?php elseif($verificar_amigos[0]['status'] == false and $_SESSION['CodUsua'] == $verificar_amigos[0]['usua_enviador'] ): ?>
            <li> <a  href="perfil.php?CodUsua=<?php echo $_GET['CodUsua']; ?>&&cancelar=<?php echo $_GET['CodUsua']; ?>"> Cancelar solicitud de amistad </a></li>

            <li><a href="#">Solicitud enviada</a></li>

        <?php endif; ?>
    <?php else: ?>
        <li><a href="../html/editarperfil.php"><span class="icon-pencil2"></span> Editar</a></li>
        <li><a href="../html/editpass.php"><span class="icon-lock"></span> Cambiar contraseña</a></li>
        <li><a href="../html/editarfoto.php"><span class=" icon-image "></span>  Cambiar foto de perfil</a></li>
    <?php endif; ?>
    <li > <a class="icon-users" class="amigos"  href="amigos.php?CodUsua=<?php echo $_GET['CodUsua']; ?>"> Ver amigos <?php 
                        if(!empty(amigos::cantidad_amigos($_GET['CodUsua'])))
                            echo amigos::cantidad_amigos($_GET['CodUsua'])[0][0];
                        else echo 0;
                     ?> </a></li>
    </div>
</div>
    </ul>

</div>
    
asked by Carlos Saz 17.08.2017 в 04:46
source

2 answers

1

The problem you have is that when there is no GET parameter CodUsua in the URL you try to access it anyway. PHP issues a warning whenever you try to access the index of an array that does not exist to warn you that this could lead to an uncontrolled error in your application.

To detect when an index of a matrix exists or not before accessing its value you must use isset() of the following way:

<?php if (isset($_GET['CodUsua']) && ($_GET['CodUsua'] != $_SESSION['CodUsua'])): ?>

Where the evaluation will fail if the index does not exist before accessing it. This is because in a boolean operation && (% logical_co_code ) the evaluation of the other operands is stopped ( are evaluated from right to left as soon as there is one that is worth Y .

Another area where you do not check the validity of the parameter is this:

<li><a class="icon-users" class="amigos" href="amigos.php?CodUsua=<?=
  htmlspecialchars(isset($_GET['CodUsua'])?$_GET['CodUsua']:'')
?>">Ver amigos <?php 
  if(isset($_GET['CodUsua'])) && !empty(amigos::cantidad_amigos($_GET['CodUsua']))) {
    echo amigos::cantidad_amigos($_GET['CodUsua'])[0][0];
  } else {
    echo 0;
  }
?> </a></li>

I understand that you want to access false instead of $_SESSION['CodUsua'] to access the friends of your user code.

Also, I added $_GET['CodUsua'] to remind you that all browser output should go through this function. The content of the super global variable htmlspecialchars() is not controlled, so could easily make you XSS .

I have made use of the ternary operator $_GET so that in case the index do not exist return an empty string, but it is in your hand to adopt another solution in case it does not exist.

    
answered by 17.08.2017 в 06:44
-3

At the moment of pressing the button, the variable GET CodUsua disappears from the URL, and PHP therefore complains that it does not exist.

Add an at before each reference to $ _ GET ['CodUsua'] so that PHP does not show errors when it does not exist. It should be @ $ _ GET ['CodUsua']

    
answered by 17.08.2017 в 05:01