Wednesday, April 6, 2011

Can I disable FF3 back button cache?

I found out that when pressing back button it gets previous page from browser cache even if I send following headers:

Test1.aspx

Server              ASP.NET Development Server/9.0.0.0
Date         Wed, 24 Mar 2010 17:49:40 GMT
X-AspNet-Version 2.0.50727
Location         Test2.aspx
Cache-Control no-cache, no-store
Pragma         no-cache
Expires         -1
Content-Type text/html; charset=utf-8
Content-Length 189
Connection         Close
From stackoverflow
  • Cache-control and such things only tell browser NOT to save in cache the downloaded stuff (js, css, images, etc.). It does not relate with the History of visited pages.

    You shouldn't try to modify browser's data. Instead, you'd handle events and stop the ones you don't want to happen in your site.

    Sergej Andrejev : What if I have a sensitive data. Say a public vote kiosk. User choses president candidate and then clicks submit. Now next voter goes to kiosk, presses back button and sees what candidate previous user has selected, because browser didn't even try to get latest version from server and displayed his from cache. What should I do then?
    Alfabravo : It means your app behaves wrong. Transactions, sessions and that kind of things are meant to handle restrictions in access to sensitive data. When webapps say "remember to close your session", they mean it! Also, if your app places confidential data in browser's cache, it means you're opening a new security breach. Plain and simple (unless you cipher it!). You can't rely on unauthorized modifications to browser's data (by definition it is on user's side) in order to provide security. Too much like DRM, putting all the security in the same side.
  • expires should be a date+timestamp and cache-control"s "must-revalidata" & "max-age" might help as well?

    Expires: Wed, 11 Jan 1984 05:00:00 GMT
    Cache-Control: no-cache, must-revalidate, max-age=0
    
    Sergej Andrejev : Actually the answer was that simple. But I want to aware others. Browsers don't cache pages, they cache requests. So if you have one page Test.aspx with one link to itself. And user first opens the page, then clicks on the link (here no-cache is added) and then presses back button the page will be retrieved from the cache because first time it was downloaded without no-cache directive
    futtta : well, browsers cache responses, which in general are pages, no? and if i'm not mistaking, "no-cache" will already be in the http response header even upon the first request.

Saving page as PDF or as HTML but with all entries

Hello every one :)

I wont to create a pdf from a form on my page, but the Problem is, I need it excactly like the page with form, all entires.

So I have for example

2 Input Fields, 7 Radio, 2 Checkboxes, and as result i need a PDF with the same sructure, but if someone check the checkbox, it must be saved in pdf.

I have tryed to save the html content of the page on submit, and save it first in html file, but the problem is, my selections woundn't be saved.

The result must have the same as i would print my form.

I hope someone can help.

The Code i using to save the page content.

> $(document).ready(function(){
>       $('input[type=submit]').click( function()   {           
> var formname = $("body").find("form").attr("name");
> var htmldata = $("form[name="+formname+"]").html();
> var enchtmldata = ncodeURIComponent(htmldata);
> $.ajax({  
> type: "post",             
> data: "data="+enchtmldata,
> url: "makepdf.php",           
> success: function()
>   {
>   alert("success");           },
> error: function()             {
>   alert("error");             }           });
> 
> }); });

PS: I using PHP and jQuery

ADDED: I think it is better to try first of all to save the form page as html, but to keep the entries in it. After that to try to convert it. But the Problem is, to save it with all data.

ADDED: how can I add a attr selected to an option field?

From stackoverflow
  • Run this in your browser's javascript command line, copy-paste the result to text editor and save as html file.

    $('body').text($('html').html())
    

    Depending on your browser, this may also work when copy-pasted to address bar:

    javascript:$('body').text($('html').html())
    

    The output is "dynamic" page source, i.e. DOM tree rendered into text.

    Fincha : The Problem is, if I have writen somethink in the field, it wouldn't be saved by using .html() or .text() ... the same is with checkboxes :(
    jholster : You are right, my suggestion doesn't work.
  • What do you use to convert the HTML to PDF? I can recommend TCPDF for that job: http://www.tecnick.com/public/code/cp_dpage.php?aiocp_dp=tcpdf

    It can convert HTML with CSS to a PDF form. I haven't tested it myself but I am quite sure that it will even keep the checked option. But for sure you have to create the HTML with PHP to have the checkbox checked.

    <input type="checkbox"<?php echo ($_REQUEST['checkbox_name'])? ' checked="checked"' : '' ?> />
    

    You should than be able to create the correct PDF form with the TCPDF Class.

    Fincha : Hm, this class is very usefull. But first of all, I need to save the html page ast, txt or thml this all inputs and after that, convert it. I using this jquery code, but all i get, is the form without entries
    Kau-Boy : But if you sent the form to itself and that add all the values in the $_REQUEST array to the value attributes of input and textare and select/check all other elements, you'll get the correct form state in the HTML source. Or just try my new suggestion.
    Fincha : hm, i will try it now
    Fincha : I have a problem to do this, a can't change forms :/
    Kau-Boy : You don't have access to the form source code? That's bad. Could you duplicate the form source code as a PDF template and add the form data to that template? That might make it a lot easier.
  • Ok, I think the problem ist, that checking a checkbox in the browser doesn't change the HTML source. So if you want to transfer the HTML via AJAX, you have to "convert" the checked state within the browser to a real source checked state.

    I am not an jQuery expert, but you shoul select all input[type="checkbox"] and test if they are checked. If they are, add an attribute "checked" with the value "checked" to the checkbox. Otherwise remove the checked attribute if it is present.

    The source HTML will than have all the correct checked states. Without this method you might get some the checkboxe that have been preselected with PHP but have been deselected by the user.

    I hope my answer was clear enough, sorry for my english :)

    Fincha : yes that it the problem, i will try to do this
    Fincha : $(this).attr("value", $(this).val()); doesn't works
    Kau-Boy : As I already mentioned, my jQeury knowledge is limited to the basics. I could give you a solution with Prototype. But that's what you have to do: select all input, textarea and select elements. For alls input that are not checkboxes or radio buttons, set the value attribute. For checkboxes and radio buttons and selects check for the "checked" or "selected" state and add a checked or selected attribute to them. For textareas append the value as a text child node to the textarea. But how to solve that with jQuery you might have to ask a jQuery expert.
  • Have you tried placing the page as a background with the variables as conditional overlays? I have achieved this in the past using FPDF. Radio buttons can be placed as an image ontop of the existing screen dump. You will have no problem putting the text in the right place also.

  • Is there any reason you can't just take the form data and send it to a sever process that will store the form data in a database? From there it should be a relatively simple process to populate the HTML form and generate a PDF form from the database.

    Fincha : sorry, no way :(

Securing S3 via your own application

Imagine the following use case:

You have a basecamp style application hosting files with S3. Accounts all have their own files, but stored on S3.

How, therefore, would a developer go about securing files so users of account 1, couldn't somehow get to files of account 2?

We're talking Rails if that's a help.

From stackoverflow
  • If you want to restrict control of those remote resources you could proxy the files through your app. For something like S3 this may defeat the purpose of what you are trying to do, but it would still allow you to keep the data with amazon and restrict access.

    You should be careful with an approach like this as it could cause your ruby thread to block while it is proxying the file, which could become a real problem with the application.

    Jacek Konieczny : It doesn't have to be proxied directly by the RoR. The HTTP server below may be configured to do the proxying and RoR would only provide some kind of security credentials (probably a cookie value to test). This setup may be quite complicated, depends on the HTTP server used, requires access to server configuration and may require extra helpers (like FastCGI authenticator app or some authentication hook). Though, the performance gain may be worth it in some cases.
    Neil Middleton : I wonder if a simple proxy -> server side redirect would provide enough to stop the vast majority of idle surfers rather than going the whole hog...
  • Serve the files using an EC2 Instance

    If you set your S3 bucket to private, then start up an EC2 instance, you could serve your files on S3 via EC2, using the EC2 instance to verify permissions based on your application's rules. Because there is no charge for EC2 to transfer to/from S3 (within the same region), you don't have to double up your bandwidth consumption costs at Amazon.

    Neil Middleton : I'm not really wanting to have a bucket per account as I think this won't scale particularly well...
    John Douthat : you can still keep all your objects in one S3 bucket.
  • I haven't tackled this exact issue. But that doesn't stop me from having an opinion :)

    Check out cancan:

    It allows custom authorization schemes, without too much hassle.

Setting jvmargs in log4j.properties file

Hey

This is a bit of a weird request but I am trying to set some jvmargs in the log4j.properties file. At present I use ant to for example set some args....

jvmarg value="-Dmail.smtp.socketFactory.port=465"

... but it would be great to group a few of these logging relevant arguments into the .properties file. Does anyone know how to do this?

Thanks in advance!

From stackoverflow
  • Log4j is only going to read the properties file after the JVM has already started - that means it can't affect the JVM arguments.

  • If your example is from your actual situation then you can set this value programatically for the java mail...

    The SMTP protocol provider supports the following properties, which may be set in the JavaMail Session object. The properties are always set as strings; the Type column describes how the string is interpreted. For example, use

        props.put("mail.smtp.port", "888");
    

    http://java.sun.com/products/javamail/javadocs/com/sun/mail/smtp/package-summary.html

    This example above should work for mail.smtp.socketFactory.port also.

    Thomas : This is something we have looked at, but ideally we would like to keep the configuration separate from the code so we can swap in different logging systems easily at a later date... I am trying to set: mail.smtp.port mail.smtp.socketFactory.class mail.smtp.socketFactory.port mail.smtp.socketFactory.fallback Setting them in the ant build file is ok... but not ideal :(
  • If the properties can be added after JVM startup, you could add a property to your properties file that lists all properties that you want to add to the SystemProperties collection, something like:

    # property names of system properties
    systemprops=mail.smtp.port mail.smtp.socketFactory.class
    
    mail.smtp.port=465
    mail.smtp.socketFactory.class=some.class
    

    Your startup code can read the systemprops value, split on whitespace and add the resulting list of properties to the SystemProperties collection while reading the values from your properties collection.

    This way your code does not need to know which properties to add to system props, only that the properties to add are defined by the systemprops property.

Estimate gaussian (mixture) density from a set of weighted samples

Assume I have a set of weighted samples, where each samples has a corresponding weight between 0 and 1. I'd like to estimate the parameters of a gaussian mixture distribution that is biased towards the samples with higher weight. In the usual non-weighted case gaussian mixture estimation is done via the EM algorithm. Does anyone know an implementation (any language is ok) that permits passing weights? If not, does anyone know how to modify the algorithm to account for the weights? If not, can some one give me a hint on how to incorporate the weights in the initial formula of the maximum-log-likelihood formulation of the problem?

Thanks!

From stackoverflow
  • Just a suggestion as no other answers are sent.

    You could use the normal EM with GMM (OpenCV for ex. has many wrappers for many languages) and put some points twice in the cluster you want to have "more weight". That way the EM would consider those points more important. You can remove the extra points later if it does matter.

    Otherwise I think this goes quite extreme mathematics unless you have strong background in advanced statistics.

  • You can calculate a weighted log-Likelihood function; just multiply the every point with it's weight. Note that you need to use the log-Likelihood function for this.

    So your problem reduces to minimizing $-\ln L = \sum_i w_i \ln f(x_i|q)$ (see the Wikipedia article for the original form).

Tell me some Http-streaming tutorial or example.

Could you tell me some http-streaming tutorial or example ( used also by Gmail ) ?

I want to create a simple chat for college project.

Attention: I am talking about Http-streaming and NOT long-polling.

Thanks ;)

From stackoverflow
  • Check here. Some cool intro about Comet. There is also a mention for Jetty and its free chat client but they are in Java not PHP. Will keep looking and post if I find something useful with PHP. Just FYI, jquery has a plugin for comet but I haven't tried it yet.

    xRobot : jquery has a plugin for long-polling. I need a http-streaming example and not long-polling as I write above ;)
    pinaki : I suppose http://ajaxian.com/archives/picomet-simple-comet-library?utm_source=feedburner&utm_medium=feed&utm_campaign=Feed%3A+ajaxian+%28Ajaxian+Blog%29 should be fine for you.
    pinaki : Just saw your comment come up. What I know is that long-polling is AJAX, http-streaming is Comet (or server push). Am I getting this wrong?
    pinaki : So i knew wrong :). Just confirmed from wikipedia. Not sure what technology the other link uses though. It says comet and as I just read Comet is an umbrella term for both http-streaming and long polling. Let me know if you manage to find anything on this.
    xRobot : ok.......... :)
  • "Comet" is just an umbrella term that includes HTTP streaming.

    For some good examples, you can check out our WebSync demos.

    For some tutorials on using it, you can check out the tutorials.

Speed up :visible:input selector avoiding filter

I have a jQuery selector that is running way too slow on my unfortunately large page:

$("#section").find(":visible:input").filter(":first").focus();

Is there a quicker way to select the first visible input without having to find ALL the visible inputs and then filtering THAT selection for the first? I want something like :visible:input:first but that doesn't seem to work.

[Edit] Here's the basic idea of what #section looks like:

<div id="section">
    <div>
        Some text <input type="text">
    </div>
    <div>
        etc. etc. <input type="text">
    </div>
</div>
From stackoverflow
  • $(":input:visible:first", "#section").focus();
    

    If you first filter for the type of control you avoid checking the :visible on all the #section's elements.

    It seems like you need only to catch the first input type="text" visible.
    This should be a bit faster.

    $("input[type='text']:visible:first", "#section").focus();
    
    Nick Craver : This is a different selector, `:input` != `input`
    Alex Bagnolini : Fixed, thank you.
    macca1 : Thanks, this update is about 80ms faster. However this selector is still costing 156ms in IE6. I feel like this should be a quick 16ms lookup or less. Is there any other way to optimizer this?
    macca1 : I'm accepting the first part of this answer. Seems like the fastest way possible of doing this. Thanks!
  • How about adding class="default_field" to the default field for each page, then using $('.default_field').focus();?

    How easy this is to do depends on your server-side technology of course, but the advantages are that it takes the processing burden off of the client (which is extra important for IE6), and it also gives you the flexibility to choose a default input other than the very first one on pages where it's appropriate.