Home >Backend Development >PHP Tutorial >PHP_CURL1 simulated POST login_PHP tutorial
CURL Introduction:
CURL allows you to connect and communicate with various servers using various types of protocols. Currently supported protocols include: http, https, ftp, gopher, telnet, dict, file, ldap, and also supports HTTPS authentication. HTTP POST, HTTP PUT, FTP upload (this can also be done through PHP's FTP extension), HTTP form-based upload, proxy, cookies and username + password authentication. (Excerpted from the manual)
In short, CURL is very powerful and can achieve many functions that the file_get_contents function cannot.
I won’t go into details about the principles, let’s talk about the code here.
CURL simulated login:
First, create two files in your project, login.php (submit login), validate.php (verify), code list:
login.php
<?php header('Content-type:text/html;Charset=utf-8'); $user = 'lee'; //登陆用户名 $pass = '123456'; //登陆密码 $va_url = 'http://localhost/validate.php'; //验证的 url 链接地址 $post_fields = "loginname={$user}&loginpass={$pass}"; //post提交信息串 $curl = curl_init(); //初始化一个cURL会话,必有 //curl_setopt()函数用于设置 curl 的参数,其功能非常强大,具体看手册 curl_setopt($curl, CURLOPT_URL, $va_url); //设置验证登陆的 url 链接 curl_setopt($curl, CURLOPT_RETURNTRANSFER, 0); //设置结果保存在变量中,还是输出,默认为0(输出) curl_setopt($curl, CURLOPT_POST, 1); //模拟post提交 curl_setopt($curl, CURLOPT_POSTFIELDS, $post_fields); //设置post串 $data = curl_exec($curl); //执行此cURL会话,必有 curl_close($curl); //关闭会话validate.php
<?php header('Content-Type:text/html;Charset=utf-8'); if ($_POST['loginname'] == 'lee' && $_POST['loginpass'] == '123456') { echo '<script>alert("登陆成功!");</script>'; } else { echo '<script>alert("登陆失败!");</script>'; }
Note: Original article, please indicate the source when reprinting: http://blog.csdn.net/liruxing1715/article/details/18551621