Page 1 of 1

automatic login on server that implement http authentication

Posted: Mon Feb 12, 2007 3:35 am
by mario99ukdw
feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


hi all, i need to automatic login into a server that using form authentication method. is there any method to solve this problem.

i have all that i need to login ( username and password ). But the problem is when i entering the server at first time ( that i'm not authorized yet ) , we will get a login form popup ( it is an HTTP Authorization model ). I need to bypass this login using an auto login procedure.

i was thinking about sending an header request like this : ( PHP Code : i got this script from someone)

Code: Select all

<?php

// Get data from the open HTTP stream
function fromhttp ($fp) {
 $text = fgets($fp,4096);
 return $text;
}

// Send data to the open HTTP stream
function tohttp ($fp,$text) {
 fwrite($fp,$text);
}

// Assign user and password
$user = "user";
$pass = "pass";

// Set server IP (DSL router IP)
$server = "213.217.110.58";

// URL to request
$url = "/button_rec.gif";

// Create the Authorization header
$auth = "Authorization: Basic " .
 base64_encode($user . ":" . $pass);

$getrequest = <<<HTTP
GET $url HTTP/1.1
Accept: text/plain,text/html
Host: localhost
User-Agent: PHP
Connection: Close
$auth


HTTP;
// Note the two blank lines above; due to PHP heredoc idiosyncrasies,
// this creates one blank line in the data to be sent

// If we can open a connection (stream) to the router
if ($fp = fsockopen($server, 80, $errno, $errstr, 30)) {

 // Set our timeout appropriately
 stream_set_timeout ($fp,2);

 // Send the request to the HTTP stream
 tohttp($fp,$getrequest);

 // Get and print response from HTTP stream
 do {
 $temp = fromhttp($fp);
 echo $temp;
 } while (!empty($temp));

 // Close the connection
 fclose($fp);

} else {

 // Report connection failure
 print "Could not open connection to router!";

}

i run this script from localhost apache. and i got the correct file. it seeem this script bypass the login form
because we adding an info into the header

Code: Select all

// Create the Authorization header
$auth = "Authorization: Basic " .
 base64_encode($user . ":" . $pass);

$getrequest = <<<HTTP
GET $url HTTP/1.1
Accept: text/plain,text/html
Host: localhost
User-Agent: PHP
Connection: Close
$auth
But again , this is like a proxy, and i don't like it. it is still not solved the problem.
i need we do these script on the destination server ( IP : 213.217.110.58 ).

i was wondering if we can set the browser header to always adding authorization header automatically.

can anybody help me how to solved this problem??


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Posted: Mon Feb 12, 2007 9:26 am
by feyd
Why not make it easier on yourself by using cURL or Snoopy?