php cURL - POST Not working
Your form on home2.php
looks like this:
<form method="POST" action="login.php" id="formLogin" name="formLogin"> Usuario <input name="login" type="text"> Senha <input name="senha" type="text"> <input type="submit"></form>
It's important to note that the username input box is named login
, yet when you create the cURL request, you're sending the username as usario
. This needs to be changed. You also need to emulate what a browser would do and send the post variables towards login.php
(action="login.php"
) instead of home2.php
(this page only includes the form, and never receives any of the input form data).
All in all, your code should be fixable by:
$ch = curl_init();curl_setopt($ch, CURLOPT_URL, 'http://localhost:8888/curl/login.php');// Change the URL ^^^^^curl_setopt ($ch, CURLOPT_POST, 1);curl_setopt ($ch, CURLOPT_POSTFIELDS, 'login=teste&senha=12345');// Change the username key ^^^^^curl_setopt ($ch, CURLOPT_COOKIEJAR, 'cookie.txt');curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);$store = curl_exec ($ch);print_r(curl_getinfo($ch));echo curl_errno($ch) . '-' . curl_error($ch);echo $store;
If you're ever in doubt, the network tab in your browser (or any HTTP packet sniffer) will tell you exactly what requests the browser makes, with what variables, towards what URLs, and you'll be able to replicate it way easier.
Note: If your success.php
page that login.php
apparently redirects to on success does anything at all, you'll also need to have this be called automatically.
If login.php
automatically redirects, you can just use the cURL option CURLOPT_FOLLOWLOCATION
set to 1.