Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

cURL Cookie Problem

Status
Not open for further replies.

uniopp

Technical User
Oct 7, 2001
152
0
0
JP
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
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;
?>
 
This is strange !!
If I duplicate the first cURL object it works fine.
So I now have three objects -
First one to logon (sets the session in the cookie.txt but not the user details)
Second one seems to add the user details to the above session cookie.txt,
Third one to process my requirements as a logged in user.
Is it normal on some websites to need this????
Thanks.
 
you have not given us enough details to answer you.

we would need the website in question. you need to examine the headers received for each set of posted data to see what was going on. i have a suspicion that the login form might be behind an ssl connection and that the https variant may be delivering a separate cookie. examine the headers to be sure.

you might also try this code although I am guessing at your requirements

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]
$agent = empty($_SERVER['HTTP_USER_AGENT']) ? "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)" : $_SERVER['HTTP_USER_AGENT'];
$options = array(
					CURLOPT_USERAGENT=>$_SERVER['HTTP_USER_AGENT'],
					CURLOPT_COOKIEFILE=>'cookiefile.txt',
					CURLOPT_COOKIEJAR=>'cookiejar.txt',
					CURLOPT_RETURNTRANSFER=>true,
					CURLOPT_FOLLOWLOCATION=>true,
					CURLOPT_VERIFYPEER=>false,
					CURLOPT_CONNECTTIMEOUT=>30);

$ch = curl_init($login_url);

//get login cookies
curl_setopt_array($ch, $options);
$data = curl_exec($ch);

//now post login data

$postData = array(	'name'=>'my user name',
					'password'=> 'my password');
					
$postOptions = array(
					CURLOPT_POST=>true,
					CURLOPT_POSTFIELDS=>$postData);

$lastPage = $login_url;

//log in
curl_setopt_array($ch, array_merge($options, $postOptions));
$result = curl_exec($ch);

//you should check here whether the result is as you expect

//create schedule page
$postData = array(
					'Finish'=>true,
					'others'=>'others');	//set key=>value pairs for the fields to complete
curl_setopt_array($ch, 
						array(	
							CURLOPT_URL=>'[URL unfurl="true"]http://www.somedomain.com/create_schedule_page',[/URL]
							CURLOPT_POSTFIELDS => $postData,
							CURLOPT_REFERER => $lastPage
							)
				);

//perform our request
$result = curl_exec($ch);

//close the connection
curl_close($ch);
echo $result;
?>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top