0

Hi everyone I try to do a Authenticate form in php.

first I create a form

<form id="frmlogin" name="frmlogin"  method="post" action="validarUsuario.php">
    <table align="center" width="200px">
        <tr>
            <td colspan="2" align="center"><h3>Iniciar sesi&oacute;n</h3></td>
        </tr>
        <tr>
            <td>Usuario</td>
            <td>
                <input type="text" name='usuario' id="usuario"  maxlength="50">
            </td>
        </tr>

        <tr>
            <td>Password</td>
            <td>
                <input type="password" name='password' id="password"  maxlength="50">
            </td>
        </tr>
        <tr>
            <td colspan="2" align="right">
                <input type="submit" name="enviar" value="Enviar" >
            </td>

        </tr>

        <?php

I call validarUsuario so I include require_once '../Classes/validarUsuario.php';

In validarUsuario.php I have this code

include("conectar_bd.php");
conectar_bd();

$usr = $_POST['usuario'];
$pw = $_POST['password'];
//Obtengo la version encriptada del password
$pw_enc = md5($pw);

$sql = "SELECT * FROM ubc_admin_user
            WHERE username = '".$usr."'
            AND password = '".$pw_enc."' ";

$result     =mysql_query($sql,$conexio);

$uid = "";

but I obtain this error

Notice: Undefined index: usuario in /Users/yp/Sites/.../Classes/validarUsuario.php on line 14 Call Stack: 0.0018 646416 1. {main}() /Users/yp/Sites/.../download/index.php:0 0.0020 654048 2. require_once('/Users/yp/Sites/.../validarUsuario.php') /Users/yp/Sites/.../index.php:38 Notice: Undefined index: password in /Users/yp/Sites/...validarUsuario.php on line 15 Call Stack: 0.0018 646416 1. {main}() /Users/../index.php:0 0.0020 654048 2. require_once('/Users/yp/Sites/.../validarUsuario.php') /Users/yp/Sites/../index.php:38

any idea!

user852610
  • 2,205
  • 7
  • 36
  • 46
  • possible duplicate of [Reference - What does this error mean in PHP?](http://stackoverflow.com/questions/12769982/reference-what-does-this-error-mean-in-php) – deceze Apr 09 '13 at 07:42
  • possible duplicate of [PHP: "Notice: Undefined variable" and "Notice: Undefined index"](http://stackoverflow.com/questions/4261133/php-notice-undefined-variable-and-notice-undefined-index) – Jocelyn Apr 17 '13 at 15:50

4 Answers4

2

Change your code to this

if (isset($_POST['Enviar'])) {
    $usr = $_POST['usuario'];
    $pw = $_POST['password'];
    //Obtengo la version encriptada del password
    $pw_enc = md5($pw);

    $sql = "SELECT * FROM ubc_admin_user WHERE username = '".$usr."'AND password = '".$pw_enc."' ";

    $result     =mysql_query($sql,$conexio);

    $uid = "";
}
chandresh_cool
  • 11,753
  • 3
  • 30
  • 45
  • he is getting that error because without submitting form he is trying to access post values. – chandresh_cool Apr 09 '13 at 07:54
  • This answer makes no sence. He is redirecting to validarUsuario.php when he submits the form so he doesnt need any of this. – Daanvn Apr 09 '13 at 07:58
  • 1
    well if he is not submitting his form and directly accessing validarUsuario.php.. than ur absolutely right.. otherwise if he is submitting than isset($_POST) would not make any difference in calling of $_POST["usuario"].. it is the worst practice though to not check the post.. again question is incomplete.. – Dinesh Apr 09 '13 at 08:00
  • Dinesh that is what I thought and gave answer accordingly – chandresh_cool Apr 09 '13 at 08:16
0

Check whether the value is received or not.

if (isset($_POST['usuario'])) {

}
if (isset($_POST['password'])) {

}

Also ensure the path of the receiving file is correct.

sandy
  • 1,126
  • 1
  • 7
  • 17
0

In your HTML form you have single quotes (') on the name values. So you should change this:

name='usuario'
name='password'

to:

name="usuario"
name="password"
Daanvn
  • 1,254
  • 6
  • 27
  • 42
0

You don't need to require_once '../Classes/validarUsuario.php'; because your form will redirect you to that page after form submit. Remove that line.

Or you can make form redirect to same address as form and add check for isset.

Replace:

<form id="frmlogin" name="frmlogin"  method="post" action="validarUsuario.php">

with:

<form id="frmlogin" name="frmlogin"  method="post" action="">

and:

require_once '../Classes/validarUsuario.php';

with:

if(isset($_POST['usuario'])){
    require_once '../Classes/validarUsuario.php';
}
Narek
  • 3,813
  • 4
  • 42
  • 58