php curl 模拟用户登陆https请求SSL实例代码

访问地址http://127.0.0.1/everyday/ssl/curl.php 代码如下

<?php 
/** curl 获取 https 请求 
* @param String $url 请求的url 
* @param Array $data 要发送的数据 
* @param Array $header 请求时发送的header 
* @param int $timeout 超时时间,默认30s 
*/ 
function curl_https($url, $data=array(), $header=array(), $timeout=30){
	$ch = curl_init(); 
	curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // 跳过证书检查 信任任何证书
	curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); // 0 域名存在与否都不验证了(1检查证书中是否设置域名) 
	curl_setopt($ch, CURLOPT_URL, $url); 
	curl_setopt($ch, CURLOPT_HTTPHEADER, $header); 
	curl_setopt($ch, CURLOPT_POST, true); 
	curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data)); 
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
	curl_setopt($ch, CURLOPT_TIMEOUT, $timeout); 

	$response = curl_exec($ch); 

	if($error=curl_error($ch)){ 
		die($error); 
	} 
	
	curl_close($ch); 
	return $response; 
} 

$url = 'https://localhost/everyday/ssl/login.php'; 
$data = array('username'=>'admin','password'=>'admin888'); 
$header = array(); 
$response = curl_https($url, $data, $header, 5); 
echo $response; 
?>

登陆地址https://localhost/everyday/ssl/login.php

<form action="#" method="post">  
    用户名:<input type="text" name="username" />  
    <br />  
    密码:<input type="password" name="password" />  
    <br />  
    <input type="submit" name="submit" value="登陆" />  
</form>  

<?php  
    if(isset($_POST["username"])){
        $user = $_POST["username"];  
        $psw = $_POST["password"];  
        if($user == "admin" && $psw == "admin888"){
			 echo "登陆成功!";  
		} else {
			 echo "户名或密码为空或者错误!";  
		}
           
	}
?>  


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值