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.
Counting data from users and decreasing their money balance?
Moderator: General Moderators
you can use the output buffer to find the size (length) of output:
each char is one byte.
Though if you output anything outside of the output buffer, these will not be included, e.g.:
Or you can structure your pages to store all output into one variable, then echo it at the end.
ala:
Again, each char is one byte.
Code: Select all
<?php
ob_start();
echo 'Hello world!';
$size = ob_get_length();
ob_end_flush();
?>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>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;
?>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)
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
?>