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

PHP simulates form post request to implement login

WBOY
WBOYOriginal
2016-08-08 09:19:42948browse

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 (
			&#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);  //输出返回值

?>
login.php

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

?>

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.

Statement:
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