1

I want to store a single integer like so:

<?php

$_SERVER['amount'] = 54;
echo($_SERVER['amount']);

?>

And be able to modify it, as well as be accessed from every new php session. However whenever the session ends the server variable disappears. How can i store a single variable on the server without a database? A .txt file seems kinda unnecessary for 2 characters stored.

12
  • 1
    Are you sure you didn't mean to use $_SESSION? Also you don't need brackets to echo variables. You have two storage options really, cookies/sessions or a SQL database. The latter is over kill for as you said just 2 characters. Commented Oct 30, 2014 at 22:35
  • Doesn't the session end when the browser gets closed? Commented Oct 30, 2014 at 22:36
  • I want the variable to be static just like a text file in FTP. Commented Oct 30, 2014 at 22:36
  • Not if you put this before your use of sessions session_start(); you can set the lifespan of them. Commented Oct 30, 2014 at 22:36
  • Okay but i don't want the variable to be unique to each session. User A and user B should both see the same variable. Will setting it as a session variable allow that? maybe there's something I'm not understanding about sessions Commented Oct 30, 2014 at 22:37

2 Answers 2

4

Your storage options are a file, a hardcoded variable in the PHP code, a database table, a cookie or a session variable, as I understand it. Probably the most elegant solution if you already are using a database is to add a new table with your permanent data variables.

Another solution, if you are looking for a quick and dirty solution, is to add a global php variable with a magic number, which is really what you are trying to do with your server variable, eg global $_AMOUNT = 54; // The amount is always 54 for all users. That doesn't really meet your requirement of being able to modify it each time the page is accessed, though.

$_SERVER is a superglobal, which is read from a file each time PHP is initiated on every pageload. You are not writing to the file that the variable is read from, and so it resets each time the script executes.

Just make a database table, in my opinion. Make one row for the table, amount. I am willing to bet the table will grow over time as you add more global variables.

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

Comments

4

You could use file_put_contents and create a kind of cache file then use file_get_contents to retrieve the data..

file_get_contents AND file_put_contents

something like

$data = array(
    'something' => 'this',
    'somethingelse' => 'again',
);

$settings = json_encode($data);

file_put_content( 'settings.txt', $settings);

//then
$texstSettings = file_get_contents('settings.txt' );
$settings = json_decode(texstSettings);

2 Comments

unless you want to use jquery local storage or something. but that solution is specific to the user. if you wanna use sqlite or something that maybe more then file_put_contents but thats really the only thing you could probably do.
IMO this is way overboard for what he's asking to do.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.