How to login with cURL with POST and Cookie











up vote
7
down vote

favorite
1












The server successfully receives login, password and CAPTCHA. How do I do it with a cookie?



I don't know what info is needed for help i'll show all:



Login Form:



<div id="loginForm">
<div id="logo"><img src="logo.png" border="0" /></div>
<div class="loginBar">Login</div>
<form action="index.php" method="post">
<input type="hidden" name="p" value="login" />
<div class="line">
<label>Captcha</label>
<img src="captcha/securimage_show.php" border="0" />
</div>
<div class="line">
<label>&nbsp;</label>
<input type="text" name="captcha" value=""/>
</div>
<div class="line">
<label>&nbsp;</label>
<input type="submit" value="Login" />
</div>
</form>
<hr/>
<a href="index.php?p=register" class="register" title="Click to register">Register</a>
</div>


cURL code:



    extract($_POST);

//set POST variables
$proxy = '127.0.0.1:8118';
$url = 'http://example.com/index.php';
$fields_string= 'p=login&user=' . $user . '&pass=' . $passwd . '&captcha=' . $_POST['captcha'] . '&submit=Login';

//open connection
$ch = curl_init();

//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);
curl_setopt($ch,CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)");
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch,CURLOPT_PROXY, $proxy);
curl_setopt($ch,CURLOPT_COOKIEJAR, "cookie.txt");
curl_setopt($ch,CURLOPT_COOKIEFILE, "cookie.txt");
//curl_setopt($ch,CURLOPT_RETURNTRANSFER, true);
//execute post
$result = curl_exec($ch);

print_r(curl_error($ch));
print_r(curl_getinfo($ch));
print_r(curl_errno($ch));

//close connection
curl_close($ch);


curl getinfo:



[url] => http://example.com/index.php 
[content_type] => text/html
[http_code] => 200
[header_size] => 415
[request_size] => 325
[filetime] => -1
[ssl_verify_result] => 0
[redirect_count] => 0
[total_time] => 1.549389
[namelookup_time] => 3.7E-5
[connect_time] => 0.000138
[pretransfer_time] => 0.000142
[size_upload] => 62
[size_download] => 1585
[speed_download] => 1022
[speed_upload] => 40
[download_content_length] => 1585
[upload_content_length] => 0
[starttransfer_time] => 1.27051
[redirect_time] => 0
[certinfo] => Array ( )
[redirect_url] =>


Real cookie:



Host:                 example.com
Name: PHPSESSID
Path: /
Content: 7qk7bb17nr030g5j59h2gq3nq6
Content raw: 7qk7bb17nr030g5j59h2gq3nq6
Expires: At end of session
Expires raw: 0
Send for: Any type of connection
Send for raw: false
Created: Fri 25 Nov 2011 10:37:24 PM EET
Created raw: 1322253443569272
Last accessed: Sat 26 Nov 2011 11:06:02 AM EET
Last accessed raw: 1322298361723991
HTTP only: No
HTTP only raw: false
This domain only: No
This domain only raw: false
Policy: no information available
Policy raw: 0
Status: no information available
Status raw: 0
---


Gerenated file by libcurl in cookie.txt:



# Netscape HTTP Cookie File
# http://curl.haxx.se/rfc/cookie_spec.html
# This file was generated by libcurl! Edit at your own risk.

example.com FALSE / FALSE 0 PHPSESSID crs9cm100agdfsujsncr964jg7


When i run this code, Account, Passwd and Captcha successfull is received by server, but have error with cookie:




Make sure you've cookies enabled




Also i saw that real cookie Content raw is different by generated with libcurl.



Also i saw that from my form i successful catch the webserver cookie.



PS!! I receive cookie from this link in login form:



  <img src="captcha/securimage_show.php" border="0" />









share|improve this question
























  • Wasn't this exact same question asked earlier today? Looks like the original question was deleted. Just wondering what happened to it because it's obviously the same exact question and source.
    – davidethell
    Nov 27 '11 at 2:50










  • nobody response.. i just tried to find some tips( but also nosuccess.
    – Tedy
    Nov 27 '11 at 3:23










  • Have you tried just posting your data to the target site directly, rather than using CURL (IE, wrap your form in form tags and make the target the target URL "example.com/index.php")?
    – Jeff Davis
    Aug 26 '16 at 17:50















up vote
7
down vote

favorite
1












The server successfully receives login, password and CAPTCHA. How do I do it with a cookie?



I don't know what info is needed for help i'll show all:



Login Form:



<div id="loginForm">
<div id="logo"><img src="logo.png" border="0" /></div>
<div class="loginBar">Login</div>
<form action="index.php" method="post">
<input type="hidden" name="p" value="login" />
<div class="line">
<label>Captcha</label>
<img src="captcha/securimage_show.php" border="0" />
</div>
<div class="line">
<label>&nbsp;</label>
<input type="text" name="captcha" value=""/>
</div>
<div class="line">
<label>&nbsp;</label>
<input type="submit" value="Login" />
</div>
</form>
<hr/>
<a href="index.php?p=register" class="register" title="Click to register">Register</a>
</div>


cURL code:



    extract($_POST);

//set POST variables
$proxy = '127.0.0.1:8118';
$url = 'http://example.com/index.php';
$fields_string= 'p=login&user=' . $user . '&pass=' . $passwd . '&captcha=' . $_POST['captcha'] . '&submit=Login';

//open connection
$ch = curl_init();

//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);
curl_setopt($ch,CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)");
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch,CURLOPT_PROXY, $proxy);
curl_setopt($ch,CURLOPT_COOKIEJAR, "cookie.txt");
curl_setopt($ch,CURLOPT_COOKIEFILE, "cookie.txt");
//curl_setopt($ch,CURLOPT_RETURNTRANSFER, true);
//execute post
$result = curl_exec($ch);

print_r(curl_error($ch));
print_r(curl_getinfo($ch));
print_r(curl_errno($ch));

//close connection
curl_close($ch);


curl getinfo:



[url] => http://example.com/index.php 
[content_type] => text/html
[http_code] => 200
[header_size] => 415
[request_size] => 325
[filetime] => -1
[ssl_verify_result] => 0
[redirect_count] => 0
[total_time] => 1.549389
[namelookup_time] => 3.7E-5
[connect_time] => 0.000138
[pretransfer_time] => 0.000142
[size_upload] => 62
[size_download] => 1585
[speed_download] => 1022
[speed_upload] => 40
[download_content_length] => 1585
[upload_content_length] => 0
[starttransfer_time] => 1.27051
[redirect_time] => 0
[certinfo] => Array ( )
[redirect_url] =>


Real cookie:



Host:                 example.com
Name: PHPSESSID
Path: /
Content: 7qk7bb17nr030g5j59h2gq3nq6
Content raw: 7qk7bb17nr030g5j59h2gq3nq6
Expires: At end of session
Expires raw: 0
Send for: Any type of connection
Send for raw: false
Created: Fri 25 Nov 2011 10:37:24 PM EET
Created raw: 1322253443569272
Last accessed: Sat 26 Nov 2011 11:06:02 AM EET
Last accessed raw: 1322298361723991
HTTP only: No
HTTP only raw: false
This domain only: No
This domain only raw: false
Policy: no information available
Policy raw: 0
Status: no information available
Status raw: 0
---


Gerenated file by libcurl in cookie.txt:



# Netscape HTTP Cookie File
# http://curl.haxx.se/rfc/cookie_spec.html
# This file was generated by libcurl! Edit at your own risk.

example.com FALSE / FALSE 0 PHPSESSID crs9cm100agdfsujsncr964jg7


When i run this code, Account, Passwd and Captcha successfull is received by server, but have error with cookie:




Make sure you've cookies enabled




Also i saw that real cookie Content raw is different by generated with libcurl.



Also i saw that from my form i successful catch the webserver cookie.



PS!! I receive cookie from this link in login form:



  <img src="captcha/securimage_show.php" border="0" />









share|improve this question
























  • Wasn't this exact same question asked earlier today? Looks like the original question was deleted. Just wondering what happened to it because it's obviously the same exact question and source.
    – davidethell
    Nov 27 '11 at 2:50










  • nobody response.. i just tried to find some tips( but also nosuccess.
    – Tedy
    Nov 27 '11 at 3:23










  • Have you tried just posting your data to the target site directly, rather than using CURL (IE, wrap your form in form tags and make the target the target URL "example.com/index.php")?
    – Jeff Davis
    Aug 26 '16 at 17:50













up vote
7
down vote

favorite
1









up vote
7
down vote

favorite
1






1





The server successfully receives login, password and CAPTCHA. How do I do it with a cookie?



I don't know what info is needed for help i'll show all:



Login Form:



<div id="loginForm">
<div id="logo"><img src="logo.png" border="0" /></div>
<div class="loginBar">Login</div>
<form action="index.php" method="post">
<input type="hidden" name="p" value="login" />
<div class="line">
<label>Captcha</label>
<img src="captcha/securimage_show.php" border="0" />
</div>
<div class="line">
<label>&nbsp;</label>
<input type="text" name="captcha" value=""/>
</div>
<div class="line">
<label>&nbsp;</label>
<input type="submit" value="Login" />
</div>
</form>
<hr/>
<a href="index.php?p=register" class="register" title="Click to register">Register</a>
</div>


cURL code:



    extract($_POST);

//set POST variables
$proxy = '127.0.0.1:8118';
$url = 'http://example.com/index.php';
$fields_string= 'p=login&user=' . $user . '&pass=' . $passwd . '&captcha=' . $_POST['captcha'] . '&submit=Login';

//open connection
$ch = curl_init();

//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);
curl_setopt($ch,CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)");
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch,CURLOPT_PROXY, $proxy);
curl_setopt($ch,CURLOPT_COOKIEJAR, "cookie.txt");
curl_setopt($ch,CURLOPT_COOKIEFILE, "cookie.txt");
//curl_setopt($ch,CURLOPT_RETURNTRANSFER, true);
//execute post
$result = curl_exec($ch);

print_r(curl_error($ch));
print_r(curl_getinfo($ch));
print_r(curl_errno($ch));

//close connection
curl_close($ch);


curl getinfo:



[url] => http://example.com/index.php 
[content_type] => text/html
[http_code] => 200
[header_size] => 415
[request_size] => 325
[filetime] => -1
[ssl_verify_result] => 0
[redirect_count] => 0
[total_time] => 1.549389
[namelookup_time] => 3.7E-5
[connect_time] => 0.000138
[pretransfer_time] => 0.000142
[size_upload] => 62
[size_download] => 1585
[speed_download] => 1022
[speed_upload] => 40
[download_content_length] => 1585
[upload_content_length] => 0
[starttransfer_time] => 1.27051
[redirect_time] => 0
[certinfo] => Array ( )
[redirect_url] =>


Real cookie:



Host:                 example.com
Name: PHPSESSID
Path: /
Content: 7qk7bb17nr030g5j59h2gq3nq6
Content raw: 7qk7bb17nr030g5j59h2gq3nq6
Expires: At end of session
Expires raw: 0
Send for: Any type of connection
Send for raw: false
Created: Fri 25 Nov 2011 10:37:24 PM EET
Created raw: 1322253443569272
Last accessed: Sat 26 Nov 2011 11:06:02 AM EET
Last accessed raw: 1322298361723991
HTTP only: No
HTTP only raw: false
This domain only: No
This domain only raw: false
Policy: no information available
Policy raw: 0
Status: no information available
Status raw: 0
---


Gerenated file by libcurl in cookie.txt:



# Netscape HTTP Cookie File
# http://curl.haxx.se/rfc/cookie_spec.html
# This file was generated by libcurl! Edit at your own risk.

example.com FALSE / FALSE 0 PHPSESSID crs9cm100agdfsujsncr964jg7


When i run this code, Account, Passwd and Captcha successfull is received by server, but have error with cookie:




Make sure you've cookies enabled




Also i saw that real cookie Content raw is different by generated with libcurl.



Also i saw that from my form i successful catch the webserver cookie.



PS!! I receive cookie from this link in login form:



  <img src="captcha/securimage_show.php" border="0" />









share|improve this question















The server successfully receives login, password and CAPTCHA. How do I do it with a cookie?



I don't know what info is needed for help i'll show all:



Login Form:



<div id="loginForm">
<div id="logo"><img src="logo.png" border="0" /></div>
<div class="loginBar">Login</div>
<form action="index.php" method="post">
<input type="hidden" name="p" value="login" />
<div class="line">
<label>Captcha</label>
<img src="captcha/securimage_show.php" border="0" />
</div>
<div class="line">
<label>&nbsp;</label>
<input type="text" name="captcha" value=""/>
</div>
<div class="line">
<label>&nbsp;</label>
<input type="submit" value="Login" />
</div>
</form>
<hr/>
<a href="index.php?p=register" class="register" title="Click to register">Register</a>
</div>


cURL code:



    extract($_POST);

//set POST variables
$proxy = '127.0.0.1:8118';
$url = 'http://example.com/index.php';
$fields_string= 'p=login&user=' . $user . '&pass=' . $passwd . '&captcha=' . $_POST['captcha'] . '&submit=Login';

//open connection
$ch = curl_init();

//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);
curl_setopt($ch,CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)");
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch,CURLOPT_PROXY, $proxy);
curl_setopt($ch,CURLOPT_COOKIEJAR, "cookie.txt");
curl_setopt($ch,CURLOPT_COOKIEFILE, "cookie.txt");
//curl_setopt($ch,CURLOPT_RETURNTRANSFER, true);
//execute post
$result = curl_exec($ch);

print_r(curl_error($ch));
print_r(curl_getinfo($ch));
print_r(curl_errno($ch));

//close connection
curl_close($ch);


curl getinfo:



[url] => http://example.com/index.php 
[content_type] => text/html
[http_code] => 200
[header_size] => 415
[request_size] => 325
[filetime] => -1
[ssl_verify_result] => 0
[redirect_count] => 0
[total_time] => 1.549389
[namelookup_time] => 3.7E-5
[connect_time] => 0.000138
[pretransfer_time] => 0.000142
[size_upload] => 62
[size_download] => 1585
[speed_download] => 1022
[speed_upload] => 40
[download_content_length] => 1585
[upload_content_length] => 0
[starttransfer_time] => 1.27051
[redirect_time] => 0
[certinfo] => Array ( )
[redirect_url] =>


Real cookie:



Host:                 example.com
Name: PHPSESSID
Path: /
Content: 7qk7bb17nr030g5j59h2gq3nq6
Content raw: 7qk7bb17nr030g5j59h2gq3nq6
Expires: At end of session
Expires raw: 0
Send for: Any type of connection
Send for raw: false
Created: Fri 25 Nov 2011 10:37:24 PM EET
Created raw: 1322253443569272
Last accessed: Sat 26 Nov 2011 11:06:02 AM EET
Last accessed raw: 1322298361723991
HTTP only: No
HTTP only raw: false
This domain only: No
This domain only raw: false
Policy: no information available
Policy raw: 0
Status: no information available
Status raw: 0
---


Gerenated file by libcurl in cookie.txt:



# Netscape HTTP Cookie File
# http://curl.haxx.se/rfc/cookie_spec.html
# This file was generated by libcurl! Edit at your own risk.

example.com FALSE / FALSE 0 PHPSESSID crs9cm100agdfsujsncr964jg7


When i run this code, Account, Passwd and Captcha successfull is received by server, but have error with cookie:




Make sure you've cookies enabled




Also i saw that real cookie Content raw is different by generated with libcurl.



Also i saw that from my form i successful catch the webserver cookie.



PS!! I receive cookie from this link in login form:



  <img src="captcha/securimage_show.php" border="0" />






php forms cookies curl login






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jul 20 '15 at 3:49









Anthony

27.1k2084147




27.1k2084147










asked Nov 26 '11 at 21:37









Tedy

40117




40117












  • Wasn't this exact same question asked earlier today? Looks like the original question was deleted. Just wondering what happened to it because it's obviously the same exact question and source.
    – davidethell
    Nov 27 '11 at 2:50










  • nobody response.. i just tried to find some tips( but also nosuccess.
    – Tedy
    Nov 27 '11 at 3:23










  • Have you tried just posting your data to the target site directly, rather than using CURL (IE, wrap your form in form tags and make the target the target URL "example.com/index.php")?
    – Jeff Davis
    Aug 26 '16 at 17:50


















  • Wasn't this exact same question asked earlier today? Looks like the original question was deleted. Just wondering what happened to it because it's obviously the same exact question and source.
    – davidethell
    Nov 27 '11 at 2:50










  • nobody response.. i just tried to find some tips( but also nosuccess.
    – Tedy
    Nov 27 '11 at 3:23










  • Have you tried just posting your data to the target site directly, rather than using CURL (IE, wrap your form in form tags and make the target the target URL "example.com/index.php")?
    – Jeff Davis
    Aug 26 '16 at 17:50
















Wasn't this exact same question asked earlier today? Looks like the original question was deleted. Just wondering what happened to it because it's obviously the same exact question and source.
– davidethell
Nov 27 '11 at 2:50




Wasn't this exact same question asked earlier today? Looks like the original question was deleted. Just wondering what happened to it because it's obviously the same exact question and source.
– davidethell
Nov 27 '11 at 2:50












nobody response.. i just tried to find some tips( but also nosuccess.
– Tedy
Nov 27 '11 at 3:23




nobody response.. i just tried to find some tips( but also nosuccess.
– Tedy
Nov 27 '11 at 3:23












Have you tried just posting your data to the target site directly, rather than using CURL (IE, wrap your form in form tags and make the target the target URL "example.com/index.php")?
– Jeff Davis
Aug 26 '16 at 17:50




Have you tried just posting your data to the target site directly, rather than using CURL (IE, wrap your form in form tags and make the target the target URL "example.com/index.php")?
– Jeff Davis
Aug 26 '16 at 17:50












1 Answer
1






active

oldest

votes

















up vote
0
down vote













It could be cause of the missing attribute



curl_setopt($ch, CURLOPT_COOKIESESSION, TRUE);


This way curl will be forced to ignore 'old' session cookies and start a new session instead.






share|improve this answer





















  • with this parameter also reveice 2 deferent cookie. 1. in cookie.txt and 2. real cookie.
    – Tedy
    Nov 26 '11 at 23:42













Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f8281815%2fhow-to-login-with-curl-with-post-and-cookie%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
0
down vote













It could be cause of the missing attribute



curl_setopt($ch, CURLOPT_COOKIESESSION, TRUE);


This way curl will be forced to ignore 'old' session cookies and start a new session instead.






share|improve this answer





















  • with this parameter also reveice 2 deferent cookie. 1. in cookie.txt and 2. real cookie.
    – Tedy
    Nov 26 '11 at 23:42

















up vote
0
down vote













It could be cause of the missing attribute



curl_setopt($ch, CURLOPT_COOKIESESSION, TRUE);


This way curl will be forced to ignore 'old' session cookies and start a new session instead.






share|improve this answer





















  • with this parameter also reveice 2 deferent cookie. 1. in cookie.txt and 2. real cookie.
    – Tedy
    Nov 26 '11 at 23:42















up vote
0
down vote










up vote
0
down vote









It could be cause of the missing attribute



curl_setopt($ch, CURLOPT_COOKIESESSION, TRUE);


This way curl will be forced to ignore 'old' session cookies and start a new session instead.






share|improve this answer












It could be cause of the missing attribute



curl_setopt($ch, CURLOPT_COOKIESESSION, TRUE);


This way curl will be forced to ignore 'old' session cookies and start a new session instead.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 26 '11 at 23:14









jamm3r

454




454












  • with this parameter also reveice 2 deferent cookie. 1. in cookie.txt and 2. real cookie.
    – Tedy
    Nov 26 '11 at 23:42




















  • with this parameter also reveice 2 deferent cookie. 1. in cookie.txt and 2. real cookie.
    – Tedy
    Nov 26 '11 at 23:42


















with this parameter also reveice 2 deferent cookie. 1. in cookie.txt and 2. real cookie.
– Tedy
Nov 26 '11 at 23:42






with this parameter also reveice 2 deferent cookie. 1. in cookie.txt and 2. real cookie.
– Tedy
Nov 26 '11 at 23:42




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f8281815%2fhow-to-login-with-curl-with-post-and-cookie%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







這個網誌中的熱門文章

Tangent Lines Diagram Along Smooth Curve

Yusuf al-Mu'taman ibn Hud

Zucchini