I'm trying to save some values from a variable into my session array with php.
Here is my code:
<?php
session_start();
if (session_status() == PHP_SESSION_NONE) {
$_SESSION['messages'] = array();
}
$request_params = array();
if (isset($_POST['send'])){
$pm = $_POST['message'];
array_push($_SESSION['messages'], $pm);
$request_params = [
'chat_id' => $id,
'text' => implode(" ", $_SESSION['messages'])
];
echo $_SESSION['messages'];
print_r($request_params);
}
?>
<div class="box-footer">
<form action="" method="post">
<div class="input-group">
<input type="text" name="message" placeholder="Write your direct message" class="form-control">
<span class="input-group-btn">
<input name="send" type="submit" class="btn btn-danger btn-flat"/>
</span>
</div>
</form>
</div>
And these are the errors that appear when I try to submit the form:
Warning: array_push() expects parameter 1 to be array, null given in new.php on line 9
Warning: implode(): Invalid arguments passed in new.php on line 12
Line 9:
array_push($_SESSION['messages'], $pm);
And line 12:
'text' => implode(" ", $_SESSION['messages'])
So how to solve these issues ?
$_SESSION['messages']is not definedsession_status() == PHP_SESSION_NONEis false and your code insideifis not executed. Isn't it?