WordPress.org

Ready to get started?Download WordPress

Forums

[Plugin: All in One Adsense and YPN] Donation code off by one? (9 posts)

  1. tim.bishop
    Member
    Posted 5 years ago #

    Unless I'm reading the code wrong, isn't the All In One Adsense and YPN donation code off by one? There seems to be a curious addition of 1 to the percentage you give. So if you specify 0 it'll actually be 1%.

    I admit my PHP isn't that hot, but I worked through the function and checked the API, and I'm fairly sure that it'll donate 1% more than you specify.

  2. 16FLF61
    Member
    Posted 5 years ago #

    First the plugin is broken, now it has backdoors...
    Whats next ?

  3. linewbie
    Member
    Posted 5 years ago #

    No, test it, I even put it in comment that adding 1 is required to get the percentage. If you don't add the 1, the percentage will actually be 1% less.

  4. tim.bishop
    Member
    Posted 5 years ago #

    Here's your code (trimmed down and tided a bit, but functionally identical):

    $donation = get_option('ai_donation');
    $d_pct = 1 + intval(round(intval($donation))); //adding the 1 is needed for some reason to obtain the correct percentage
    if($d_pct){
        $d_rand = mt_rand(1,100);
        $d_range = range(1, $d_pct);
        if(array_search($d_rand, $d_range)){
            // use donation code
        }
    }

    If you set a value of 5 for the donation percentage $d_pct ends up being 6. The if statement is then true.

    Next a random number is picked between 1 and 100 inclusive. Then an array created between 1 and (using 5% as an example) 6 inclusive - so the array is [1, 2, 3, 4, 5, 6].

    The array_search function then looks for the random number in the array. Since the array is 1-6 and the random number is 1-100, that means 6 out of 100 times (on average) it'll match. That's 6% of the time, not 5%.

    But, what saves you here is that array_search returns the element of the the array that matched, which in the case of the first element is 0. The if statement is therefore false. If this is by design it seems to be quite sloppy coding.

    So in the above example only the numbers 2-6 can match, so it would be correct at 5%. This won't work for case where you specify 100% though, because 1% of the time it'd still be false.

    I therefore take back the statement about being off by one, but it's a subtle point, and easily overlooked.

  5. linewbie
    Member
    Posted 5 years ago #

    Yeah I didn't spend too much time on that, I just wrote the code and ran it a couple million times to make sure the percentage looks right. That's when I noticed I had to add the 1 to get the right percentage. Anyway it works correctly.

  6. tim.bishop
    Member
    Posted 5 years ago #

    How about this:

    $donation = get_option('ai_donation');
    $d_pct = intval(round(intval($donation)));
    if($d_pct){
        $d_rand = mt_rand(1,100);
        if($d_rand <= $d_pct){
            // use donation code
        }
    }

    It's simpler and will avoid any questions in the future.

  7. tim.bishop
    Member
    Posted 5 years ago #

    Actually,

    $d_pct = intval($donation);

    Is probably all that's required for that line.

  8. nareshg
    Member
    Posted 5 years ago #

    not going it too the details here as I have not got a chance to look at the code and not much of an expert there at the moment.

    but I am using this plugin and have ads on my site for more than a week, I see 5 to 10 folks each day visitng my site as per google analytics and each of them spending a 2 to 4 minutes on an average on the site....

    but do not see any movements on my adsense revenue...not even one ad is difficult to believe ? I am not expecting much here but would like to know if the ads are working with my adsense id ....

    any thoughts....could for some reason the code be doing something by mistake...and replacing my adsense id ?

    Also, there is no way to test if your ads are working right ?

  9. tim.bishop
    Member
    Posted 5 years ago #

    nareshg: view the source of your webpage and check the adsense ID is correct.

Topic Closed

This topic has been closed to new replies.

About this Topic