How can I test a javascript caching solution? - javascript

I'm looking into some of the established methods for forcing reload of cached javascript files. Problem is, I can't get a script to cache locally. I checked the network tab of Chrome for the "Disable cache" option is off. I'm using MVC to add this to a site homepage:
#section scripts
{
#Scripts.Render("~/Scripts/app/test.js")
}
And here is the test.js content:
alert('Welcome to 2');
If I change the alert text and refresh the page, regardless of whether the project is restarted or not, it is always fresh...

It's normally a server configuration issue.
https://medium.com/pixelpoint/best-practices-for-cache-control-settings-for-your-website-ff262b38c5a2
For a client-side dev who doesn't want to mess with server configs, you could also change your test.js file to a test.php file (literally just change the extension), call it the exact same way you're calling it (except, again, changing the extension to PHP) and add something like this to the very top of the PHP file:
<?php
$seconds_to_cache = 3600;
$ts = gmdate("D, d M Y H:i:s", time() + $seconds_to_cache) . " GMT";
header("Expires: $ts");
header("Pragma: cache");
header("Cache-Control: max-age=$seconds_to_cache");
?>
Then upload it to your server. As long as your server handles PHP (it probably does), this code should force the browser to cache the new file, which will function just like any JS file.
(Taken from https://electrictoolbox.com/php-caching-headers/)

Related

How to include PHP $_SESSION values in a javascript file?

I use $_SESSION['siteRoot'] to store the root address of my website in (it's basically a framework so this can change depending on the URL used to access the site). I need to use this value in some of my javascript files...
Up until now I've been including my js files as .php, and putting the following code at the top of my js files, like so:
<?php
header("Content-type: application/javascript");
session_start();
?>
This has been working fine on my local-host for testing - but when I upload it to the live server I noticed that my session was being reset every time I reload the page, and after a day of debugging finally discover that it's the session_start(); line in my java-script files that is causing this behavior.
I've tried the following:
if (!isset($_SESSION))
{
session_start();
}
if (session_id() == '')
{
session_start();
}
if (session_status() !== PHP_SESSION_ACTIVE)
{
session_start();
}
and also just leaving out the session_start altogether. If I don't start the session then I can't use the variables (obviously...), but I can't find a way of starting it that doesn't wipe the session I already created in my main page.
Any ideas?
The problem was not caused by the session itself exactly, but because I hadn't included my class autoloader before calling the session, and so my custom classes were not surviving the deserialize process (even though it's a javascript file and I don't use any of said classes!)
I changed my javascript code to:
<?php
include ('include/autoloader.php');
header("Content-type: application/javascript");
session_start();
?>
and everything works fine. Easy to forget when you're not using any of the serialized classes on that page!

PHP scripts writes to file on localhost but not on IIS server

I've made a simple website that works fine on localhost. So I've put it on an IIS Windows 2008r2 server and now my PHP scripts don't write to my JSON files anymore. I've checked the server and PHP is installed on it so I don't really know what's wrong or where to look.
I'm still not getting it to work so thought I'd explain the situation in more detail.
So this script works on localhost but not on IIS server.
<?php
$myFile = "../json/countries.json";
$fh = fopen($myFile, 'w') or die("can't open file");
$stringData = json_encode($_POST["data"]);
fwrite($fh, $stringData);
fclose($fh)
?>
I've tried adding:
error_reporting(E_ALL);
ini_set('display_errors', '1');
and
chmod("../json/countries.json", 0644);
to the php but not seeing any different results or any errors.
Here's the javascript function that starts the process, and outputting the object to the console does show the correct data to be saved.
function saveJson(object, file) {
console.log("Saving JSON data: " + JSON.stringify(object));
$.ajax
({
type: "POST",
dataType : 'json',
async: false,
url: file,
data: { data:object },
success: function () {console.log("Thanks!"); },
failure: function() {console.log("Error!");}
});
}
Still the json files are not being changed.
Anyone good with Windows Server and php that might know why?
Thanks
This kind of problem occurs because of three reason only:
Don't have proper Directory owner.Ex:- In Apache default user is www:data.
First check the directory owner.
You don't have sufficient write permission for directory i.e. 755
Check directory permission
Path is incorrect to a directory to write or upload file.
Check the directory path where you are writing the file.
According to php document Installation on Windows systems default user is IUSER
Hence you have to set the directory owner 'IUSR' is part of the IIS_IUSRS group, If its not works then try to set 'IIS AppPool\{YouApplicationPoolName}' from IIS AppPool\DefaultAppPool Ex. e.g. IIS AppPool\yourdomain.com. This setting is required for IIS8.
First try to change the owner to IUSER. It should work according to php document.
Set the write permission for the directory.
How to Change a directory permission & user group on windows system.
Rightclick on directory->security->edit
Please check attached screen shot.
What version of PHP are you using. If it is less than 5.2, the json encode function may not be working. Use phpinfo() to find out.
It may also be that $_POST['data'] does not exist. try putting a array instead to see if thats the problem. Like: array(1,2,3);
Also if no warnings are being shown use: error_reporting(E_ALL); OR error_reporting(-1); at the top of your script. It may be that your host is turning off these errors. this will allow you to see what the problem is.
The host also has the ability to deactivate certain PHP function. Contact your customer service regarding this problem. Although I find it very unlikely that they have deactivated the ones you are using.
Make sure the file exist relative to where the PHP code is being executed. If this is the problem, the die command should run. You can see f the file exists using:
if (file_exists ( $filename ) ) echo "file exists";
else echo "file does not exist";
If none of the above work then I haven't got a clue! Sorry.
Add IIS_IUSRS with access "execute, list, read" to security tab on properties.

PHP Ajax jQuery cross domain file loading

So before you say this can't be done. These are all files that I have one server. I just have some of them listed under different domains.
My PHP script will access all the files but when I try to do an ajax request to try to load the file I will often get an error (because the site i am accessing is secure and the one I am accessing it through isn't).
What I need is a way to have php grab the file. But I need aJax to retrieve the file and render it for me. I am also using ACE editor to edit the file
The bit of code I have here will actually error out as well because it will load and print out the file where $page is defined but won't load where htmlspecialchars is.
<script>
var e = ace.edit("editor");
<?php
$page = readfile($_SERVER['DOCUMENT_ROOT'].$_GET['dir']);
echo 'e.setValue('.htmlspecialchars($page, ENT_QUOTES).');';
?>
</script>
I have an ajax get request working but it doesn't work when I go to a directory with a special htaccess file. Now I can't change the htaccess file (unless there is a way for me to confirm that it is my script running and not someone else.
The question is, how can I access those other files without getting that error? Mind you those files could be extension. It is not limited to just scripts or css, mostly they will be html or php files.
After an hour of searching the deep dark depths of the php.net site I was able to put together a solution that works.
<?php
echo htmlspecialchars(
addslashes(
file_get_contents(
$_SERVER['DOCUMENT_ROOT'].$_GET['d‌​ir']
)
)
); ?>
the addslashes is the extra part that I needed. Then I also had to put it between the div for the editor. I couldn't use the editor.setValue() function.

HTTP File Download: Monitoring Download Progress

I am in a situation, when I have to implement downloading of large files(up to 4GB) from a Web server: Apache 2.4.4 via HTTP protocol. I have tried several approaches, but the best solution looks to be the usage of X-SendFile module.
As I offer progress bar for file uploads, I would need to have the same feature for file downloads. So here are my questions:
Is there any way, including workaround, to achieve file downloads progress monitoring?
Is there any way, including workaround, to calculate file download transfer speed?
Is there better way to provide efficient file downloads from a web server than usage of X-Sendfile module?
Is there better file download option in general, that would allow me to monitor file download progress? It can be a client (JavaScript) or server solution(PHP). Is there any particular web server that allows this?
Currently I use:
Apache 2.4.4
Ubuntu
Many times thanks.
2 ideas (not verified):
First:
Instead of placing regular links to files (that you want to download) on your page place links like .../dowanload.php which may look sth like this:
<?php
// download.php file
session_start(); // if needed
$filename = $_GET['filename']);
header( 'Content-type: text/plain' ); // use any MIME you want here
header( 'Content-Disposition: attachment; filename="' . htmlspecialchars($filename) . '"' );
header( 'Pragma: no-cache' );
// of course add some error handling
$filename = 'c:/php/php.ini';
$handle = fopen($filename, 'rb');
// do not use file_get_contents as you've said files are up to 4GB - so read in chunks
while($chunk = fread($handle, 1000)) // chunk size may depend on your filesize
{
echo $chunk;
flush();
// write progress info to the DB, or session variable in order to update progress bar
}
fclose($handle);
?>
This way you may keep eye on your download process. In the meantime you may write progress info to the DB/session var and update progress bar reading status from DB/session var using AJAX of course polling a script that reads progress info.
That is very simplified but I think it might work as you want.
Second:
Apache 2.4 has Lua language built in:
mod_lua
Creating hooks and scripts with mod_lua
I bet you can try to write LUA Apache handler that will monitor your download - send progress to the DB and update progress bar using PHP/AJAX taking progress info from the DB.
Similarly - there are modules for perl and even python (but not for win)
I see main problem in that:
In a php+apache solution output buffering may be placed in several places:
Browser <= 1 => Apache <= 2 => PHP handler <= 3 => PHP Interpreter
process
You need to control first buffer. But directly from PHP it is impossible.
Possible solutions:
1) You can write own mini daemon which primary function will be only send files and run it on another than 80 port 8880 for example. And process downloading files and monitor output buffer from there.
Your output buffer will be only one and you can control it:
Browser <= 1 => PHP Interpreter process
2) Also you can take mod_lua and control output buffers directly from apache.
3) Also you can take nginx and control nginx output buffers using built-in perl (it is stable)
4) Try to use PHP Built-in web server and control php output buffer directly. I can't say anything about how it is stable, sorry. But you can try. ;)
I think that nginx+php+built-in perl is more stable and powerful solution.
But you can choose and maybe use other solution non in that list. I will follow this topic and waiting your final solution with interest.
Read and write to the database at short intervals is killing performance.
I would suggest to use sessions (incrementing the value of sent data in the loop) with which you can safely off by quite another php file, you can return data as JSON which can be used by the javascript function/plugin.

Return downloadable file in PHP to Internet Explorer with JavaScript

I have a PHP web application in which users can click a button to download an XML file generated on the fly. It has worked well for a long time, but suddenly I am getting bug reports from a single Internet Explorer user (I only have a screenshot showing the bug). I need your help to figure out possible causes.
First, the button click in the web GUI is handled with jQuery which has a JavaScript window.location statement:
$("#generate-button").click(function() {
...
window.location = "generateXml.phtml";
}
generateXml.phtml is PHP which creates an XML string which is returned:
header('Content-Type: application/xml');
header('Content-Disposition: attachment; filename=xmlFile.xml');
echo $xmlString;
Up until now, users have always been prompted whether to open or save the xmlFile.xml (which is correct). Now however, a user of Internet Explorer (version unknown but it appears to be IE10) is prompted whether to open or save generateXml_phtml:
Note that IE has replaced the . (dot) with _ (underscore). It seems the PHP server code in generateXml.phtml is not even executed.
Do you have any ideas? I cannot replicate the bug using IE on my own system.
Can you give a direct link to the problem? Try change the extenssion to .php its weird if the file ins´t being processed the web server should only give you a executed code not the raw code, maybe is something wrong with .phtml extenssion...

Categories