How to send login and password to this authentication system..?(automatic login)

Asked

Viewed 3,588 times

2

you very likely already know and/or have seen that old apache authentication system (htpasswd | image below).

inserir a descrição da imagem aqui

Well, I need to create a code that sends the login and password to the form and goes straight to the page. No need to type everything manually.

I know it is possible to do this in html forms using CURL, but in this one I have no idea.

Note: I want to realize login automatic, do not create that window. If anyone can help I’d appreciate it.

======================== Example using CURL and an HTML form:

<?php
  $curl = curl_init();

            curl_setopt ($curl, CURLOPT_URL, 'http://www.sitecomsistemaauth.com/index.php');
            curl_setopt ($curl, CURLOPT_POST, 1);
            curl_setopt ($curl, CURLOPT_POSTFIELDS, 'meulogin=abc&senha=123');
            curl_setopt ($curl, CURLOPT_FOLLOWLOCATION, true);
            curl_setopt ($curl, CURLOPT_COOKIEJAR, 'cookie.txt');
            curl_setopt ($curl, CURLOPT_RETURNTRANSFER, 1;

            // Variável que armazena o HTML renderizado após o login;
            $sendPost = curl_exec ($curl);

            // REGEX PARA PEGAR STRINGS DA PÁGINA
            $pattern = '@(.*)@';
            $target = $sendPost;
            $matches = array();

            $finalContent = $normalMode($pattern,$target,$matches);

            // Retorna todos os resultados encontrado;
            echo "<pre>";
            var_dump($finalContent,$matches);

            curl_close ($curl);
?>

This I can do in HTML form, but how to do in a basic "form" http Authentication?.

  • Is that something you want? http:///user:password@domain:port?

  • Hi, thanks for answering. So, I had already tried this method only it has some disadvantages, for example: It doesn’t work in IE and if I use inside php it would have to be something like "header("Location: http://user:password@domain")" and in this case it would go straight to the page and the php code would end. I want something to work the page after you understand? type store the page that comes after login in a variable and use regular expressions to manipulate the content.

  • So I didn’t understand your question. You could edit to make it clearer.

  • I edited it. Actually the doubt is very simple, maybe I’m actually expressing myself incorrectly. If I send the parameters via URL as in the example, the window will appear asking for login and password in the same way.

  • There is a link: (http://solvedstack.com/questions/how-do-i-make-a-request-using-http-basic-authentication-with-php-curl) but I don’t understand how it works.

1 answer

1


If you want to pass the credentials just do so:

curl_setopt($curl, CURLOPT_USERPWD, "$usuario:$senha");
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);

To use:

$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, "http://www.site.com/index.php");
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_USERPWD, "login:senha");
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
$output = curl_exec($curl);
curl_close($curl);

If the problem persists, change

curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);

For

curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_ANY);

If it is Digest access authentication

curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_DIGEST); 


Second solution

$headers = array(
    'Content-Type:text/html',
    'Authorization: Basic '. base64_encode("login:senha") // <---
);
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, "http://www.site.com/index.php");
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
$output = curl_exec($curl);
curl_close($curl);
  • Please avoid long discussions in the comments; your talk was moved to the chat

  • @utluiz he doesn’t have enough points

  • 1

    No problem. This is an automatic system message when I moved messages.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.