PHP global array as time table

Asked

Viewed 438 times

2

I’m in the middle of a problem, I’d like to use a array as a table temporary, sort of like this:

$array = [
    "codigo"    => "0123",
    "descricao" => "produto"
];

But I would need him to go global, But I have a lot of questions, can this be too heavy on the server? Global variables give conflicts between customers, or are like sessions, and are not shared between accesses?

And I’d also like to ask for an example of how to use global arrays.

Thanks in advance

  • 1

    vc define it in a specific file and make a include in the other files that use this array.

  • and in that case it will not lose the values that were entered by other pages?

  • Can its values change? if yes seems better to use session.

  • Yes they can change, Sessions would not get too heavy??

2 answers

1

It will weigh and is in danger even of overthrowing your server (I already made this mistake). Create a cookie with the data and use the cookie when you need it, or record it in a database. Session and global variables serve to facilitate maintenance and store configuration variables, so use only when needed.

  • I get it, I think I better search for temporary tables in Mysql so I think cookies are very dangerous, I find it kind of easy to be deleted

  • Do so, create a table in ,mysql with the data you need, and a creation date column, plus a column with the session id. Then do a data deletion routine by date, and the session id will give security that the data is right.

  • In my case, I have some tables that are children of another, and they are included during the filling of the parent table fields, and as I cannot include them in db before including the father, I am having these difficulties rsrs, and I wanted to avoid creating so many tables, since only in this case I have about 5 tables daughters. But I will study the best solution, and put it as an issue here

  • 1

    In this case you are probably doing gambiarra, try to study better the way the data are being included.

1

You can use a . txt file to save your data.

$nome_arquivo = 'arquivo_temp_'.time().'.txt';
$_SESSION['arquivo_temp'] = $nome_arquivo;

$fp = fopen($nome_arquivo,'w+');
fwrite($fp,$dados_a_armazenar);

You can save the data in file . txt and then recover the data when you want.

$nome_arquivo = $_SESSION['arquivo_temp'];
$fp = fopen($nome_arquivo,'r');

To delete the file you can delete when the user leaves the system, or it can create a function that deletes the files after X time.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.