I have a form with two buttons, login Button wich load main page and the register Button wich load register page and I created sessions to verify permissions. Only admin user can load the register page. The problem is I'm having an error of undefined variable -> Notice: Undefined variable: _SESSION in C: \ xampp \ htdocs \ test \ register.php on line 23.
In the php code below I have 2 main "if" conditions, the first if is to loginButton wich works perfectly without any error. The second "if" is to the registerButton althought load the page, session variable is not being set. Although I see the error I don't know what is causing becouse both codes are simillar.
Here is the form code:-
<form action="login.php" method="POST" id="form1"><div class="field"><input name="user" class="input is-large" placeholder="Enter your user name" autofocus="" required><input name="password" class="input is-large" type="password" placeholder="Password" required><button type="submit" name="loginButton" class="button is-block is-link is-large is-fullwidth">Login</button><br><br><button type="submit" name="registerButton" id="register" class="button is-block is-success is-large is-fullwidth">Register</button></div></form>
Here is the php code:-
<php
session_start();
include('conexao.php');
if(empty($_POST['usuario']) || empty($_POST['senha'])) {
header('Location: index.php');
exit();
}
$usuario = mysqli_real_escape_string($conexao, $_POST['usuario']);
$senha = mysqli_real_escape_string($conexao, $_POST['senha']);
$query = "select * from usuario where usuario = '{$usuario}' and senha = '{$senha}'";
$result = mysqli_query($conexao, $query);
$row = mysqli_fetch_array($result);
$user = $row['usuario'];
$categoria = $row['categoria'];
if (isset($_POST['loginButton'])) { // working without error
if ($user === $usuario) {
$_SESSION['usuario'] = $usuario;
header('Location: Draft.php');
exit();
}else{
$_SESSION['nao_autenticado'] = true;
header('Location: index.php');
exit();
}
}
if (isset($_POST['registerButton'])) {
if($categoria == 'admin'){
$_SESSION['categoria'] = $categoria; //problem
header('Location: cadastro.php');
exit();
}else{
$_SESSION['nao_administrador'] = true; //problem
header('Location: index.php');
exit();
}
}