user9133983
user9133983

Reputation:

How to store different variables in session array

I'm making a basic Chat Box with PHP. Basically I don't want to use database connection for sending message to a user. Instead of that, I like storing different values that a user send in a session array.

For example take a a look at this:

<?php 
if (session_status() == PHP_SESSION_NONE) {
    session_start();
    $_SESSION['messages'] = array();  
}
if (isset($_POST['send'])){
    $pm = $_POST['message'];
    array_push($_SESSION['messages'], $pm); 
    $request_params = [
        'chat_id' => $id,
        'text' => implode(" ", $_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>

As you can see it basically store the message that user typed in a variable called $pm and by this sentence: 'text' => implode(" ", $_SESSION['messages']) it will simple push string value of $pm into $_SESSION['messages'].

Then I try to save the $id value (no need to include the code of id here, it is just an id of user) as chat_id and $_SESSION['messages'] as text.

Now in order to test this out I tried print_r($request_params); but it just shows only this:

Array ( [chat_id] => 108132368 [text] => )

As you can see it does not return the session variable which is $_SESSION['messages']).

So why it does not work ? How can I store different variables in a session array ?

UPDATE 1:

    <?php 
session_start();
if (session_status() == PHP_SESSION_NONE) {
    $_SESSION['messages'] = 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>

Upvotes: 0

Views: 111

Answers (2)

Nimeshka Srimal
Nimeshka Srimal

Reputation: 8950

Please change your code as below:

<?php

//check if a session has been defined earlier somewhere.
if (session_status() == PHP_SESSION_NONE)
    session_start();

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

if (isset($_POST['send'])){
   // your code...
}

Initially your problem was that you created a fresh array every time the form was submitted (sorry, I misunderstood your purpose earlier).

However, you should not be declaring the $_SESSION['messages'] inside the session_status() == PHP_SESSION_NONE condition. You need to see if $_SESSION['messages'] array is already defined, and if not, define that. Use an isset() to do that.

Hope it helps :)

Upvotes: 1

Professor Abronsius
Professor Abronsius

Reputation: 33823

I think the problem may lie here

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

The variable $_SESSION['messages'] is being overwritten on each page load so perhaps change to

session_start();
if( empty( $_SESSION['messages'] ) ) $_SESSION['messages'] = array(); 

Upvotes: 1

Related Questions