Because you do not work error messages when using Ajax51 [closed]

0

Because I get this error

  

Fatal error: Can not use try without catch or finally in   C: \ xampp \ htdocs \ www \ WEB Studies \ Codes   Studied \ Prinick \ Prinick04 \ PHP Advanced \ core \ models \ class.Access.php   online 51

Line 51 is the

catch (Exception $login) {
    echo $login->getMessage();
}

Here the code of the class Access

<?php

class Acceso{
    private $email;
    private $user;
    private $pass;

    private function Encrypt($string){
        $sizeof = strlen($string) -1;
        $result = '';
        for ($x=$sizeof; $x>=0; $x--) { 
            $result .=$string[$x];
        }
        $result = md5($result);
        return $result;
    }

    public function Login(){
        try {
            if (!empty($_POST['user']) and !empty($_POST['pass']) and !empty($_POST['session'])) {
                $db = new Conexion();
                $this->user = $db->real_escape_string($_POST['user']);
                $this->pass = $this->Encrypt($_POST['pass']);

                $sql = $db->query("SELECT * FROM user WHERE user='$this->user' AND pass='$this->pass'; ");

                if ($db->rows($sql) > 0) {
                    $datos = $db->recorrer($sql);
                    $_SESSION['id'] = $datos['id'];
                    $_SESSION['user'] = $datos['user'];
                    $_SESSION['email'] = $datos['email'];

                    if ($_POST['session'] == true ) {
                        ini_set('session.cookie_lifetime',time() + (60*60*24*2) );
                    }
                    echo 1;
                }else{  
                    throw new Exception(2);

                }
                $db->liberar($sql);
                $db->close();



                }else{
                    throw new Exception("Error : Datos Vacios");

                }
            }
        } catch (Exception $login) {
            echo $login->getMessage();
        }
    }

    public function Registrar(){

    }
    public function Recuperar(){

    }

}



?>

Here is the code of the login.tpl where we have your script

<script>
    window.onload = function(){ 
      document.getElementById('send_request').onclick = function(){

        var connect, user, pass, session, form, result;
        user = document.getElementById('user').value;
        pass = document.getElementById('pass').value;
        session = document.getElementById('session').checked ? true : false;

        if (user != '' && pass != '') {
          form ='user=' + user + '&pass=' + pass + '&session=' + session;

          connect = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject('Microsoft.XMLHTTP');
          connect.onreadystatechange = function(){
            if (connect.readyState == 4 && connect.status == 200) {
              console.log(connect.responseText);

              if (parseInt(connect.responseText) == 1) {
                result = '<div class="alert alert-dismissible alert-success" style="width: 500px;"> ';
                result += '<button type="button" class="close" data-dismiss="alert">&times;</button>'; 
                result += '<strongConectado></strong> Bienvenidos, solo un poco mas ';
                result += '</div>';
                location.href = '?view=index';
                document.getElementById('_AJAX_').innerHTML = result;
              }else{
                result = '<div class="alert alert-dismissible alert-danger" style="width: 500px;"> ';
                result += '<button type="button" class="close" data-dismiss="alert">&times;</button>'; 
                result += '<strongERROR></strong>Credenciales Incorrectas ';
                result += '</div>';
                document.getElementById('_AJAX_').innerHTML = result;
              }
            }else if(connect.readyState != 4){
              result = '<div class="alert alert-dismissible alert-warning" style="width: 500px;"> ';
                result += '<button type="button" class="close" data-dismiss="alert">&times;</button>'; 
                result += 'Procesando... ';
                result += '</div>';
                document.getElementById('_AJAX_').innerHTML = result;
            }
          }
          connect.open('POST','?view=login',true);
          connect.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
          connect.send(form);
        }else{
          result = '<div class="alert alert-dismissible alert-warning" style="width: 500px;"> ';
          result += '<button type="button" class="close" data-dismiss="alert">&times;</button>'; 
          result += '<strongERROR></strong> El usuario y la contraseña no pueden estar vacios ';
          result += '</div>';
          document.getElementById('_AJAX_').innerHTML = result;
        }






      }
    }
  </script>

Here loginController.php

<?php
    if (!isset($_SESSION['id'],$_SESSION['user'],$_SESSION['email'])) {
        if ($_POST) {
            include('core/models/class.Acceso.php');
            $acceso = Acceso();
            $acceso->Login();
            exit;
        }else{
            $template = new Smarty();
            $template->display('public/login.tpl');
        }
    }else{
        header('location: ?view=index');
    }



?>
    
asked by Gamez 12.04.2017 в 16:25
source

1 answer

1

If you notice, you have a "}" of more.

}else{
  throw new Exception("Error : Datos Vacios");
}

After that code there is a} that is not closing anything. Therefore for the program it is as if you never put a try {} catch because it closes the function before entering the catch.

And then in loginController.php you have this:

if ($_POST) {
   include('core/models/class.Acceso.php');
   $acceso = Acceso();
   $acceso->Login();
   exit;
}

and product to which $acceso = Acceso(); you must place $acceso = new Acceso(); because it is a new instance of the class as an object now

if ($_POST) {
   include('core/models/class.Acceso.php');
   $acceso = new Acceso();
   $acceso->Login();
   exit;
}

I hope it helps you that

    
answered by 12.04.2017 / 16:31
source