What character encoding should I use for a HTTP header?

In short: Only ASCII is guaranteed to work. Some non-ASCII bytes are allowed for backwards compatibility, but are not supposed to be displayable. HTTPbis gave up and specified that in the headers there is no useful encoding besides ASCII: Historically, HTTP has allowed field content with text in the ISO-8859-1 charset [ISO-8859-1], supporting other charsets … 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 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

Set HTTP header for one request

There’s a headers parameter in the config object you pass to $http for per-call headers: $http({method: ‘GET’, url: ‘www.google.com/someapi’, headers: { ‘Authorization’: ‘Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==’} }); Or with the shortcut method: $http.get(‘www.google.com/someapi’, { headers: {‘Authorization’: ‘Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==’} }); The list of the valid parameters is available in the $http service documentation.

Removing X-Powered-By

I think that is controlled by the expose_php setting in PHP.ini: expose_php = off Decides whether PHP may expose the fact that it is installed on the server (e.g. by adding its signature to the Web server header). It is no security threat in any way, but it makes it possible to determine whether you … Read more

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

Adding headers to requests module

From http://docs.python-requests.org/en/latest/user/quickstart/ url=”https://api.github.com/some/endpoint” payload = {‘some’: ‘data’} headers = {‘content-type’: ‘application/json’} r = requests.post(url, data=json.dumps(payload), headers=headers) You just need to create a dict with your headers (key: value pairs where the key is the name of the header and the value is, well, the value of the pair) and pass that dict to the headers … Read more