Page 1 of 1

Counting data from users and decreasing their money balance?

Posted: Tue Jul 04, 2006 3:45 am
by lip9000
I wish to create a site that requires people to pay to be able to view its content. But I want it so that people can purchase, for example $15.00 worth of time, and they get 30MB worth of traffic to view that site, $0.50c per MB. They will be able to login to their given account to access the site.

Is there a PHP function or command that counts the amount of bytes the user is receiving from the webpage they are on, and thus subtracting from their account balance which will be stored in a field under their username in MySQL?

The same sort of system is found in schools and universities where students receive an account balance each month and they can only use that much moneys worth of bandwidth in the month, otherwise they will get a msg saying, "you have run out of credit", or something like that.

If you have any ideas, please post, many thanks.

Posted: Tue Jul 04, 2006 5:10 am
by Jenk
you can use the output buffer to find the size (length) of output:

Code: Select all

<?php
ob_start();

echo 'Hello world!';

$size = ob_get_length();

ob_end_flush();
?>
each char is one byte.

Though if you output anything outside of the output buffer, these will not be included, e.g.:

Code: Select all

/* ignore this line */ ?>
<html>
</body>
<?php
ob_start();

echo 'Hello!';

$size = ob_get_length(); // 6

ob_end_flush();
?>
</body>
</html>
Or you can structure your pages to store all output into one variable, then echo it at the end.

ala:

Code: Select all

<?php

$output = "<html>\n<body>\n";

for ($i = 0; $i < 5; $i++) {
    $output .= "<p>This is line number: $i</p>\n";
}

$output .= "</body>\n</html>";

$size = strlen($output);

echo $output;

?>
Again, each char is one byte.

Posted: Tue Jul 04, 2006 9:00 am
by lip9000
so then all I would need to do is get $output and convert that into megabytes somehow and then multiply that by $0.50c to the MB, then subtract that from the 'credit' field in the database on that user?

Posted: Tue Jul 04, 2006 9:04 am
by Jenk
Yup :)

Though because pages of html are less than a meg, it would be better to just subtract from a total number of bytes in a DB field, then calculate remaining allowance from that, rather than concentrate of monetary value.

Let's say for example they have 10MB to play with.. they open a page of (random number) 24kB.

(10MB = 10485760 bytes)

Code: Select all

<?php

$total = 10485760; //number is retrieved from database

$size = 24576; //number is calculated like previous examples

$total = ($total - $size);

$allowance = round(((($total / 1024) / 1024) * 0.5), 2);

echo "Your remaining allowance is \${$allowance}"; //$4.99

//insert new total into DB

?>

Posted: Tue Jul 04, 2006 10:42 pm
by lip9000
Thanks mate :D