Sto tentando di creare un programma di accesso sicuro utilizzando i socket. Ecco il codice che ho scritto:Accesso protetto con socket
<?php
$myusername=$_POST["username"];
$mypassword=$_POST["password"];
$host="localhost";
$port=80;
$timeout=60;
$target="/admin_area.php";
if($myusername=="admin" && $mypassword=="passwd")
{
if (!$sock=fsockopen("ssl://".$host,$port,$errnum,$errstr,$timeout))
{
die ("Could not open socket: [$errnum] $errstr");
}
else
{
$posted_vars=array("username"=>$myusername,
"password"=>$mypassword);
$body="";
foreach ($posted_vars as $parameter=>$value)
{
$body.="&".$parameter."=".$value;
}
$headers="POST ".$target." HTTP/1.0 \r\n";
$headers.="Content-Type: application/x-www-form-urlencoded \r\n";
$headers.="Content-Length: ".strlen($body)." \r\n";
$headers.="Connection: Keep-Alive \r\n";
$headers.="Authorization: Basic ".base64_encode($myusername.":".$mypassword)." \r\n\r\n";
fputs ($sock,$headers.$body);
$data="";
while (!feof ($sock))
{
$data.=fgets($sock,3000);
}
list($res_head,$res_body)=explode("\r\n\r\n",$data);
echo $res_body;
}
}
else
{
echo "Login not happened successfully";
}
?>
quando l'eseguo, i seguenti avvisi vengono restituiti:
Warning: fsockopen(): SSL operation failed with code 1. OpenSSL Error messages: error:140770FC:SSL routines:SSL23_GET_SERVER_HELLO:unknown protocol in C:\wamp\www\log_in.php on line 12
Warning: fsockopen(): Failed to enable crypto in C:\wamp\www\log_in.php on line 12
Warning: fsockopen(): unable to connect to ssl://localhost:80 (Unknown error) in C:\wamp\www\log_in.php on line 12
Could not open socket: [0]
Il problema è che questo codice funziona correttamente se tolgo le istruzioni per l'utilizzo del protocollo SSL in la funzione fsockopen()
, ma avrei bisogno di implementare una connessione HTTP sicura.
Sarei molto grato a chiunque possa dirmi dove mi sbaglio. Grazie!
non si dovrebbe usare $ port = 443; – Aivar
Ho già provato questa opzione, ma in questo modo restituisce un altro errore: fsockopen(): impossibile connettersi a ssl: // localhost: 443 – prisca
E sei sicuro che https localhost sia installato correttamente? – Aivar