FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


Saving a variable in php





speeDemon
I know methods to save values in tables inside MySQL databases using php, but is there any other simple way to save, say, a single variable/value?

Like for instance, when someone presses a download link/button, I can just access this variable and increase it's value by 1, and when the page loads, then just echo out the value saying "This has been downloaded $X no. of times"

or is my only option to make a new table for this simple purpose?
jmraker
You could save the value in a text file with

Code:
$filename = 'pageCounts/page1.txt';
if(file_exists($filename))
  $counter = (int)file_get_contents($filename);
else
  $counter = 0;
file_put_contents($filename, $counter . '', LOCK_EX);


But you'd need to set the directory permissions of the pageCounts so the program can read and write to files, also there's no guarantee that 2 users accessing the page at the same time would always add 2. (the 2 instances could read the file at the same time and have the same value in $counter). If that worries you, you'd have to use something like flock to lock the file and probably loop until it's unlocked.
Fire Boar
Flat file (what jmraker suggested) and database are pretty much your only two options here. For single values I'd usually suggest just having a table "variables" with two columns: "key" and "value". That way, you could store an arbitrary number of single values... though this number should ideally be constant in the normal operation of your site. If it increases as content is being added, you should probably be storing the value along with the content. For instance, downloading a file, you'd usually keep the download count in the table listing all the files available for download.
jmraker
One of the downsides of storing info in a file (that's accessible by a web server) is that unless the web server is configured to block it anyone could view the files if they can figure out the url or know it's there, like if they see the filename in an error message

If you store the files in the userData/ directory they could enter http://www.site.com/userData and the web server could list the files. If it doesn't list them, they might be able to enter userData/user1.txt and see the file's contents.

But if you're just doing page counts, they'd just see a number
rvec
Saving a value to a mysql database is much faster than opening a file. Especially if you already have an open connection (for some other variable).
Lucifer_iix
Code:
session_start();


For starting your session

Doc: http://nl2.php.net/manual/en/function.session-start.php


And your array is than: $_SESSION[$MYVar] = $Value;

Doc: http://nl2.php.net/manual/en/reserved.variables.php
http://nl2.php.net/manual/en/reserved.variables.session.php

2 --- 3 to go
Related topics
EDIT: document root
i need this
Variable Name With A Variable in it. Is It possible?
Admin-Center voor website
Editing files online
i need this,pls help
Software development algorithms
How To : Improve Your PHP Programming
PHP Tutorials
storing current page address as php variable
Need Help integrating php variable into mySQL SELECT query
Assigning a variable in PHP by address bar
Getting PHP variable to work in FCKeditor
send variable from php tp php file
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.