Home > Backend Development > PHP Tutorial > PHP simulates form post request to implement login

PHP simulates form post request to implement login

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-08-08 09:19:42
Original
1021 people have browsed it

The project needs to write a program that does not require form submission. I checked a lot of information and finally used CURL to simulate post submission. The sample program is as follows:

index.php

<?php
	header("Content-type:text/html;charset=utf-8");
	
	$stuid = "201300301013";
	$pwd = "111336";
	$uri = "127.0.0.1/login.php";//这里换成你服务器的地址
	// 参数数组
 	$data = array (
Copy after login
			&#39;stuid&#39; => $stuid,
 			'pwd' => $pwd			
 	);
	
	$ch = curl_init ();  //初始化curl
	
	curl_setopt ( $ch, CURLOPT_URL, $uri );
	curl_setopt ( $ch, CURLOPT_POST, 1 );  //使用post请求
	curl_setopt ( $ch, CURLOPT_HEADER, 0 );
	curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, 1 );
	curl_setopt ( $ch, CURLOPT_POSTFIELDS, $data);  //提交数据
	curl_setopt ( $ch, CURLOPT_FOLLOWLOCATION, true);  //重定向地址也输出
	$return = curl_exec ( $ch ); //得到返回值
	
	curl_close ( $ch );  //关闭
	
	print_r($return);  //输出返回值

?>
Copy after login
login.php

<?php
      $user = $_POST[&#39;stuid&#39;];
      $pwd = $_POST[&#39;pwd&#39;];
     &#160;
Copy after login
     /*
          这里对得到的数据进行处理
     */
      
      echo "成功!";  //最后输出结果

?>
Copy after login

The browser will output "success".

This is just a simple example. There are many things about curl that need to be studied. If you use PHP's curl to write a crawler, the mechanism is not very good. It is recommended to use java to implement the crawler program.

My own experience, for reference only!

Copyright Statement: This article is an original article by the blogger and may not be reproduced without the blogger's permission.
The above introduces the PHP simulation form post request to realize login, including the relevant content. I hope it will be helpful to friends who are interested in PHP tutorials.

Related labels:
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Issues
curl simulated login
From 1970-01-01 08:00:00
0
0
0
Convert cURL command line to PHP cURL code
From 1970-01-01 08:00:00
0
0
0
Convert command line cURL to PHP cURL
From 1970-01-01 08:00:00
0
0
0
How to set boolean value true in php curl
From 1970-01-01 08:00:00
0
0
0
Please tell me, php curl request page shows blank
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template