I am new to PHP. I am doing a little project (where I read data, a password, from a database) to learn PHP and I am using some variables that I obtain in a form in different PHP files. I thought I should use session_start() and the global variable $_SESSION to use the same variables in different PHP files. Below it is my code, I have this files in one folder: index.php(HTML and forms), conn.php(connection to the database), login.php (file where I read data from database)
Can someone explain to me why I don't need to use session_start() in this case and in what specific situations I need to use the global variable $_SESSION?
index.php:
<?php
include_once "conn.php";
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<form id="login" method="POST" action="login.php">
<label for="loginUsername">Username</label>
<input type="text" id="loginUsername" name="loginUsername">
<br>
<label for="loginPassword">Password</label>
<input type="password" name="loginPassword" id="loginPassword"><br>
<input type="submit" name="login" value="Login">
</form>
</body>
</html>
conn.php:
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "loginsystem";
$conn = mysqli_connect($servername, $username, $password, $dbname);
login.php:
<?php
include_once "conn.php";
if(isset($_POST["login"])){
$username = $_POST["loginUsername"];
$password = $_POST["loginPassword"];
echo $password;
$sql = "SELECT password FROM users WHERE username=?;";
$stmt = mysqli_stmt_init($conn);
mysqli_stmt_prepare($stmt, $sql);
mysqli_stmt_bind_param($stmt, 's', $username);
mysqli_stmt_execute($stmt);
$result = mysqli_stmt_get_result($stmt);
while($row=mysqli_fetch_assoc($result)){
echo $row["password"];
}
}