0

hi i am trying to pass login form but its give to me success all the time ,if its wrong user or user in data base , this is my code =>

this is the ajax code =>

$J(document).ready(function () {
    $J("#add_err").css('display', 'none', 'important');
    $J("#login").click(function () {
        email = $J("#email").val();
        password = $J("#password").val();  
        $J.ajax({ 
            type: "POST",
            url: "login.php",
            data: "email=" + email + "&password=" + password,
            success:function (data) {
                if (data) {
                    //$("#add_err").html("right username or password");
                    window.location = "dashboard.php";
                } else {
                    $J("#add_err").css('display', 'inline', 'important');
                    $J("#add_err").html("<img src='images/alert.png' /> <b>Wrong username or password </b>" );
                }
            },
            beforeSend: function ()
            {
                $J("#add_err").css('display', 'inline', 'important');
                $J("#add_err").html("<img src='images/spin.gif' /> Loading...")
            }
        }); 
        return false;
    });
});

this is the form => in file login.php

<form action="" method="POST" id="loginForm">
            <div class="form-group has-feedback">
                <input type="email" name="email" id="email" class="form-control" placeholder="Email">
                <span class="glyphicon glyphicon-envelope form-control-feedback"></span>
            </div>
            <div class="form-group has-feedback">
                <input type="password"  name="password" id="password" class="form-control" placeholder="Password">
                <span class="glyphicon glyphicon-lock form-control-feedback"></span>
            </div>
            <div class="row">
                <div class="col-xs-8">
                    <div class="checkbox icheck">
                        <input type="checkbox" name="iCheck"  /> 
                        <b>Remember me </b>
                        <script>
                            $J(document).ready(function () {
                                $J('input').iCheck({
                                    checkboxClass: 'icheckbox_flat-blue',
                                    radioClass: 'iradio_flat',
                                    increaseArea: '-10%', // optional
                                });
                            });
                        </script>
                    </div>
                </div>
                <!-- /.col -->
                <div class="col-xs-4">
                    <button type="submit" id="login" class="btn btn-primary btn-block btn-flat">Sign In</button>
                </div>
                <!-- /.col -->
            </div>
        </form>

and this is the post i move to data base => in same file login.php

if (isset($_POST['email']) && isset($_POST['password'])) {
session_start();
$dbConnect = new dbconnect();
$email = $_POST['email'];
$password = $_POST['password'];
$userData = $dbConnect->verify_login($email, $password);
if (!$userData) {
    echo "false";
}}

varifylogin function=>

    public function verify_login($email, $password) {
    $this->Connect();
    $sql = "SELECT email,password FROM `users_details` WHERE `email`='$email' AND `password`='$password' ";
    if ($this->res = mysqli_query($this->ind_connect, $sql)) {
        $num_row = mysqli_num_rows($this->res);
        $row = mysqli_fetch_assoc($this->res);
        if ($num_row == 1) {
            echo 'true';
            $_SESSION['email'] = $row['email'];
            $_SESSION['password'] = $row['password'];
        }
    } else {
        echo 'errr';
    }
    $this->Disconnect();
}

i realy do not know what to do help is welcome now Ty.

Liran Atli
  • 43
  • 1
  • 9
  • `if (data) {` checks if the variable is not falsy, in your php-script however you are returning a string (`echo "false";`) therefore the variable `data` is set and not falsy. – empiric Nov 22 '16 at 18:29
  • Can we see the body of verify_login function – Ghost Worker Nov 22 '16 at 18:40
  • the data is not false he get values from data base – Liran Atli Nov 22 '16 at 18:41
  • your function `verify_login` doesn't have a return value. So `if (!$userData) {` is always true.Therefore it echoes `"false"` which is the content if the js variable `data`. Therefore `if(data) {` is always true – empiric Nov 22 '16 at 18:49
  • 1
    [Little Bobby](http://bobby-tables.com/) says ***[your script is at risk for SQL Injection Attacks.](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php)*** Learn about [prepared](http://en.wikipedia.org/wiki/Prepared_statement) statements for [MySQLi](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php). Even [escaping the string](http://stackoverflow.com/questions/5741187/sql-injection-that-gets-around-mysql-real-escape-string) is not safe! [Don't believe it?](http://stackoverflow.com/q/38297105/1011527) – Jay Blanchard Nov 22 '16 at 19:48
  • 1
    **Never store plain text passwords!** Please use PHP's [built-in functions](http://jayblanchard.net/proper_password_hashing_with_PHP.html) to handle password security. If you're using a PHP version less than 5.5 you can use the `password_hash()` [compatibility pack](https://github.com/ircmaxell/password_compat). Make sure you ***[don't escape passwords](http://stackoverflow.com/q/36628418/1011527)*** or use any other cleansing mechanism on them before hashing. Doing so *changes* the password and causes unnecessary additional coding. – Jay Blanchard Nov 22 '16 at 19:48
  • Why are you storing the password in the session? Geez, that could be so easily hacked. – Jay Blanchard Nov 22 '16 at 19:49
  • @LiranAtli If you would read and understand my comments you would be able to solve your problem. We are not a coding service. When trying to code a web app you should start with the basics of the programming language – empiric Nov 22 '16 at 20:26

1 Answers1

0

In the function: verify_login

You must return a true value of boolean type, not as text.

Then improve the code as follows.

$userData = $dbConnect->verify_login($email, $password);
if ($userData) {
    echo "true";
}else{
    echo "false";
}
}

In ajax call

success:function (data) {
if (data=='true') {
//..
}else{
//..
}
}

Another way is to make php return a json, and in ajax it indicates dataType: "json"

$.ajax({
    url : "",
    type : "POST",
    dataType : 'json',
    success : function(data) {
        console.log(data);
        if(data.success==true){

        }else{
        }
   }
 });

In the file php:

echo json_encode(array('success'=>true));
Geynen
  • 2,081
  • 2
  • 10
  • 6
  • in verifaylogin i do return true / false and i change the echo from data but now its never pass... the ajax data does not get the "true" :( – Liran Atli Nov 22 '16 at 21:56
  • i know the problem login.php page is not clear with the echo true he got more html stuff. i need to clear the page and send only the "true" or "false" echo – Liran Atli Nov 22 '16 at 23:12