Easy way to test a URL for 404 in PHP?

If you are using PHP’s curl bindings, you can check the error code using curl_getinfo as such: $handle = curl_init($url); curl_setopt($handle, CURLOPT_RETURNTRANSFER, TRUE); /* Get the HTML or whatever is linked in $url. */ $response = curl_exec($handle); /* Check for 404 (file not found). */ $httpCode = curl_getinfo($handle, CURLINFO_HTTP_CODE); if($httpCode == 404) { /* Handle … Read more

HTTP authentication logout via PHP

Mu. No correct way exists, not even one that’s consistent across browsers. This is a problem that comes from the HTTP specification (section 15.6): Existing HTTP clients and user agents typically retain authentication information indefinitely. HTTP/1.1. does not provide a method for a server to direct clients to discard these cached credentials. On the other … Read more

How to configure static content cache per folder and extension in IIS7?

You can set specific cache-headers for a whole folder in either your root web.config: <?xml version=”1.0″ encoding=”UTF-8″?> <configuration> <!– Note the use of the ‘location’ tag to specify which folder this applies to–> <location path=”images”> <system.webServer> <staticContent> <clientCache cacheControlMode=”UseMaxAge” cacheControlMaxAge=”00:00:15″ /> </staticContent> </system.webServer> </location> </configuration> Or you can specify these in a web.config file in … Read more

How to prevent browser page caching in Rails

I finally figured this out – http://blog.serendeputy.com/posts/how-to-prevent-browsers-from-caching-a-page-in-rails/ in application_controller.rb. After Ruby on Rails 5: class ApplicationController < ActionController::Base before_action :set_cache_headers private def set_cache_headers response.headers[“Cache-Control”] = “no-cache, no-store” response.headers[“Pragma”] = “no-cache” response.headers[“Expires”] = “Mon, 01 Jan 1990 00:00:00 GMT” end end Ruby on Rails 4 and older versions: class ApplicationController < ActionController::Base before_filter :set_cache_headers private def … Read more

HTTP HEAD Request in Javascript/Ajax?

Easy, just use the HEAD method, instead of GET or POST: function UrlExists(url, callback) { var http = new XMLHttpRequest(); http.open(‘HEAD’, url); http.onreadystatechange = function() { if (this.readyState == this.DONE) { callback(this.status != 404); } }; http.send(); } This is just a short example to show how to use the HEAD method. Production code may … Read more

Java – How to find the redirected url of a url?

Simply call getUrl() on URLConnection instance after calling getInputStream(): URLConnection con = new URL( url ).openConnection(); System.out.println( “orignal url: ” + con.getURL() ); con.connect(); System.out.println( “connected url: ” + con.getURL() ); InputStream is = con.getInputStream(); System.out.println( “redirected url: ” + con.getURL() ); is.close(); If you need to know whether the redirection happened before actually getting … Read more