0

After login header location is not working. it staying in login.php and shows as blank page. How can I solve this problem? Help me out please.

This my index.php code:

<div class="col-sm-12 col-md-12">
  <form action="login.php" method="POST">
      <div class="form-group"> 
        <label>Email</label>
        <input class="form-control" name="email" placeholder="Type your Email..." type="email" required="">
      </div>    
    </div>

    <div class="col-sm-12 col-md-12">
      <div class="form-group"> 
        <label>Password</label>
        <input class="form-control" name="password" placeholder="Type your password..." type="password" required=""> 
      </div>
    </div>

    <div class="col-sm-12 col-md-12">
      <div class="login-box-box-action">
        No account? <a data-toggle="modal" href="#registerModal">Register</a>
      </div>
    </div>

    </div>
    </div>

    <div class="modal-footer text-center">
      <button type="submit" class="btn btn-primary" onclick= "myFunction()">Sign-in</button>
      <button type="button" data-dismiss="modal" class="btn btn-primary btn-border">Cancel</button>
    </div>
  </form>

This is my login.php code:

<?php
    include 'db_connection.php';

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


    $sql="SELECT * FROM registration WHERE email='$email' and password='$password'";

    $result = $con->query($sql);
    $count = $result->num_rows;

    if($count==1){
            header("location:index.php");
    }
    else {
        echo "Wrong Username or Password";
    }

?>

The connection is ok as far as I know. This is db_connection.php code:

<?php
    $db_host = "localhost";
    $db_user = "root";
    $db_pass = "";
    $db_name = "tourism";

    $con = new mysqli($db_host, $db_user, $db_pass, $db_name);
    if($con->connect_error){
        die("something wrong");
    }
?>
RobC
  • 22,977
  • 20
  • 73
  • 80
zarin
  • 1
  • 1
  • `header('Location: ....')` capital `L` – RiggsFolly Apr 09 '18 at 17:00
  • 1
    What's onclick= "myFunction()" doing? (on the submit button) – Kyle B Apr 09 '18 at 17:01
  • Add `ini_set('display_errors', 1); ini_set('log_errors',1); error_reporting(E_ALL); mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);` to the top of your script. This will force any `mysqli_` errors to generate an Exception that you can see on the browser and other errors will also be visible on your browser. – RiggsFolly Apr 09 '18 at 17:02
  • 1
    **Never store plain text passwords!** Please use **[PHP's built-in functions](http://php.net/manual/en/function.password-hash.php)** 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. – John Conde Apr 09 '18 at 17:03
  • 2
    Your script is at risk of [SQL Injection Attack](//stackoverflow.com/questions/60174) Have a look at what happened to [Little Bobby Tables](http://bobby-tables.com/) Even [if you are escaping inputs, its not safe!](//stackoverflow.com/questions/5741187) Use [prepared parameterized statements](https://php.net/manual/en/mysqli.quickstart.prepared-statements.php). – John Conde Apr 09 '18 at 17:03
  • i used a alert on element. like as function myFunction(){ alert("You are Logged In."); } @KyleB – zarin Apr 09 '18 at 17:05
  • *"it staying in login.php and shows as blank page"* - That *very likely* means you're getting an error. In PHP it's often referred to as the "white screen of death". *"how to solve this problem?"* - Turn on error reporting and do some debugging to determine what the error is. Currently you're assuming that all of your code is working and that the problem *must* be in the line of code attempting to return a redirect header. On what do you base such an assumption? Do less assuming and more debugging. – David Apr 09 '18 at 17:05

0 Answers0