Store old folder size in session php - php

I calculation a size of a folder using php and then store it in session, than i delete some files with other function and than calculate size of folder,
i want difference of both state old and new
But session stores every time new data and difference becomes 0
How do i maitain old size in session and prevent it from overwriting?
here is some part of code
$this->size_session = $currentsize;
$_SESSION['oldsize']=$this->size_session;
i calculate new size after submit button
if($_REQUEST['file_delete']):
$this->size_now = $currentsize;
$_SESSION['oldsize']- $this->size_now;
endif;

You should read the $_SESSION['oldsize'] to another variable BEFORE changing its value:
$old_size = $_SESSION['oldsize'];
if ($_REQUEST ... ):
(...)
endif;
$difference = $old_size - $_SESSION['oldsize'];
Only that $_SESSION['oldsize'] is not a very good name, shouldn't it be 'actualsize', since you use it to store the actual size? ;)

Not sure if you typed this example manually or copy/paste, but:
$_SESSION['oldsize']- $this->size_now;
Should probably look like this:
$difference = $_SESSION['oldsize'] - $this->size_now;

Related

setting Global variables or application values in laravel 5.3?

I want to assign a value as 0 by default, then every time that page is hit
I want to increase the value by one and when the value is 4 then I have to do some action on that page and change back the value of that variable to 0.
I want to you use global variables or application variables.
I don't want to use the database to save values. I tried my best to save the value in .env but read somewhere it's not the right way to do. Please guide me the best possible way? Working on Laravel 5.3
Thank you very much.
You can create a file in the config folder and store the option there. For example, in a file named system.php:
<?php
return [
"my_option" => 0,
];
Then you can have access to it calling get method from Config:
$myOption = \Config::get('system.my_option');
More info: https://laravel.com/docs/5.2/configuration
You can use session for this:
$counter = (int) session('counter', 0);
session(['error_pages' => ++$counter]);
if ($counter >= 4) {
// here you do what you want
}
Of course in case you want to use this mechanism not in single user session you can use cookies to store counter value.
You can use the config function, is less overhead than use the session:
config(['shared_variable'=>$value])

save a number from one webpage on the server side and being able to retrieve it

i have a website for E-invoice. I'm trying to find the best way where i can retrieve a number "in my case the invoice number" and when creating a new invoice the old number will be saved somewhere and retrieved to be +1.
for now i know that i can't use cookies or session of course and i can't use HTML5 local storage to save the last invoice number and i know that it have to be something on the server side.
can i use xml or save the value in a text file then retrieve it? can anyone please inform me of the best approach to do this.
here a possible solution (untested)
<?php
$file = fopen("number.txt","rw");
$number = fread($file);
$number += 1; //the new number to proceed
fwrite($file,$number);
fclose($file);
?>

how to use php to save old array and also show the new arrays?

I have two text boxes for first and last name and I want the first and last name to print out after submitting.
I wonder if there's an easy to to print out old and new arrays I entered....
for example:
I entered ABC ABC then after submitting it shows ABC ABC then I reload the page and type in CCC CCC and submitting but this time the print out is ABC ABC CCC CCC.
how can I do that?
this is what I have for my html scripts
<form action = "./3.php" method = "get">
First name: <input type = "text" name = "firstname">
Last name: <input type = "text" name = "lastname">
<input type = "submit">
</form>
this is my php scripts
<?php
$array = array($_GET["firstname"],$_GET["lastname"]);
foreach($array as $info)
{
echo $info . "<br/>";
}
?>
you could use $_SESSION[] to store values so that you can use it again until you done.
note: Using sessions may have effect that every time youre want your transactionsto be finish you must destroy session..
you may store the values in a table [firstname, lastname].
And every time you submit
insert new values
read all the values from the table.
display
Just to add to the other answers:
What you are looking for is called persistent data. You need some data to exist over a longer period of time than just an HTTP request (a page load). In order to do that, you have to store the data somewhere more permanent.
There are a few ways you can do this.
You can store the data in a database.
You can store the data in a file.
You can store the data in a cookie/session.
You're looking for the simplest and easiest solution - and that would be cookies/sessions. PHP offers built-in methods for dealing with cookies and sessions.
Using cookies/sessions is very similar to just creating a variable and storing the value you need in it, except that PHP saves this data onto a file (either on the client's machine or your server) and loads the data into the variables for you automatically before the script starts, so you don't have to think much about how to handle the file that's created.
In PHP, here's an example using cookies:
setcookie('TestCookie', $value, time()+3600*24);
This creates a cookie (a small file) on the client's machine, names it "TestCookie", stores the contents of $value in the file, and saves the file. PHP will automatically load that file into a global array named $_COOKIE, which you can use on any script in that domain e.g. www.domain.com for the next 24 hours.
echo $_COOKIE['TestCookie'];
Sessions work very similar to this, except they store the file on the server, which is a more secure, but slightly more complicated process.

PHP - How many times the page has been viewed

I want PHP to be able to echo the amount of times the page has been viewed. Being a server side scripting language I'm fairly confident there's a way.
This is what I'm thinking...
main.php
<body>
<?php
include("views.php");
$views = $views + 1;
echo $views;
?>
</body>
views.php
<?php $views = 0; ?>
This works, but does not update. (It will display 1, but will not keep counting upon refresh.)
The problem is that the variable $views does not persist from view to view. In fact, the next time someone comes back to your website $views would have been reset to 0. You'll need to take a look at some form of persistence to store the total number of views.
One way that you can accomplish this is to use a database or via a file. If you are using files, you can do the following inside of your views.php file.
views.php
$views = 0;
$visitors_file = "visitors.txt";
// Load up the persisted value from the file and update $views
if (file_exists($visitors_file))
{
$views = (int)file_get_contents($visitors_file)
}
// Increment the views counter since a new visitor has loaded the page
$views++;
// Save the contents of this variable back into the file for next time
file_put_contents($visitors_file, $views);
main.php
include("views.php");
echo $views;
You will need to store the data somewhere. Variables do not keep their state between requests. $views = 0 always means $views = 0, regardless of whether that variable is being included or not.
Write the number of views to a file (file_put_contents, file_get_contents) or to a database to store them permanently.
When you refresh the page, the state is not saved. This $views is set to 0 everytime you begin, and gets incremented by 1.
To increment the count and save the value, you will need to persist the number by either using a database or a file.
Great idea will be to use a database like MySQL. There are a lot of articles over the Internet how to set it up and use with PHP.
What you would probably want to do - update a page row in 'views' every time page is accessed. Simplest way is something like this:
<?php
/* don't forget to connect and select a database first */
$page = 'Home Page'; // Unique for every page
mysql_query("UPDATE views SET num = num + 1 WHERE page = '$page'");

Counter in ActionScript 3.0 with...PHP or?

I am doing this flash banners for multiple clients and one major request is to have some sort of counter so they know how many times the banner has been clicked.
I know how to do it in ActionScript 3.0, I make a simple var:int and i increase it +1 when a click is made on the banner. What do I do with the value of this var(say its 121) where do I store it online so its safe and can be changed by multiple flash banners(as3).
But how do I save this information so next time when the banner is loaded(on diffrent webpages) the number of clicks is whatever it was last time it was loaded.
Should I look into PHP for that ? I have no clue how to do this... some examples, tutorials, whatever works... would be much appreciated.(I am a designer, not programmer...please dont speak php-ish, or you know... :D)
I've googled a bit, and found some help, but i am still confused, and much of it its not AS3, I'm thinking maybe stuff has evolved a bit since the stuff that I found(2008)...
Thank you very much.
You'd have to store (and fetch) the value somewhere - either in the DB, in a text-file, ...
I'd go search for a tutorial on PHP+MySQL. If you don't like PHP-ish, you're probably better of finding another solution though :p
Example tutorial: http://www.freewebmasterhelp.com/tutorials/phpmysql
You need to store the data you want be retrievable/update-able from multiple clients, to be stored on a server.
You can use any server side language with a database.
Server Languages : PHP, ASP.net, JSP, ColdFusion
Database : MySQL, MSSQL, PostgreSQL, Oracle, DB2 etc..
Use whatever combination you are comfortable with.
In general:
You have a web app that increments the counter in the database
call the page using URLLoader from your AS3 banner.
Database
counter_table
-------------
counter INT
PHP File
$db = mysql_connect('localhost', 'mysql_user', 'mysql_password');
mysql_select_db('database_name');
mysql_query('UPDATE counter_table SET counter = counter + 1');
AS3 Banner
// url request with your php page address
var scriptRequest:URLRequest = new URLRequest("http://www.example.com/script.php");
// loader
var scriptLoader:URLLoader = new URLLoader();
// load page to trigger database update
scriptLoader.load(scriptRequest);
Do you also want to retrieve the value of the number of clicks in Banner ?
Easy solution (really not the best :) You should use one of the other answers.. anyways, make a php file that reads txt file containing the count of visits.. and in your flashbanner just call the php file. It'll add one hit per call..
PHP:
<?php
/**
* Create an empty text file called counterlog.txt and
* upload to the same directory as the page you want to
* count hits for.
*
*
* #Flavius Frantz: YOU DONT NEED THESE:
* Add this line of code on your page:
* <?php include "text_file_hit_counter.php"; ?>
*/
// Open the file for reading
$fp = fopen("counterlog.txt", "r");
// Get the existing count
$count = fread($fp, 1024);
// Close the file
fclose($fp);
// Add 1 to the existing count
$count = $count + 1;
// Display the number of hits
// If you don't want to display it, comment out this line
//echo "<p>Page views:" . $count . "</p>";
// Reopen the file and erase the contents
$fp = fopen("counterlog.txt", "w");
// Write the new count to the file
fwrite($fp, $count);
// Close the file
fclose($fp);
?>
Example code from: (google: php counter file) http://www.totallyphp.co.uk/text-file-hit-counter
Code is not tested, but looks ok. I only commented just a little..

Categories