简体   繁体   English

如何在会话数组中存储不同的变量

[英]How to store different variables in session array

I'm making a basic Chat Box with PHP. 我正在用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 aa 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'] . 如您所见,它基本上存储了用户在名为$pm的变量中键入的message ,并通过以下语句: 'text' => implode(" ", $_SESSION['messages'])它将简单地推送$pm 字符串值到$_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 . 然后,我尝试将$id值(无需在此处包含id的代码,它只是用户的id)保存为chat_id$_SESSION['messages']作为text

Now in order to test this out I tried print_r($request_params); 现在为了测试这一点,我尝试了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']) . 如您所见,它不会返回会话变量$_SESSION['messages'])

So why it does not work ? 那么为什么它不起作用? How can I store different variables in a session array ? 如何在会话数组中存储不同的变量?

UPDATE 1: 更新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>

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['messages']在每次加载页面时都会被覆盖,因此可能更改为

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

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. 但是,您不应该在session_status() == PHP_SESSION_NONE条件内声明$_SESSION['messages'] You need to see if $_SESSION['messages'] array is already defined, and if not, define that. 您需要查看是否已经定义了$_SESSION['messages']数组,如果没有定义,请进行定义。 Use an isset() to do that. 使用isset()可以做到这一点。

Hope it helps :) 希望能帮助到你 :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM