Go Back   vb.org Archive > vBulletin 4 Discussion > vB4 Programming Discussions
  #1  
Old 03-04-2013, 01:47 PM
nerbert nerbert is offline
 
Join Date: May 2008
Posts: 784
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default Headers to cache JavaScript

I have a member I have to suspend from time to time and during the suspension he tries registering several times a day. My plan is to cache some JavaScript on his computer to identify him and abort his registration attempts.

My other admin suggested testing for a JS variable and if it has no value use AJAX to call a PHP file whose output is treated as a JS file that will be cached. Using the headers he suggested, I created a file, cache.php, to test caching:

PHP Code:
<?php
$farfuture 
mktime(0,0,0,1,1,2030);
header("Cache-control: public",true);
header("Pragma:",true);
header("Last-modified:" date("D, d M Y H:i:s",$farfuture) . "GMT"true);
header("Expires:" date("D, d M Y H:i:s",$farfuture) . "GMT" true);
header('Content-Type: text/javascript; charset=utf-8');
?>
var testVar = "test";
and I request it with AJAX in the headinclude template with this:

Code:
<script>
        if(typeof testVar == 'undefined') {
        	var getCache = new vB_AJAX_Handler(true);
		getCache.onreadystatechange(function() {
			alert(getCache.handler.responseText);
		});
                getCache.send('cache.php');
	} else {
		alert('t = ' + testVar);
	}
               
</script>
So if it fails to cache I should get "var testVar = "test";" and if it caches I should get "t = test".

It doesn't cache. Do I have a problem with my headers or is there some feature of vBulletin that prevents caching?
Reply With Quote
  #2  
Old 03-04-2013, 02:46 PM
kh99 kh99 is offline
 
Join Date: Aug 2009
Location: Maine
Posts: 13,185
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

I'm not a js or caching expert or anything, so maybe I just don't understand how it's supposed to work. But I don't see how that is supposed to detect whether or not a file is cached by the browser. If it's cached it just means that loading it won't require a request to the server, but it doesn't mean that it will already be loaded for you. So it seems to me that testVar would always be undefined.
Reply With Quote
  #3  
Old 03-04-2013, 03:13 PM
nerbert nerbert is offline
 
Join Date: May 2008
Posts: 784
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

He says that JS files are generally cached on your browser so they don't have to be loaded from the server every time. So if a file is missing it should be available in your browser cache.

This is supposed to cache the simple one line file giving a value to testVar.

Maybe I should try manually adding and then removing a test file using <script src="my-test-file.js"></script> and see what happens
Reply With Quote
  #4  
Old 03-04-2013, 03:15 PM
kh99 kh99 is offline
 
Join Date: Aug 2009
Location: Maine
Posts: 13,185
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

I tried to add this to the above post but for some reason I'm having a problem. Anyway:

It is an interesting idea. It seems like what you'd want to do is something like, have cache.php return code that sets a variable to a different value depending on whether or not that particular user is loading it, then just load it with <script src="cache.php"> and check the value of the variable. Also maybe set it to not cache if a guest loads it, that way it won't get cached before the user logs in.
Reply With Quote
  #5  
Old 03-04-2013, 03:38 PM
nerbert nerbert is offline
 
Join Date: May 2008
Posts: 784
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

I'm having trouble using Quick Reply here. There seems to be some AJAX problem here.

If I can get caching to work at all I'll put in various conditionals and store the user's name and id. But for now caching doesn't seem to work. I tried

Code:
<!--script type="text/javascript" src="{vb:raw vboptions.bburl}/clientscript/test-secret.js"></script-->
<script>
	alert('s = ' + secret);
</script>
and alternating whether the script tags linking to the file are commented or not and caching still isn't working. (The file just gives a value to the secret variable)
Reply With Quote
  #6  
Old 03-04-2013, 03:45 PM
kh99 kh99 is offline
 
Join Date: Aug 2009
Location: Maine
Posts: 13,185
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Quote:
Originally Posted by nerbert View Post
and alternating whether the script tags linking to the file are commented or not and caching still isn't working.
Well, that's what I meant. If you comment out the script tags that load the file, it doesn't matter if it's cached or not, the code won't be loaded.
Reply With Quote
  #7  
Old 03-04-2013, 04:01 PM
nerbert nerbert is offline
 
Join Date: May 2008
Posts: 784
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

OK I see what you mean. Instead of commenting the script tags I changed the file name and now it gets the value of secret from cache. I'll try putting in the tags you suggested for cach.php and see what happens.
Reply With Quote
  #8  
Old 03-04-2013, 04:50 PM
kh99 kh99 is offline
 
Join Date: Aug 2009
Location: Maine
Posts: 13,185
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Again I'm not an expert on the subject, but I think, depending on browser settings, the browser can send a request to check if the file has changed. So to make it work you might have to detect that request and return a "not changed" response. I think that's what's going on at the beginning of image.php, so you might want to look at that.
Reply With Quote
  #9  
Old 03-04-2013, 09:47 PM
nerbert nerbert is offline
 
Join Date: May 2008
Posts: 784
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

I found something that almost works:

Code:
<script src="{vb:raw vboptions.bburl}/cache.php"></script>
<script>
	alert('x = ' + testVar)
</script>
No AJAX necessary.

Since cache.php is a php file I can require global.php and set testVar to the userid. After logging out that value is preserved for a few refreshes and then for some reason it loads the file from the server and returns a userid = 0 as it should for a guest.

Does anyone know how the software would direct the browser to reload a cached file after several page refreshes? Or would that be a browser setting?
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT. The time now is 04:27 AM.


Powered by vBulletin® Version 3.8.12 by vBS
Copyright ©2000 - 2024, vBulletin Solutions Inc.
X vBulletin 3.8.12 by vBS Debug Information
  • Page Generation 0.04831 seconds
  • Memory Usage 2,242KB
  • Queries Executed 11 (?)
More Information
Template Usage:
  • (1)SHOWTHREAD
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_header_end
  • (1)ad_header_logo
  • (1)ad_navbar_below
  • (1)ad_showthread_beforeqr
  • (1)ad_showthread_firstpost
  • (1)ad_showthread_firstpost_sig
  • (1)ad_showthread_firstpost_start
  • (3)bbcode_code
  • (1)bbcode_php
  • (1)bbcode_quote
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)navbar
  • (3)navbar_link
  • (120)option
  • (9)post_thanks_box
  • (9)post_thanks_button
  • (1)post_thanks_javascript
  • (1)post_thanks_navbar_search
  • (9)post_thanks_postbit_info
  • (9)postbit
  • (9)postbit_onlinestatus
  • (9)postbit_wrapper
  • (1)spacer_close
  • (1)spacer_open
  • (1)tagbit_wrapper 

Phrase Groups Available:
  • global
  • inlinemod
  • postbit
  • posting
  • reputationlevel
  • showthread
Included Files:
  • ./showthread.php
  • ./global.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/class_hook.php
  • ./includes/modsystem_functions.php
  • ./includes/functions_bigthree.php
  • ./includes/class_postbit.php
  • ./includes/class_bbcode.php
  • ./includes/functions_reputation.php
  • ./includes/functions_post_thanks.php 

Hooks Called:
  • init_startup
  • init_startup_session_setup_start
  • init_startup_session_setup_complete
  • cache_permissions
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • style_fetch
  • cache_templates
  • global_start
  • parse_templates
  • global_setup_complete
  • showthread_start
  • showthread_getinfo
  • forumjump
  • showthread_post_start
  • showthread_query_postids
  • showthread_query
  • bbcode_fetch_tags
  • bbcode_create
  • showthread_postbit_create
  • postbit_factory
  • postbit_display_start
  • post_thanks_function_post_thanks_off_start
  • post_thanks_function_post_thanks_off_end
  • post_thanks_function_fetch_thanks_start
  • post_thanks_function_fetch_thanks_end
  • post_thanks_function_thanked_already_start
  • post_thanks_function_thanked_already_end
  • fetch_musername
  • postbit_imicons
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • postbit_display_complete
  • post_thanks_function_can_thank_this_post_start
  • tag_fetchbit_complete
  • forumrules
  • navbits
  • navbits_complete
  • showthread_complete