Posts Tagged http

How to use basic authentication on GAE with PHP

As you probably noticed PHP in Google Application Engine does not provide the variables $_SERVER[‘PHP_AUTH_USER’] and $_SERVER[‘PHP_AUTH_PW’]. You can perform basic authentication anyway using the variable $_SERVER[‘HTTP_AUTHORIZATION’]

PHP code to parse HTTP authorization header looks the following way:

 $nobasic = substr($_SERVER['HTTP_AUTHORIZATION'],6); // removing 'Basic ' from the string
 $decoded = base64_decode($nobasic); // decoding string with user:password
 list($client_user, $client_pass) = explode(':',$decoded);
 if ($client_user == "username" && $client_pass == "password") {
   // Successfully authenticated
 } else {
   // Authentication failed, username or password are wrong
} else {
 // Not authenticated as there is no appropriate header in HTTP request

You can check if your authentication is working sending appropriate header in HTTP request, check here to get the information how to form the header

, , , , , , , ,

Leave a comment

How to create manually string for basic http authentication

If you need to test Basic authentication, you can manually add header like this into HTTP request:

Authorization: Basic dXNlcm5hbWU6cGFzc3dvcmQ=

The line after Basic is formed from your username and password the following way in Mac or Linux command line:

echo -n "username:password"  | openssl base64 -base64

On the other hand, you can use “curl” directly from command line to call the request with basic authentication, use the following command (it is GET in the example below):

curl --user username:password https://your_webservice_here

, , , ,

1 Comment