O
O
omerkhan2021-08-23 08:53:06
PHP
omerkhan, 2021-08-23 08:53:06

How to fix "Undefined array key "login" in" in sessions, PHP?

Hello. I got an error. I work with sessions and I want to make sure that when a person logs in, his login and password do not disappear from the "login" and "password" fields. But I get an error - the login and pass sessions are undefined. But if you log in, then all this will be displayed in the field. But still this mistake hurts the eye . Wanted to fix this issue with:

<?php
if($_SESSION['login'] == '' || $_SESSION['pass']):
?>
<?php
else:
?>
<?php
endif;
?>

Everything seems to be working. But it throws an error
Undefined array key "login" in C:\OpenServer\domains\filephp\reg.php on line 15.
Here is the code:
<b>reg.php:</b>
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <link rel="stylesheet" href="css/style.css">
  <title>Document</title>
</head>
<body>
  <?php 
    require_once 'header.html';
    session_start();
  ?>
  <?php
    if($_SESSION['login'] == '' || $_SESSION['pass'] == ''):
  ?>
  <div class="col">
 		<div class="row">
 			<h1>Регистрация</h1>
      <form method = "post" action = "check_reg.php">
        <input type = "text" name = "login" placeholder = "Ваш логин">
        <input type = "password" name = "pass" placeholder = "Ваш пароль">
        <input type = "submit" value = "Зарегистрироваться">
      </form>
    </div>
    <div class="row" style = "float: right; margin-top: -212px">
      <h1>Авторизация</h1>
      <form method = "post" action = "login.php">
        <input type = "text" name = "login" placeholder = "Ваш логин" value = >
        <input type = "password" name = "pass" placeholder = "Ваш пароль">
        <input type = "submit" value = "Войти">
      </form>
    </div>
  </div>
  <?php
    else:
  ?>
  <div class="col">
    <div class="row">
      <h1>Регистрация</h1>
      <form method = "post" action = "check_reg.php">
        <input type = "text" name = "login" placeholder = "Ваш логин">
        <input type = "password" name = "pass" placeholder = "Ваш пароль">
        <input type = "submit" value = "Зарегистрироваться">
      </form>
    </div>
    <div class="row" style = "float: right; margin-top: -212px">
      <h1>Авторизация</h1>
      <form method = "post" action = "login.php">
        <input type = "text" name = "login" placeholder = "Ваш логин" value = "<?=?>">
        <input type = "password" name = "pass" placeholder = "Ваш пароль">
        <input type = "submit" value = "Войти">
      </form>
    </div>
  </div>
<?php endif;?>
  <?php
    require_once 'footer.html';
  ?>
</body>
</html>
<b>check_reg.php:</b>
<?php
  session_start();
  $login = htmlspecialchars(trim($_POST['login']));
  $pass = htmlspecialchars(trim($_POST['pass']));
  if(strlen($login) < 6)
    echo 'Логин не менее 6 символов';
  else if(strlen($pass) < 8)
    echo 'Пароль не менее 8 символов';
  $mysql = new mysqli('localhost', 'root', '', 'register');
  $mysql -> query("INSERT INTO `users` (`login`, `pass`) VALUES('$login', '$pass')");
  $_SESSION['login'] = $user['login'];
  $_SESSION['pass'] = $user['pass'];
  $mysql -> close();
  header('Location: reg.php');
  exit();
?>

screen
612337c03bc0f146681910.jpeg

Answer the question

In order to leave comments, you need to log in

3 answer(s)
R
Rsa97, 2021-08-23
@Rsa97

if (($_SESSION['login'] ?? '') === '');

S
Sergey delphinpro, 2021-08-23
@delphinpro

Check for the existence of a key array_key_exists

J
John Didact, 2021-08-23
@JohnDidact

<?php
session_start();
require_once 'header.html';
if(!isset($_SESSION['login'], $_SESSION['pass']) || $_SESSION['login'] == '' || $_SESSION['pass'] == ''):
?>
Регистрация
Авторизация
<?php
else:
?>
Регистрация
Авторизация
<?php
endif;
require_once 'footer.html';
?>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question