Counting the number of <script src> inclusions

Discussion in 'PHP' started by vinyl, Apr 3, 2006.

  1. #1
    For instance, if somebody use this code:

    <script src="http://mysite.com/mypage.php"></script>
    <script src="http://mysite.com/mypage.php"></script>
    <script src="http://mysite.com/mypage.php"></script>
    HTML:
    How can I count the number of times that mypage.php is included, of course, thru mypage.php? /I just wanna get $count with values 1, 2 and 3/

    The point is to eliminate lets say more than 3 inclusions of mypage.php in single remote page.

    Thanks in advance!
     
    vinyl, Apr 3, 2006 IP
  2. themole

    themole Peon

    Messages:
    82
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #2
    You could use sessions I think.

    
    <?php
    session_start();
    
    if(!$_SESSION['include_counter'])
    	$_SESSION['include_counter'] = 1;
    elseif($_SESSION['include_counter'])
    	$_SESSION['include_counter']++;
    	
    if($_SESSION['include_counter'] > 3)
    	exit;
    	
    else
    {
    
    	//the code you want included only 3 times here
    	
    }
    ?>
    
    Code (markup):
    -the mole
     
    themole, Apr 3, 2006 IP
  3. vinyl

    vinyl Well-Known Member

    Messages:
    302
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    165
    #3
    yes, but session needs to be destroyed at some point in order to bypass infinite loop and session_destroy(); must depend of the unique URL (that single page which includes mypage.php undefined number of times) or something else which gives that functionality of separating pages (maybe visitor's IP?) - any ideas how to achieve it in the most effective way?
     
    vinyl, Apr 3, 2006 IP
  4. exam

    exam Peon

    Messages:
    2,434
    Likes Received:
    120
    Best Answers:
    0
    Trophy Points:
    0
    #4
    What you really want is to do the include just once right? Do it like the old C coders did. Here's your include file:
    
    <?php
    if (!defined ('MY_INCLUDE_FILE_BLAH')) {
         define ('MY_INCLUDE_FILE_BLAH', 1);
         // All your code here.
    }
    ?>
    
    PHP:
    EDIT: Just re-read the original post- You want to include the same code 3 times? Why?
     
    exam, Apr 4, 2006 IP
  5. vinyl

    vinyl Well-Known Member

    Messages:
    302
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    165
    #5
    im not the one which actually adds all those includes - mypage.php is only thing i host/control and its php script which outputs ads (using document.write(), as its ment to be included as javascript src) - i just want to prevent people from including mypage.php more than 3 times, per page (so that they dont get 4th, 5th etc. ad output at all)
     
    vinyl, Apr 4, 2006 IP
  6. exam

    exam Peon

    Messages:
    2,434
    Likes Received:
    120
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Your javascript include file:
    if (typeof count == 'undefined') {
    	count = 1;
    } else {
    	++count;
    }
    if (count <= 3) {
    	// All your ad code here
    	document.writeln ('Ads ' + count + '<br>');
    }
    Code (markup):
    A test page including the js.
    <html><head>
    
    
    <script src="js.js"></script>
    <script src="js.js"></script>
    <script src="js.js"></script>
    <script src="js.js"></script>
    <script src="js.js"></script>
    <script src="js.js"></script>
    
    </head>
    <body>
    adfsadsffdsa
    </body>
    </head>
    
    Code (markup):
    The output when viewed in browser:
    Ads 1
    Ads 2
    Ads 3
    adfsadsffdsa 
    Code (markup):
    And to think- I normally charge for this stuff. I must be going soft in the brain :rolleyes:
     
    exam, Apr 4, 2006 IP
  7. vinyl

    vinyl Well-Known Member

    Messages:
    302
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    165
    #7
    no, you are just willing to help free of charge and i appreciate it very much. thanks man!
     
    vinyl, Apr 4, 2006 IP
  8. exam

    exam Peon

    Messages:
    2,434
    Likes Received:
    120
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Donate something in Rob's thread for Reggie and I won't mention it :) j/k. Seriously I hope it works for you.
     
    exam, Apr 4, 2006 IP
  9. vinyl

    vinyl Well-Known Member

    Messages:
    302
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    165
    #9
    of course it does! one question though, pretty irrelevant for the functionality, just being curious -- count var from JS cant be passed to $count in PHP, right?
    functionality = I already bypassed execution of many PHP lines by placing them inside the JS's if-clause, in the place of // All your ad code here)
     
    vinyl, Apr 4, 2006 IP
  10. exam

    exam Peon

    Messages:
    2,434
    Likes Received:
    120
    Best Answers:
    0
    Trophy Points:
    0
    #10
    >> count var from JS cant be passed to $count in PHP, right

    That is correct.
     
    exam, Apr 4, 2006 IP
  11. vinyl

    vinyl Well-Known Member

    Messages:
    302
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    165
    #11
    but wait, then i didnt "bypassed execution of many PHP lines by placing them inside the JS's if-clause, in the place of // All your ad code here)", didnt I?

    can it be achieved somehow?
     
    vinyl, Apr 6, 2006 IP
  12. exam

    exam Peon

    Messages:
    2,434
    Likes Received:
    120
    Best Answers:
    0
    Trophy Points:
    0
    #12
    It's actually the *output* of your php code that goes in the JS if construct. The user's browser then won't display it.

    If you wanted to actually know who was including the code more than 3 times, you could send that information back to the server using an XMLHTTPRequest object.
     
    exam, Apr 6, 2006 IP