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

2

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

Well,Ineedtocreateacodethatsendstheloginandpasswordtotheformandentersdirectonthepage.Noneedtomanuallyentereverything.

IknowthatitispossibletodothisinhtmlformsusingCURL,butinthatcaseIhavenoidea.

Note:Iwanttoperformloginautomatically,donotcreatethiswindow.IfanyonecanhelpIthank.

========================ExampleusingCURLandanHTMLform:

<?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 "form" http basic authentication?.

    
asked by anonymous 05.08.2015 / 23:56

1 answer

1

If you want to pass the credentials just do it like this:

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 you continue to persist, change

curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);

By

curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_ANY);

If you are 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);
    
06.08.2015 / 00:39