0

I am trying to create a simple Ajax Login system and I have a jQuery Ajax call in my login.php as:

 if(proceed){
   var data = 'email='+tempEmail+'&pass='+tempPass; 
    var loginreq = $.ajax({
                            type: "POST",
                            url : "assets/tempusers.php",
                            cache: false,
                            data: data
                           });
  loginreq.done(function(html) {
  if(html=='true'){
     window.location.replace('app.php');
  }
  else  {
        $("#loginRequest").before('<div class="alert alert-danger err" role="alert">Email or Password Is Not Correct</div>');
   }
  });    
 }

The tempusers.php File is like:

<?php
session_start();
include 'conconfig.php';
$con = new mysqli(DB_HOST,DB_USER,DB_PASS,DB_NAME);

 $email = $_POST['email'];
 $pass =  $_POST['pass'];

 $query = "SELECT * FROM tempusers WHERE user='$email' AND pass='$pass'";
 $result = mysqli_query($con,$query)or die(mysqli_error());
 $num_row = mysqli_num_rows($result);
 $row=mysqli_fetch_assoc($result);
 if( $num_row >=1 ) {
  echo 'true';
     $_SESSION['uName'] = $row['uName'];
  }
 else{
  echo 'false';
 }
?>

and in my app.php I have

<?php
 session_start();
 if(!empty($_SESSION['uName'])){
    header('Location: login.php');
 }
?>
<!DOCTYPE html>
<html lang="en">
  <head>
    <title>Secret Land</title>
  </head>
  <body>
      <h1>Hi This is App</h1>
  </body>
</html>

I am sure that I am inputting correct User Email ans Password into the boxes but the app.php is not showing up! I am not getting any error message either.

When I remove the beginning part of the app.php from the page every thing works fine!

<?php
 session_start();
 if(!empty($_SESSION['uName'])){
    header('Location: login.php');
 }
?>

Can you please let me know what i am doing wrong and what is causing this issue?

1
  • Are you storing passwords as a plain text in the db? are they encrypted/hashed? Commented Oct 8, 2014 at 20:08

1 Answer 1

2

If valid login

$_SESSION['uName'] = $row['uName'];

so yes, it is NOT empty

if(!empty($_SESSION['uName']))

suppose it should be

if(empty($_SESSION['uName']))

right? :)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.