0

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>
1
  • Its work fine and give output Array ( [chat_id] => 1 [text] => ss ) Commented Dec 23, 2017 at 14:33

2 Answers 2

1

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(); 
Sign up to request clarification or add additional context in comments.

Comments

1

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 :)

6 Comments

can you please add the updated code so we can see what's wrong?
Sure, checkout UPDATE 1.
Hi, you can remove the updated code, it's not necessary.
If you still have the issue after adding this code, close the browser and open it again. (or else call to session_destroy(); towards the end of your script, load the page, then remove the line, and load the page again.)
I Posted a new question about this, please view it here
|

Your Answer

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