PHP/cURL POST problems?

shakey_jake

New member
Apr 8, 2007
40
0
0
I'm pretty new to this so not really too sure what's wrong so don't know how to describe the problem. But...basically what I'm attempting to do is login to a particular website and then submit a form that's on a second page.

I've managed to do this fine with loads of other sites, but this for some reason is giving some trouble.


The login form:

Code:
<form action="/lo/index.php" method="POST" enctype="multipart/form-data">
<table width="350" border="1" style="border-collapse: collapse" bordercolor="#5e74b3">
<tr>
<td align="center" class="ff">
<img src="templates/images/key.gif" border="0" width="48" height="48" align="absmiddle">
<span class="sectitle">Administration Entrance</span>
</td>
</tr>
<tr>
<td align="center" style="background: #fef0e5;">

<br>
<span class="ftitle">Password: </span>
<input type="password" name="password" size="15" maxlength="255" value="" class="text">
<Br><br>
<input type="hidden" name="redir" value="">
<input type="hidden" name="processlogin" value="1">
<input type="image" src="templates/images/login.gif" border="0" width="72" height="25" value="Login" class="submit"><br>
<input type="checkbox" name="saveinfo" value="save" checked>Save my info for Auto Login next time</td></tr>
</table>
</form>
...and the code I'm using to submit the login form...

Code:
<?php
    
    $lourl = 'http://www.domain.com';
    $lopass = 'password';

    $cookie = file('/tmp/cookie.txt');    
    $useragent = 'Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.5; en-US; rv:1.9.2.3) Gecko/20100401 Firefox/3.6.3';
    $lologinparameters = array('password' => $lopass, 'processlogin' => '1', 'saveinfo' => 'save');
    
    $ch = curl_init();

    curl_setopt($ch, CURLOPT_HEADER, FALSE);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($ch, CURLOPT_FAILONERROR, TRUE);
    curl_setopt($ch, CURLOPT_COOKIESESSION, TRUE);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
    curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie);
    curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie);
    curl_setopt($ch, CURLOPT_USERAGENT, $useragent);
    curl_setopt($ch, CURLOPT_URL, $lourl . '/lo/index.php');

    curl_setopt($ch, CURLOPT_POST, TRUE);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $lologinparameters);
    

    $lologin = curl_exec($ch);

    curl_close($ch);
    
    echo $linksorganizer;
?>
...which seems to work just fine and get me logged in.

The next part is what I'm having trouble with.

The form to submit (with all the shit cleaned out of it):

Code:
<form name="myform" action="/lo/index.php" method="POST" enctype="multipart/form-data" onsubmit="select_all_selectlist('sites');">

    <input type="text" name="name" value="" size="30" maxlength="255" class="text">

    <input type="text" name="email" value="" size="40" maxlength="255" class="text">

    <input type="text" name="icq" value="" size="20" maxlength="255" class="text">

    <select name="partnerid"><option value='0'>Select Partner</option></select>

    <input type="text" name="url" value="" size="50" maxlength="255" class="text">

    <input type="text" name="recip_url" value="" size="40" maxlength="255" class="text">

    <input type="text" name="abc_recip" value="" size="40" maxlength="255" class="text">

    <input type="text" name="anchor" value="" size="50" maxlength="255" class="text">

    <input type="text" name="title" value="" size="50" maxlength="255" class="text">    <a href="javascript:void(0);" onclick="copy_anchor()" class='helplink'>Copy from above</a>

    <textarea name="descr" cols="50" rows="4"></textarea>

    <script>
        var sitegrp=new Array();
        sitegrp[1]="1||http://www.siteone.com|#|2||http://www.sitetwo.com|#|3||http://www.sitethree.com";
        sitegrp[2]="4||http://www.sitefour.com|#|5||http://www.sitefive.com|#|6||http://www.sitesix.com";
    </script>

    <select name="sitecategories" onchange="display_sites_by_category(this.value);">
        <option value='0'>Select Category</option>
        <option value='-1'>All Categories</option>
        <option value='1'>Category 1</option>
        <option value='2'>Category 2</option>
    </select>

    <select name="tmpfeeds" id="tmpfeeds" multiple style="width:500px;" size="10" onDblClick="copySelectedOptions(document.getElementById('tmpfeeds'),document.getElementById('sites'))"></select>
                
    <select name="sites[]" id="sites" multiple size='10' style="width:500px;" onDblClick="removeSelectedOptions(document.getElementById('sites'))"></select><br>

    <a onclick="removeAllOptions(document.getElementById('sites'))">Deassign All</a><br><span class="helptext">(Multiple selection enabled. Hold CTRL and click on desired values)

    <select name="status">
        <option value='1'>Disabled</option>
        <option value='2' selected>Active</option>
    </select>

    <select name="link_check"><option value='0'>No</option><option value='1' selected>Yes</option></select>

    <select name="is_special"><option value='0' selected>No</option><option value='1'>Yes</option></select>

    <select name="invisible"><option value='0' selected>No</option><option value='1'>Yes</option></select>
    
    <input type="text" name="exptime" value="0" size="2" maxlength="255" class="text">
    
    <textarea name="comments" cols="60" rows="4"></textarea>
    
    <input type="checkbox" name="check_recip" value="1"> Yes
    
    <input type="hidden" name="addthetrade" value="1">

    <input id="submit" type="image" src="templates/images/add.gif" border="0" width="72" height="25" value="Add" class="submit">

</form>
...and the code I'm trying to submit it using...

Code:
<?php
    $webmastername = 'name';
    $webmasteremail = 'name@domain.com';
    $webmastericq = '123456789';
    $tradeurl = 'http://www.tradeurl.com';
    $recipurl = 'http://www.recipurl.com';
    $abcrecip = 'http://www.abcrecip.com';
    $anchor = 'Anchor Text';
    $title = 'Title Text';
    $description = 'Description of trade';
    $comments = 'Comments about trade';
    $addtradeto = '2';

    $loaddparameters = array(
        'name' => $webmastername,
        'email' => $webmasteremail,
        'icq' => $webmastericq,
        'url' => $tradeurl,
        'recip_url' => $recipurl,
        'abc_recip' => $abcrecip,
        'anchor' => $anchor,
        'title' => $title,
        'descr' => $description,
        'sites[]' => $addtradeto,
        'status' => '2',
        'link_check' => '1',
        'is_special' => '0',
        'invisible' => '0',
        'exptime' => '0',
        'comments' => $comments,
        'check_recip' => '0',
        'addthetrade' => '1',
        'expaction' => '1',
        'partnerid' => '0',
        'sitecategories' => ''
    );
        
    $ch = curl_init();

    curl_setopt($ch, CURLOPT_HEADER, FALSE);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($ch, CURLOPT_COOKIESESSION, TRUE);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
    curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie);
    curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie);
    curl_setopt($ch, CURLOPT_USERAGENT, $useragent);
    curl_setopt($ch, CURLOPT_URL, $lourl . '/lo/index.php');

    curl_setopt($ch, CURLOPT_POST, TRUE);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $loaddparameters);
    
    $addlotrade = curl_exec($ch);

    curl_close($ch);
?>
I've submitted to form manually and watched the output from Live HTTP Headers and afaik I've got everything in there.

Can anybody see any potential problems with it that could be causing it to not work?
 


Looks like your cookie code is messed up.

$cookie = file('/tmp/cookie.txt');
..
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie);

What you're doing is loading the cookie file into an array, then parsing it over. The problem is curl can't save back to an array. Change your code to just:

$cookie = '/tmp/cookie.txt';

Now it should stay logged in.
 
Looks like your cookie code is messed up.

$cookie = file('/tmp/cookie.txt');
..
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie);

What you're doing is loading the cookie file into an array, then parsing it over. The problem is curl can't save back to an array. Change your code to just:

$cookie = '/tmp/cookie.txt';

Now it should stay logged in.

That's what I had originally, but changed it to file('/tmp/cookie.txt'); after reading something somewhere in an attempt to get it working. Must have forgot to change it back.

...still not working though :(
 
...although I think the issue is with staying logged in as when I echo each curl request the first one loads up the page you'd see after you login, the second curl request just shows the login page :(
 
...although I think the issue is with staying logged in as when I echo each curl request the first one loads up the page you'd see after you login, the second curl request just shows the login page :(


Well yeah, your second script never sets $cookie
 
Well yeah, your second script never sets $cookie

Am I correct in thinking that $cookie only needs to be set the once?

BTW, should've said really, they're both the same script, just split them into 2 for the convenience of posting here. What the whole thing looks like is:

Code:
<?php
    
    $lourl = 'http://www.domain.com';
    $lopass = 'password';

    $cookie = file('/tmp/cookie.txt');    
    $useragent = 'Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.5; en-US; rv:1.9.2.3) Gecko/20100401 Firefox/3.6.3';
    $lologinparameters = array('password' => $lopass, 'processlogin' => '1', 'saveinfo' => 'save');
    
    $ch = curl_init();

    curl_setopt($ch, CURLOPT_HEADER, FALSE);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($ch, CURLOPT_FAILONERROR, TRUE);
    curl_setopt($ch, CURLOPT_COOKIESESSION, TRUE);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
    curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie);
    curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie);
    curl_setopt($ch, CURLOPT_USERAGENT, $useragent);
    curl_setopt($ch, CURLOPT_URL, $lourl . '/lo/index.php');

    curl_setopt($ch, CURLOPT_POST, TRUE);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $lologinparameters);
    

    $lologin = curl_exec($ch);

    curl_close($ch);
    
    
    
    $webmastername = 'name';
    $webmasteremail = 'name@domain.com';
    $webmastericq = '123456789';
    $tradeurl = 'http://www.tradeurl.com';
    $recipurl = 'http://www.recipurl.com';
    $abcrecip = 'http://www.abcrecip.com';
    $anchor = 'Anchor Text';
    $title = 'Title Text';
    $description = 'Description of trade';
    $comments = 'Comments about trade';
    $addtradeto = '2';

    $loaddparameters = array(
        'name' => $webmastername,
        'email' => $webmasteremail,
        'icq' => $webmastericq,
        'url' => $tradeurl,
        'recip_url' => $recipurl,
        'abc_recip' => $abcrecip,
        'anchor' => $anchor,
        'title' => $title,
        'descr' => $description,
        'sites[]' => $addtradeto,
        'status' => '2',
        'link_check' => '1',
        'is_special' => '0',
        'invisible' => '0',
        'exptime' => '0',
        'comments' => $comments,
        'check_recip' => '0',
        'addthetrade' => '1',
        'expaction' => '1',
        'partnerid' => '0',
        'sitecategories' => ''
    );
        
    $ch = curl_init();

    curl_setopt($ch, CURLOPT_HEADER, FALSE);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($ch, CURLOPT_COOKIESESSION, TRUE);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
    curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie);
    curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie);
    curl_setopt($ch, CURLOPT_USERAGENT, $useragent);
    curl_setopt($ch, CURLOPT_URL, $lourl . '/lo/index.php');

    curl_setopt($ch, CURLOPT_POST, TRUE);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $loaddparameters);
    
    $addlotrade = curl_exec($ch);

    curl_close($ch);
?>
 
The <?php at the start of the second chunk was misleading ;)

Set CURLOPT_VERBOSE to true, use CURLOPT_STDERR to send the output to a local file. You'll have something you can compare to live headers, or post here. From there it should be pretty easy to identify why the cookie isn't working.
 
The <?php at the start of the second chunk was misleading ;)

Set CURLOPT_VERBOSE to true, use CURLOPT_STDERR to send the output to a local file. You'll have something you can compare to live headers, or post here. From there it should be pretty easy to identify why the cookie isn't working.

Thanks Insomniac, turned out it wasn't saving the cookie running as a regular user, need to sudo to run it.
 
You can use this little JavaScript. Bookmark it and run it on any page. Helps me a lot when I'm making cURL apps.

Code:
javascript:if((a=document.forms).length>1){ x=prompt("Number of Forms: "+a.length+"\nEnter Index"); } else { x=0; }arr=[]; for(i=0;i<(e=a[x].elements).length;i++){ arr[i]=e[i].name+"="+e[i].value; } alert(a[x].action+"\n\n\n"+arr.join("&"))