-2

i use the twilio script for phone verification. i get always the code via sms after i send it too my number but i get always this message: "Your verification code is incorrect"

I get the following errors in my error log:

PHP Notice:  Undefined index: code in reserved.php on line 4
PHP Notice:  Undefined variable: numbers in reserved.php on line 15
PHP Notice:  Undefined index: id in /db_functions.php on line 53
PHP Notice:  Undefined index: phone in /db_functions.php on line 54
PHP Notice:  Undefined index: verified in /db_functions.php on line 56
PHP Notice:  Undefined index: start in /db_functions.php on line 57
PHP Notice:  Undefined index: nb_display in /db_functions.php on line 58
PHP Notice:  Undefined variable: jsOnReady in header.php on line 19

I have it all in my Database..?

here my reserved.php code:

<?php
include_once('.../webzone.php');

$code = $_GET['code'];

if($code=='') $jsOnReady = "$('#code').focus();";
else $numbers = get_sms_numbers(array('code'=>$code));

include_once('.../..../header.php');
?>

<div class="container"><center>

<?php
if(count($numbers)>0) {

    if($numbers[0]['verified']!=1) {
        $m1 = new MySqlTable();
        $sql = 'UPDATE '.$GLOBALS['db_table']['sms'].' SET verified=1 WHERE code="'.$m1->escape($code).'"';
        $m1->executeQuery($sql);
    }

    include_once('locked_content.php');

}
else {
    ?>
    <h3>Your verification code</h3>
    <p class="alt" style="margin-bottom:20px;">Please enter the code you have received by SMS</p>
    <form method="GET">
    <input type="text" id="code" name="code" placeholder="Your verification code" style="padding:10px; width:300px;" value="<?php echo $code; ?>"><br>
    <input type="submit" class="btn btn-primary btn-large" value="Verify my code">
    </form>
    <?php
    if($code!='' && count($numbers)==0) {
        $message = 'Your verification code is incorrect';
        echo '<div class="alert alert-error"><button type="button" class="close" data-dismiss="alert">x</button>'.$message.'</div>';
    }
}

?>

</center>
</div>

<?php
include_once('.../.../footer.php');
?>

here the db_functions.php code:

<?php

function add_history($criteria=array()) {
    $type = $criteria['type'];
    $phone = $criteria['phone'];
    $message_id = $criteria['message_id'];
    $message = $criteria['message'];
    $results = $criteria['results'];

    $m1 = new MySqlTable();
    $sql = 'INSERT INTO '.$GLOBALS['db_table']['sms_history'].' (type, phone, message_id, message, results, created) VALUES ("'.$m1->escape($type).'", "'.$m1->escape($phone).'", "'.$m1->escape($message_id).'", "'.$m1->escape($message).'", "'.$m1->escape($results).'", "'.date('Y-m-d H:i:s').'")';
    $m1->executeQuery($sql);
}

function add_sms_number($criteria=array()) {
    $phone = $criteria['phone'];
    $code = $criteria['code'];

    $m1 = new MySqlTable();
    $sql = 'INSERT INTO '.$GLOBALS['db_table']['sms'].' (phone, code, created) VALUES ("'.$m1->escape($phone).'", "'.$m1->escape($code).'", "'.date('Y-m-d H:i:s').'")';
    $m1->executeQuery($sql);
}

function get_sms_history($criteria=array()) {
    $type = $criteria['type'];
    $phone = $criteria['phone'];
    $start = $criteria['start'];
    $nb_display = $criteria['nb_display'];

    $m1 = new MySqlTable();
    $sql = "SELECT * FROM ".$GLOBALS['db_table']['sms_history']." WHERE 1 ";

    if($type!='') $sql .= " AND type='".$m1->escape($type)."'";
    if($phone!='') $sql .= " AND phone='".$m1->escape($phone)."'";

    $sql .= " ORDER BY id DESC";

    if($nb_display!='') $sql .= ' LIMIT '.$start.', '.$nb_display;

    $result = $m1->customQuery($sql);

    if($GLOBALS['demo_mode']==1) {
        for($i=0; $i<count($result); $i++) {
            $result[$i]['phone'] = substr($result[$i]['phone'], 0, -4).'xxxx';
            if($result[$i]['phone']=='') $result[$i]['phone']='xxxx';
        }
    }

    return $result;
}

function get_sms_numbers($criteria=array()) {
    $id = $criteria['id'];
    $phone = $criteria['phone'];
    $code = $criteria['code'];
    $verified = $criteria['verified'];
    $start = $criteria['start'];
    $nb_display = $criteria['nb_display'];

    $m1 = new MySqlTable();
    $sql = "SELECT * FROM ".$GLOBALS['db_table']['sms']." WHERE 1 ";

    if($id!='') $sql .= " AND id='".$m1->escape($id)."'";
    if($phone!='') $sql .= " AND phone='".$m1->escape($phone)."'";
    if($code!='') $sql .= " AND code='".$m1->escape($code)."'";
    if($verified!='') $sql .= " AND verified='".$m1->escape($verified)."'";

    $sql .= " ORDER BY id DESC";

    if($nb_display!='') $sql .= ' LIMIT '.$start.', '.$nb_display;

    $result = $m1->customQuery($sql);

    if($GLOBALS['demo_mode']==1) {
        for($i=0; $i<count($result); $i++) {
            $result[$i]['phone'] = substr($result[$i]['phone'], 0, -4).'xxxx';
            if($result[$i]['phone']=='') $result[$i]['phone']='xxxx';
        }
    }

    return $result;
}

/*
START Default add/update functions
*/

function save_posted_data($data, $table_name) {

    $s1 = new MySqlTable();

    $fields='';
    $fields_values='';
    if(count($data)>0) {
        foreach($data as $ind => $value) {
            $fields .= $s1->escape($ind).',';
            $fields_values .= "'".$s1->escape($value)."',";
        }
    }

    $fields = substr($fields,0,-1);
    $fields_values = substr($fields_values,0,-1);

    $sql = "INSERT INTO $table_name ($fields) VALUES ($fields_values)";
    $s1->executeQuery($sql);
}

function update_posted_data($data, $id, $table_name) {

    $s1 = new MySqlTable();

    $fields='';
    if(count($data)>0) {
        foreach($data as $ind => $value) {
            $fields .= $s1->escape($ind)."='".$s1->escape($value)."',";
        }
    }

    $fields = substr($fields,0,-1);
    $fields_values = substr($fields_values,0,-1);

    $sql = "UPDATE $table_name SET $fields WHERE id='".$s1->escape($id)."'";
    $s1->executeQuery($sql);
}

?>

Thanks for help!

kolag32
  • 3
  • 1
  • Possible duplicate of [PHP: "Notice: Undefined variable" and "Notice: Undefined index"](http://stackoverflow.com/questions/4261133/php-notice-undefined-variable-and-notice-undefined-index) – Charlotte Dunois Aug 27 '16 at 10:55

1 Answers1

0

In general your code looks awful. However, it's easy to explain your issues:

PHP Notice: Undefined index: code in reserved.php on line 4

  • $_GET doesn't contain code. You should check it with isset() firstly.

PHP Notice: Undefined variable: numbers in reserved.php on line 15

  • It seems that sometimes the code is empty string and variable '$numbers' is not defined in this case. Before if write $numbers = [];

PHP Notice: Undefined variable: jsOnReady in header.php on line 19

  • See previous comment because this case is exactly the same.

PHP Notice: Undefined index: id in /db_functions.php on line 53 PHP Notice: Undefined index: phone in /db_functions.php on line 54 PHP Notice: Undefined index: verified in /db_functions.php on line 56 PHP Notice: Undefined index: start in /db_functions.php on line 57 PHP Notice: Undefined index: nb_display in /db_functions.php on line 58

  • You call get_sms_numbers with array that contains only one item 'code'. You should use isset() as well.
Andrej
  • 7,474
  • 1
  • 19
  • 21