doumao1917 2014-11-07 21:59
浏览 80
已采纳

如何在PHP中使用cURL发送post请求后获取/重定向到下一页?

This code given below is sending the correct post request. Problem is, whenever is assign incorrect username and password to uname and pword respectively is shows the correct output which 'singin1.php' page with the message 'Wrong Username or Password"(stored in $result) but when correct username and password are provided, it shown me the same 'signin1.php' page. It does not show me the authorized screen which i should get after logging in.

<?php
//set POST variables
$url = 'http://computerinfo.in/school/signin1.php';

$fields = array(
                        'uname' => 'username',
            'pword' => 'password',
            'submt' => 'Submit'
                );

//url-ify the data for the POST
foreach($fields as $key=>$value) { $fields_string[]= $key.'='.$value;}
$fields_items = implode ('&', $fields_string);


//open connection
$ch = curl_init();
//fname%3Dasdf%26lname%3Dsdafasdf%26lolz%3DSubmit
//set the url, number of POST vars, POST data

curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_items);

//execute post
$result = curl_exec($ch);
echo "<br>Field Item= "."<br>";
echo $result."=Result"."</br>";


echo curl_errno($ch) . '<br>' . 
            curl_error($ch);

//close connection
curl_close($ch);

?>

I want to know if i am missing something in order to get the next screen , which i should see after normal log in. The above code is present at http://computerinfo.in/school/test.php and code with correct username and password is present at http://computerinfo.in/school/test1.php

and link of sign in page is http://computerinfo.in/school/signin1.php

Structure of signin.php

    if(isset($_POST['submt']))
    {

        if(uname && pword are correct)
        {
            //creating session using session_start();  and redirection using header;
        }
        else
        {           
            $message="Wrong Username or Password";
        }

signin.php is working fine , if i am providing username and password manually.

  • 写回答

1条回答 默认 最新

  • dongqingchan2385 2014-11-07 22:48
    关注

    As 'singin1.php' page is using redirection with header, and session. It is compulsory to tell cURL to follow redirection and to fetch cookies.

    To Follow redirection , this line should be added

    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    

    and to fetch cookies and create session.

    curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie.txt');
    curl_setopt($ch, CURLOPT_COOKIEFILE,  'cookie.txt');
    

    Final code for test.php will look like

    <?php
    //set POST variables
    $url = 'http://computerinfo.in/school/signin1.php';
    
    $fields = array(
                            'uname' => 'username',
                'pword' => 'password',
                'submt' => 'Submit'
                    );
    
    //url-ify the data for the POST
    foreach($fields as $key=>$value) { $fields_string[]= $key.'='.$value;}
    $fields_items = implode ('&', $fields_string);
    
    
    //open connection
    $ch = curl_init();
    
    //set the url, number of POST vars, POST data
    
    curl_setopt($ch,CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); //NEW LINE
    curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_items);
    curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie.txt'); //NEW LINE
    curl_setopt($ch, CURLOPT_COOKIEFILE,  'cookie.txt'); // NEW LINE
    //execute post
    $result = curl_exec($ch);
    
    echo "<br>Field Item= "."<br>";
    echo $result."=Result"."</br>";
    
    
    echo curl_errno($ch) . '<br>' . 
                curl_error($ch);
    //close connection
    curl_close($ch);
    
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?