Hi,
I am trying to create a schedule on a website that requires login before processing a form that generates a schedule.
I have written a very basic form (form.html) that submits to a php script (create_schedule.php) that I have put together from various searches on Google.
What is supposed to happen is that we login to the main login page using username/password and save the cookie. We then use the same cookie so we appear to be loged in on another page that creates a schedule.
I have payed around with it for hours but what is happening is that on the first submission it fails and asks me to login. If I then submit the same form.html again it is successful.
Any ideas why the first attemp is not working?
Thank you.
form.html
create_schedule.php
I am trying to create a schedule on a website that requires login before processing a form that generates a schedule.
I have written a very basic form (form.html) that submits to a php script (create_schedule.php) that I have put together from various searches on Google.
What is supposed to happen is that we login to the main login page using username/password and save the cookie. We then use the same cookie so we appear to be loged in on another page that creates a schedule.
I have payed around with it for hours but what is happening is that on the first submission it fails and asks me to login. If I then submit the same form.html again it is successful.
Any ideas why the first attemp is not working?
Thank you.
form.html
Code:
<form action="create_schedule.php" method="POST">
<p class="style1">Create Schedule</p>
<p> <table>
<br>
</p>
<tr>
<td><span class="style2">Schedule Name:</span></td>
<td><input type="text" name="schedule_name"></td>
</tr>
<tr>
<td><input type="submit" name="SubmitForm" value="Send"></td>
</tr>
<form>
create_schedule.php
Code:
<?php
//Assign the variables from the form
$schedule_name=$_POST['schedule_name'];
//Login to the main website
$login_url = '[URL unfurl="true"]http://www.somedomain.com/login';[/URL]
//These are the post data username and password
$post_login = 'name=myusername&password=mypassword';
//Create a curl object
$ch = curl_init();
//Set the URL
curl_setopt($ch, CURLOPT_URL, $login_url );
//This is a POST query
curl_setopt($ch, CURLOPT_POST, true );
//Spoof the user-agent to be the browser that the user is on (and accessing the php script)
curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
//Set the post data
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_login);
//Set the cookie storing files
curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookie.txt');
curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie.txt');
//We want the content after the query
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
//Execute the action to login
curl_exec($ch);
// Close the connection to Free the memory
curl_close($ch);
//Login to create schedule webpage
//create array of data to be posted
// not sure if we need to post the followin two again
$post_data['name'] = "myusername";
$post_data['password'] = "mypassword";
// Name of the schedule
$post_data['rp[name]'] = "$schedule_name";
// Add a whole lot more fields
// ........
$post_data['create'] = 'Finish';
//End of array of data to be posted
//traverse array and prepare data for posting (key1=value1)
foreach ($post_data as $key => $value) {
$post_items[] = $key . '=' . $value;
}
//create the final string to be posted using implode()
$post_string = implode ('&', $post_items);
//create cURL connection
$ch = curl_init('[URL unfurl="true"]http://www.somedomain.com/create_schedule_page');[/URL]
//set options
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($ch, CURLOPT_USERAGENT,
"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)");
// Include our cookie.txt
curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookie.txt');
curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie.txt');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_REFERER, "[URL unfurl="true"]http://www.somedomain.com");[/URL]
//set data to be posted
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_string);
//perform our request
$result = curl_exec($ch);
//close the connection
curl_close($ch);
echo $result;
?>