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!
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
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?
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?
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)
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
Donate something in Rob's thread for Reggie and I won't mention it j/k. Seriously I hope it works for you.
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)
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?
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.