3

I have just finished creating an entire login and register systsem in PHP, but my problem is I haven't used any sessions yet. I'm kind of a newbie in PHP and I've never used sessions before. Want to log in with session and some if errors in code cannot go to the dashboard page help me to solve this problem.

<?php
session_start();
error_reporting(0);
include('config.php');
if(isset($_POST['submit']))
{
$result = mysqli_query($dbh,"SELECT * FROM users WHERE email='" . $_POST["email"] . "' and password = '". $_POST["password"]."'");
$row  = mysqli_fetch_array($result);
if(is_array($row)) {
$_SESSION["id"] = $row[id];
$_SESSION["username"] = $row[username];
} else {
$message = "Invalid Email or Password!";
}
}
if(isset($_SESSION["id"])) {
header("Location:dashboard.php");
}
?>

Want to log in with session and some if errors in code cannot go to the dashboard page help me to solve this problem.

Zain Farooq
  • 2,956
  • 3
  • 20
  • 42
Krishnan R
  • 45
  • 7

2 Answers2

1
 <input type="password" name="pass" class="form-control" placeholder="Password">

pass should be password. I mean your frontend html doesn't confirm with php . Use this instead

 <input type="password" name="password" class="form-control" placeholder="Password">
Pradeep Singh
  • 432
  • 5
  • 11
  • No both are different – Krishnan R May 24 '19 at 11:38
  • Can this solve your problem...https://www.w3schools.com/php/func_mysqli_fetch_array.asp – Pradeep Singh May 25 '19 at 23:23
  • Instead of using "Select * ...." please use "Select id,username, email, password from ...". This confirms the ordering of the fields, incase you use index. Further if you have access to phpMyAdmin in your localhost try a sample query like this there. "Select * from users;". Because it is difficult to guess what you know and what you don't, it's difficult to find where the exact problem is. – Pradeep Singh May 26 '19 at 00:38
1

You are passing constants in the indexes of the array $row. Change your code from this

$_SESSION["id"] = $row[id];
$_SESSION["username"] = $row[username];

To this

$_SESSION["id"] = $row['id'];
$_SESSION["username"] = $row['username'];

Your code is wide open to sql injection. I recommend you to use prepared statements as you are a newbie so its better for you to spend your energies in the right direction

Zain Farooq
  • 2,956
  • 3
  • 20
  • 42