3
votes

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.

2
What errors you are facing? - Zain Farooq
no errors are comes but ts doesn't go to the dashboard page - Krishnan R
It seems like you have saved your php file in BOM - Zain Farooq
Sorry I didn't understand - Krishnan R
They should look like as $row['id']; and $row['username']; - Zain Farooq

2 Answers

1
votes

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

1
votes
 <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">