-3

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 ?

6
  • This means that $_SESSION['messages'] is not defined Commented Dec 23, 2017 at 16:45
  • But I have defined as you can see at the top of my code. Commented Dec 23, 2017 at 16:48
  • I suppose that you don't. Commented Dec 23, 2017 at 16:49
  • @u_mulder What do u mean by I suppose that you don't ?! I have pasted my code here and it is the way it is. Explain me what is your talking about man Commented Dec 23, 2017 at 16:56
  • 1
    If you think about your code you can understand that maybe session_status() == PHP_SESSION_NONE is false and your code inside if is not executed. Isn't it? Commented Dec 23, 2017 at 16:58

1 Answer 1

2

You get the error : Warning: array_push() expects parameter 1 to be array, null given

because your $_SESSION['messages'] variable is never set.

When this part of your code gets executed,

session_start();
if (session_status() == PHP_SESSION_NONE) {
    $_SESSION['messages'] = array();  
}

The first line starts a new session. So session_status() == PHP_SESSION_NONE will never be true since session_status() returns 2 and PHP_SESSION_NONE equals to 1. As a result, $_SESSION['messages'] = array(); will not get executed.

What you need to be doing is, check if a session has been started, and start one if not.

if (session_status() == PHP_SESSION_NONE) {
    session_start();
}

This will check if you have a session_start() called somewhere earlier in your script.

Then after that, add this line:

if(!isset($_SESSION['messages']))
    $_SESSION['messages'] = array();

Hope it helps.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.