2013-05-06 3 views
5

소켓을 사용하여 보안 로그인 프로그램을 만들려고합니다. 내가 그것을 실행하면소켓으로 보안 로그인

<?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"; 
} 

?> 

, 다음과 같은 경고가 반환됩니다 : 여기에 내가 쓴 코드는

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]

문제 I는 SSL 프로토콜을 사용하기위한 명령을 제거하면이 코드가 제대로 작동한다는 것입니다 fsockopen() 기능이 있지만 보안 HTTP 연결을 구현해야합니다.

내가 틀린 곳을 말해 줄 수있는 사람에게는 매우 감사 할 것입니다. 감사합니다.

+3

$ port = 443; – Aivar

+0

난 이미이 옵션을 시도했지만 그런 식으로 다른 오류를 반환합니다 : fsockopen() : ssl에 연결할 수 없습니다 : // localhost : 443 – prisca

+0

그리고 localhost https가 올바르게 실행되고 있는지 확인 하시겠습니까? – Aivar

답변

2

내가 원하는 것을 정확히 지정하기 위해 여기에 약간의 변경을 제안 할 것입니다.

if (!$sock=fsockopen("ssl://".$host,$port,$errnum,$errstr,$timeout)) 

SSL은 기본이 될 것이다 포트 80을 사용하기 때문에 443

그것은 알려져있다으로 사용 SSL의 버전이 아닌 경우 일부 시스템에서 문제를 야기 할 수 $ 포트를 지정하지 않습니다 지정된, 그리고 그것을 감지하려고 PHP에 의존합니다. 나는 서버가 SSLv2의 또는 SSLv3에를 사용하는 경우 따라 하나

if (!$sock=fsockopen("sslv2://".$host,443,$errnum,$errstr,$timeout)) 

또는

if (!$sock=fsockopen("sslv3://".$host,443,$errnum,$errstr,$timeout)) 

을 시도하는 것이 좋습니다 것입니다.

이렇게하면 모호성이 제거되고 서버에 진행 방법을 명확하게 지시합니다.