mark- PHP_CURL1之模拟POST登陆

派生到我的代码片

 
  1. <?php  
  2. header('Content-type:text/html;Charset=utf-8');  
  3. $user = 'lee';       //登陆用户名  
  4. $pass = '123456';    //登陆密码  
  5. $va_url = 'http://localhost/validate.php';            //验证的 url 链接地址  
  6. $post_fields = "loginname={$user}&loginpass={$pass}"; //post提交信息串  
  7. $curl = curl_init(); //初始化一个cURL会话,必有  
  8. //curl_setopt()函数用于设置 curl 的参数,其功能非常强大,具体看手册  
  9. curl_setopt($curl, CURLOPT_URL, $va_url);      //设置验证登陆的 url 链接  
  10. curl_setopt($curl, CURLOPT_RETURNTRANSFER, 0); //设置结果保存在变量中,还是输出,默认为0(输出)  
  11. curl_setopt($curl, CURLOPT_POST, 1);           //模拟post提交  
  12. curl_setopt($curl, CURLOPT_POSTFIELDS, $post_fields); //设置post串  
  13. $data = curl_exec($curl);  //执行此cURL会话,必有  
  14. //检查是否有错误  
  15. if(curl_errno($curl)) {  
  16.     exit('Curl error: ' . curl_error($curl));  
  17. }  
  18. curl_close($curl);         //关闭会话  


 

validate.php

 

 

[php] view plaincopy在CODE上查看代码片派生到我的代码片
 
  1. <?php  
  2. header('Content-Type:text/html;Charset=utf-8');  
  3. if ($_POST['loginname'] == 'lee' && $_POST['loginpass'] == '123456') {  
  4.     echo '<script>alert("登陆成功!");</script>';  
  5. else {  
  6.     echo '<script>alert("登陆失败!");</script>';  
  7. }  


登陆成功,JS 弹出“登陆成功”;登陆失败,JS弹出“登陆失败”。

注:原创文章,转载请注明出处:http://blog.csdn.net/liruxing1715/article/details/18551621

posted @ 2015-11-22 21:39  longerhe123  阅读(169)  评论(0编辑  收藏  举报