0

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.

CyberBoy
  • 745
  • 1
  • 7
  • 31
  • The code, probably, relies on sessions. Store and use session cookie, make initial request to load the page with form, get cookie, use it with posted data. – Cheery Nov 07 '14 at 22:10
  • Yups , I have added the abstract code for signin1.php page also in question.... get cookies and using it with posted data... can you give a hint how to do it , or any tutorial.... I am new to cURL. Thank You – CyberBoy Nov 07 '14 at 22:19
  • `and redirection using header;` you have to tell CURL to follow the redirection. `curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);`, but still, in order to work with sessions you have to store cookies. – Cheery Nov 07 '14 at 22:23
  • Great, Now I can follow the redirection. Problem remains: how to get the cookies and can i store the cookies in browser, and log in to the system ? – CyberBoy Nov 07 '14 at 22:31
  • Not sure why you want to store those cookies in browser. To store them and to use with curl look at http://stackoverflow.com/questions/12885538/php-curl-and-cookies, for example. – Cheery Nov 07 '14 at 22:34
  • Problem Solved (y). I working on something else, this example was to explain my problem . – CyberBoy Nov 07 '14 at 22:42

1 Answers1

0

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);

?>
CyberBoy
  • 745
  • 1
  • 7
  • 31