why does a file run more then once

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
nite4000
Forum Contributor
Posts: 209
Joined: Sun Apr 12, 2009 11:31 am

why does a file run more then once

Post by nite4000 »

I have a file included and thing it it should only run one time if the variables are there if not then it should not run but what happeneing is

That its a file that gives commision and when its been given thats good but its calling it again if i refresh the page.

I have tried the require
require_once
include
and include_once but nothing stops it

so how do i have it call the file one time?
User avatar
Jade
Forum Regular
Posts: 908
Joined: Sun Dec 29, 2002 5:40 pm
Location: VA

Re: why does a file run more then once

Post by Jade »

Are you storing the variables in sessions? Can you post your code?
nite4000
Forum Contributor
Posts: 209
Joined: Sun Apr 12, 2009 11:31 am

Re: why does a file run more then once

Post by nite4000 »

do you want the code from the index page where it is ran or do you wanan see the code from the referral page that gives the commision?
User avatar
Jade
Forum Regular
Posts: 908
Joined: Sun Dec 29, 2002 5:40 pm
Location: VA

Re: why does a file run more then once

Post by Jade »

Both.
nite4000
Forum Contributor
Posts: 209
Joined: Sun Apr 12, 2009 11:31 am

Re: why does a file run more then once

Post by nite4000 »

Here is the php code in index.

Code: Select all

<?php
session_start();
		require_once('../inc/db.php');
$dbc = db_connect();
		require_once('../inc/check_login.php');
		require_once('../inc/initialize.inc.php');
		include('../inc/functions.php');
include('referral.php');


?>
here is the code from referral

Code: Select all

<?php
session_start();
		require_once('../inc/db.php');
$dbc = db_connect();
		require_once('../inc/check_login.php');
		require_once('../inc/functions.php');
		require_once('../inc/initialize.inc.php');

//Commision precentages for each referel level
$commis_lvl1_prc = $r_settings['top_level']; //0.08;
$commis_lvl2_prc = $r_settings['level_2'];//0.05;
$commis_lvl3_prc = $r_settings['level_3'];//0.04; //$5 * 0.04 = 0.20
$commis_lvl4_prc = $r_settings['level_4'];//0.03;
$commis_lvl5_prc = $r_settings['level_5'];//0.02;
$commis_lvl6_prc = $r_settings['level_6'];//0.01;

// Get referrals data
$reffs = mysql_query("SELECT * FROM referrals WHERE username='{$_SESSION['admin_user']}'")or die(mysql_error());
while($myinfo=mysql_fetch_array($reffs))
{
$username=$myinfo["username"];
$referral=$myinfo["referral"];
$level_2=$myinfo["level_2"];
$level_3=$myinfo["level_3"];
$level_4=$myinfo["level_4"];
$level_5=$myinfo["level_5"];
$level_6=$myinfo["level_6"];
$commis_lvl_1=$myinfo["commis_lvl_1"];
$commis_lvl_2=$myinfo["commis_lvl_2"];
$commis_lvl_3=$myinfo["commis_lvl_3"];
$commis_lvl_4=$myinfo["commis_lvl_4"];
$commis_lvl_5=$myinfo["commis_lvl_5"];
$commis_lvl_6=$myinfo["commis_lvl_6"];

// Get accounts data
$depos = mysql_query("SELECT * FROM accounts")or die(mysql_error());
while($mydeps=mysql_fetch_array($depos))
{
$dpuser=$mydeps["username"];
$deposit=$mydeps["total_deposit"];

        if ($level_6 != NULL){
        $commis_lvl_6=$commis_lvl6_prc * $deposit;
        }
        if ($level_5 != NULL){
        $commis_lvl_5=$commis_lvl5_prc * $deposit;
        }
        if ($level_4 != NULL){
        $commis_lvl_4=$commis_lvl4_prc * $deposit;
        }
        if ($level_3 != NULL){
        $commis_lvl_3=$commis_lvl3_prc * $deposit;
        }
        if ($level_2 != NULL){
        $commis_lvl_2=$commis_lvl2_prc * $deposit;
        }
        if ($referral != NULL){
        $commis_lvl_1=$commis_lvl1_prc * $deposit;
        }

        $r=mysql_query("UPDATE referrals SET commis_lvl_1='$commis_lvl_1' WHERE username ='$username' AND '$dpuser' = '$referral'")or die(mysql_error());
        $r=mysql_query("UPDATE referrals SET commis_lvl_2='$commis_lvl_2' WHERE username ='$username' AND '$dpuser' = '$level_2'")or die(mysql_error());
        $r=mysql_query("UPDATE referrals SET commis_lvl_3='$commis_lvl_3' WHERE username ='$username' AND '$dpuser' = '$level_3'")or die(mysql_error());
        $r=mysql_query("UPDATE referrals SET commis_lvl_4='$commis_lvl_4' WHERE username ='$username' AND '$dpuser' = '$level_4'")or die(mysql_error());
        $r=mysql_query("UPDATE referrals SET commis_lvl_5='$commis_lvl_5' WHERE username ='$username' AND '$dpuser' = '$level_5'")or die(mysql_error());
        $r=mysql_query("UPDATE referrals SET commis_lvl_6='$commis_lvl_6' WHERE username ='$username' AND '$dpuser' = '$level_6'")or die(mysql_error());
   }
}

// Update accounts
$reffs2 = mysql_query("SELECT * FROM referrals WHERE username='{$_SESSION['admin_user']}'")or die(mysql_error());
while($myinfo2=mysql_fetch_array($reffs2))
{
$earnuser=$myinfo2["username"];
$commis_ear_1=$myinfo2["commis_lvl_1"];
$commis_ear_2=$myinfo2["commis_lvl_2"];
$commis_ear_3=$myinfo2["commis_lvl_3"];
$commis_ear_4=$myinfo2["commis_lvl_4"];
$commis_ear_5=$myinfo2["commis_lvl_5"];
$commis_ear_6=$myinfo2["commis_lvl_6"];
$earned_total = $commis_ear_1 + $commis_ear_2 + $commis_ear_3 + $commis_ear_4 + $commis_ear_5 + $commis_ear_6;


[b]// I think its this code here
    $r=mysql_query("UPDATE accounts SET acct_balance=acct_balance + '$earned_total' WHERE username ='$earnuser' LIMIT 1")or die(mysql_error());
    $r=mysql_query("UPDATE accounts SET earned_total=earned_total + '$earned_total' WHERE username ='$earnuser'  LIMIT 1")or die(mysql_error());[/b]}
?>

User avatar
Jade
Forum Regular
Posts: 908
Joined: Sun Dec 29, 2002 5:40 pm
Location: VA

Re: why does a file run more then once

Post by Jade »

Okay so the problem is you're not checking if they've already had a commission. The solution is to:

1) Update your database so you keep record of whose earned a commission today and only let them earn commissions every X days. That means at the top of your commission script you'd have to check to see if this user has earned a commission already or not.

2) You can set a session variable once they've earned a commission and then prevent them from earning one again as long as that session variable is set.

I would recommend putting both of these practices into place to ensure they don't get multiple commissions. Your updated script would look something like this:

Code: Select all

<?php
session_start();
                require_once('../inc/db.php');
$dbc = db_connect();
                require_once('../inc/check_login.php');
                require_once('../inc/functions.php');
                require_once('../inc/initialize.inc.php');

$days_per_commission = 30;

//check to see if they have a record in the database
$result = mysql_query("SELECT DATEDIFF(last_commission_date, CURDATE()) as days FROM referrals WHERE username='{$_SESSION['admin_user']}'")
or die ('cannot select last referral date');

$row = mysql_fetch_assoc($result);

if ($_SESSION['commission_today'])
{
  echo "You've already earned your commission today.";
}
else if ($row['days'] < $days_per_commission)
{
    echo "You can only earn commissions every $days_per_commission days";
}
else //give them the commission
{

//Commision precentages for each referel level
$commis_lvl1_prc = $r_settings['top_level']; //0.08;
$commis_lvl2_prc = $r_settings['level_2'];//0.05;
$commis_lvl3_prc = $r_settings['level_3'];//0.04; //$5 * 0.04 = 0.20
$commis_lvl4_prc = $r_settings['level_4'];//0.03;
$commis_lvl5_prc = $r_settings['level_5'];//0.02;
$commis_lvl6_prc = $r_settings['level_6'];//0.01;

// Get referrals data
$reffs = mysql_query("SELECT * FROM referrals WHERE username='{$_SESSION['admin_user']}'")or die(mysql_error());
while($myinfo=mysql_fetch_array($reffs))
{
$username=$myinfo["username"];
$referral=$myinfo["referral"];
$level_2=$myinfo["level_2"];
$level_3=$myinfo["level_3"];
$level_4=$myinfo["level_4"];
$level_5=$myinfo["level_5"];
$level_6=$myinfo["level_6"];
$commis_lvl_1=$myinfo["commis_lvl_1"];
$commis_lvl_2=$myinfo["commis_lvl_2"];
$commis_lvl_3=$myinfo["commis_lvl_3"];
$commis_lvl_4=$myinfo["commis_lvl_4"];
$commis_lvl_5=$myinfo["commis_lvl_5"];
$commis_lvl_6=$myinfo["commis_lvl_6"];

// Get accounts data
$depos = mysql_query("SELECT * FROM accounts")or die(mysql_error());
while($mydeps=mysql_fetch_array($depos))
{
$dpuser=$mydeps["username"];
$deposit=$mydeps["total_deposit"];

        if ($level_6 != NULL){
        $commis_lvl_6=$commis_lvl6_prc * $deposit;
        }
        if ($level_5 != NULL){
        $commis_lvl_5=$commis_lvl5_prc * $deposit;
        }
        if ($level_4 != NULL){
        $commis_lvl_4=$commis_lvl4_prc * $deposit;
        }
        if ($level_3 != NULL){
        $commis_lvl_3=$commis_lvl3_prc * $deposit;
        }
        if ($level_2 != NULL){
        $commis_lvl_2=$commis_lvl2_prc * $deposit;
        }
        if ($referral != NULL){
        $commis_lvl_1=$commis_lvl1_prc * $deposit;
        }

        $r=mysql_query("UPDATE referrals SET commis_lvl_1='$commis_lvl_1' WHERE username ='$username' AND '$dpuser' = '$referral'")or die(mysql_error());
        $r=mysql_query("UPDATE referrals SET commis_lvl_2='$commis_lvl_2' WHERE username ='$username' AND '$dpuser' = '$level_2'")or die(mysql_error());
        $r=mysql_query("UPDATE referrals SET commis_lvl_3='$commis_lvl_3' WHERE username ='$username' AND '$dpuser' = '$level_3'")or die(mysql_error());
        $r=mysql_query("UPDATE referrals SET commis_lvl_4='$commis_lvl_4' WHERE username ='$username' AND '$dpuser' = '$level_4'")or die(mysql_error());
        $r=mysql_query("UPDATE referrals SET commis_lvl_5='$commis_lvl_5' WHERE username ='$username' AND '$dpuser' = '$level_5'")or die(mysql_error());
        $r=mysql_query("UPDATE referrals SET commis_lvl_6='$commis_lvl_6' WHERE username ='$username' AND '$dpuser' = '$level_6'")or die(mysql_error());
   }
}

// Update accounts
$reffs2 = mysql_query("SELECT * FROM referrals WHERE username='{$_SESSION['admin_user']}'")or die(mysql_error());
while($myinfo2=mysql_fetch_array($reffs2))
{
$earnuser=$myinfo2["username"];
$commis_ear_1=$myinfo2["commis_lvl_1"];
$commis_ear_2=$myinfo2["commis_lvl_2"];
$commis_ear_3=$myinfo2["commis_lvl_3"];
$commis_ear_4=$myinfo2["commis_lvl_4"];
$commis_ear_5=$myinfo2["commis_lvl_5"];
$commis_ear_6=$myinfo2["commis_lvl_6"];
$earned_total = $commis_ear_1 + $commis_ear_2 + $commis_ear_3 + $commis_ear_4 + $commis_ear_5 + $commis_ear_6;


<span style="font-weight: bold">// I think its this code here
    $r=mysql_query("UPDATE accounts SET acct_balance=acct_balance + '$earned_total' WHERE username ='$earnuser' LIMIT 1")or die(mysql_error());
    $r=mysql_query("UPDATE accounts SET earned_total=earned_total + '$earned_total' WHERE username ='$earnuser'  LIMIT 1")or die(mysql_error());</span>}

//update their session
$_SESSION['commission_today'] = true;

//update their last commission
mysql_query("UPDATE referrals SET last_commission_date = CURDATE() WHERE username='{$_SESSION['admin_user']}'")
or die ('cannot update last commission date');

}//end else
?>
nite4000
Forum Contributor
Posts: 209
Joined: Sun Apr 12, 2009 11:31 am

Re: why does a file run more then once

Post by nite4000 »

ok i will give it a show but will that work if they suppose to get commision when there downlink makes a deposit i mean if they have 4 downlinks and each make a deposit on the same day how is that days per commision gonna work?
User avatar
Jade
Forum Regular
Posts: 908
Joined: Sun Dec 29, 2002 5:40 pm
Location: VA

Re: why does a file run more then once

Post by Jade »

So you're saying they can have multiple referrals in the same day? If that's the case add a date field to the referrals table called commission_collected (or something like that) and update it with CURDATE() when they get their commission. Then if they refresh the page you just have to check to see if there's a date in the commission_collected field. If there don't let them collect a commission again.
nite4000
Forum Contributor
Posts: 209
Joined: Sun Apr 12, 2009 11:31 am

Re: why does a file run more then once

Post by nite4000 »

k sounds like a good idea. but i am still lost about one thing.. if user_b makes more then one deposit the same day then the user_a wont get commision but once right?
User avatar
Jade
Forum Regular
Posts: 908
Joined: Sun Dec 29, 2002 5:40 pm
Location: VA

Re: why does a file run more then once

Post by Jade »

I'm not sure what you're talking about. If the referral is tied to a specific user and you're checking for that user when you update the commissions then it will only update commissions for that one user.
nite4000
Forum Contributor
Posts: 209
Joined: Sun Apr 12, 2009 11:31 am

Re: why does a file run more then once

Post by nite4000 »

let me try to explain more..

lets say i add the date field and user A gets the commision from USER B thats fine... now what if USER B makes ANOTHER deposit that same day. then would user A still get that commision from that deposit as well?
nite4000
Forum Contributor
Posts: 209
Joined: Sun Apr 12, 2009 11:31 am

Re: why does a file run more then once

Post by nite4000 »

I could really use some help on this guys. I have tried everything.

a user can get as much commision as they want via downlinks deposits I just need to stop the doubling of when they refresh the index page it wotn give it to them again til there referrals make another deposit

anyone?
User avatar
Jade
Forum Regular
Posts: 908
Joined: Sun Dec 29, 2002 5:40 pm
Location: VA

Re: why does a file run more then once

Post by Jade »

If you just want to prevent refreshing of the page then you need to use a session variable that sets to true once they've received a commission and then is reset back to false when they navigate away from file you're add the the commission to their record.
Post Reply