Get data from $ _SESSION

I am trying to create a simple server side check, this is my method

public function create() {
        $name = $_POST['name'];

        session_start();
        unset($_SESSION['errors']);

        $count = $this->model->checkIfUserExists($name);
        if($count > 0) {       
            $_SESSION['errors'] = array(
                'message'   => 'User Already Exists',
                'variables' => array(
                    'name'     => $_POST['name'],
                    'password' => $_POST['password'],
                ),
            );  

            header('location: ' . URL . 'user/registration');
            exit;
        }           

        $data = array();
        $data['name'] = $_POST['name'];
        $data['password'] = $_POST['password'];
        $this->model->create($data);            
        header('location: ' . URL);
    }

and below code from registration.php

<?php 

if (isset($_SESSION['errors']) && count($_SESSION['errors']) > 0) {

echo '<ul>';
foreach ($_SESSION['errors'] as $error) {
    echo '<li>' . $error['message'] . '</li>';
}
echo '</ul>';
unset($_SESSION['errors']);
}
?>

but i got errors

Warning: Illegal string offset 'message' in C:\xampp\htdocs\test\views\user\registration.php on line 6
 

Notice: Undefined index: message in C:\xampp\htdocs\test\views\user\registration.php on line 6

How to fix it?

+4
source share
3 answers

This usually Warning: Illegal string offset ...means that you are trying to access a string as an array .

You are currently setting $_SESSION['errors']to an associative array with two elements messageand variables. I believe that you are trying to create an array with several errors, each of which has messageand variables.

This type of configuration should be performed:

...

$_SESSION['errors'] = array();

if ($count > 0) {
    $_SESSION['errors'][] = array(
        'message' = > 'User Already Exists',
        'variables' = > array(
            'name' = > $_POST['name'],
            'password' = > $_POST['password'],
        ),
    );

    header('location: '.URL.'user/registration');
    exit;
}

...   

Empty square brackets add a new element to the array:

$myArray[] = $myNewElement;

:

$_SESSION['errors'][] = array(
    'message' = > 'Error two',
    'variables' = > array(...),
);
$_SESSION['errors'][] = array(
    'message' = > 'Error three',
    'variables' = > array(...),
);
+5

, foreach . [ "message", "variables" ]

$_SESSION['errors'] = : $_SESSION['errors'][] =

$_SESSION['errors'][] = array(
                    'message'   => 'User Already Exists',
                    'variables' => array(
                        'name'     => $_POST['name'],
                        'password' => $_POST['password'],
                    ),
                ); 
0

registration.php on first runsession_start()

<?php 
session_start();
if (isset($_SESSION['errors']) && count($_SESSION['errors']) > 0) {

echo '<ul>';
foreach ($_SESSION['errors'] as $error) {
    echo '<li>' . $error['message'] . '</li>';
}
echo '</ul>';
unset($_SESSION['errors']);
}
?>
0
source

Source: https://habr.com/ru/post/1627345/


All Articles