PHP_CURL1之模拟POST登陆
CURL简介:
CURL允许你与各种的服务器使用各种类型的协议进行连接和通讯,目前支持的协议包括:http、https、ftp、gopher、telnet、dict、file、ldap,同时也支持HTTPS认证、HTTP POST、HTTP PUT、 FTP 上传(这个也能通过PHP的FTP扩展完成)、HTTP 基于表单的上传、代理、cookies和用户名+密码的认证。(摘自手册)
总之 CURL 功能非常强大,能实现很多 file_get_contents 函数所不能实现的功能。
原理性的东西不再赘述,这里代码来说话。
CURL模拟登陆:
首先,在你的项目中建立两个文件,login.php(提交登陆)、validate.php(验证),代码清单:
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); //关闭会话
<?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>'; }
登陆成功,JS 弹出“登陆成功”;登陆失败,JS弹出“登陆失败”。
注:原创文章,转载请注明出处:http://blog.csdn.net/liruxing1715/article/details/18551621

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











Python simulates the browser sending post requests importrequests format request.postrequest.post(url,data,json,kwargs)#post request format request.get(url,params,kwargs)#Compared with get request, sending post request parameters are divided into forms ( x-www-form-urlencoded) json (application/json) data parameter supports dictionary format and string format. The dictionary format uses the json.dumps() method to convert the data into a legal json format string. This method requires

1. Java calls post interface 1. Use URLConnection or HttpURLConnection that comes with java. There is no need to download other jar packages. Call URLConnection. If the interface response code is modified by the server, the return message cannot be received. It can only be received when the response code is correct. to return publicstaticStringsendPost(Stringurl,Stringparam){OutputStreamWriterout=null;BufferedReaderin=null;StringBuilderresult=newSt

For PHP developers, using POST to jump to pages with parameters is a basic skill. POST is a method of sending data in HTTP. It can submit data to the server through HTTP requests. The jump page processes and jumps the page on the server side. In actual development, we often need to use POST with parameters to jump to pages to achieve certain functional purposes.

实现如下:server{listen80;listen443ssl;server_namenirvana.test-a.gogen;ssl_certificate/etc/nginx/ssl/nirvana.test-a.gogen.crt;ssl_certificate_key/etc/nginx/ssl/nirvana.test-a.gogen.key;proxy_connect_timeout600;proxy_read_timeout600;proxy_send_timeout600;c

PHP is a widely used server-side scripting language that can be used to create interactive and dynamic web applications. When developing PHP applications, we usually need to submit user input data to the server for processing through forms. However, sometimes we need to determine whether form data has been submitted in PHP. This article will introduce how to make such a determination.

PHP is a programming language widely used in website development, and page jumps and carrying POST data are common requirements in website development. This article will introduce how to implement PHP page jump and carry POST data, including specific code examples. In PHP, page jumps are generally implemented through the header function. If you need to carry POST data during the jump process, you can do it through the following steps: First, create a page containing a form, where the user fills in the information and clicks the submit button. Acti in the form

Machine learning makes computer graphics (CG) simulations more realistic! The method is called Neural Flow Maps (NFM), which can accurately simulate smoke with four vortices: more complex ones can also be easily realized: you know, in this era of AI applications flying all over the sky, CG physics simulation is still It is the world of traditional numerical algorithms. △NFM simulates "leapfrog". Although the application of neural networks in CG can create dazzling visual effects, it cannot strictly and robustly describe physical properties. △NFM simulates "ink droplets" It is precisely for this reason that physical simulation based on neural networks is still in the proof of concept stage, and the effects generated are far from SOTA. In order to solve this complex problem,

Title: PHP code example: How to use POST to pass parameters and implement page jumps In web development, it often involves the need to pass parameters through POST and process them on the server side to implement page jumps. PHP, as a popular server-side scripting language, provides a wealth of functions and syntax to achieve this purpose. The following will introduce how to use PHP to implement this function through a practical example. First, we need to prepare two pages, one to receive POST requests and process parameters
