Basically, there is a secured site, which I can log into and then add data via a form once I'm logged in. I can pass the login information in the URL, and I can get it to work if, for example, I type the address into my browser bar, like so:

https://my.example.com/authentication.php?user=Username&pass=PW&destination=https://someurl.example.com/Something

This will bring me to the page I want to post form data to. Which is great, except that if I use this url in my code, it doesn't work. It just brings me back to the initial login page. I should also say that if I use:

header("Location: https://my.example.com/authentication.php?user=Username&pass=PW&destination=https://someurl.example.com/Something");

I will be brought to the form that I want to post data to. The problem is that I don't want to actually redirect to the page, I just want to get to the form that I need to post information to.

If I attempt to call this URL in my code, it doesn't work. There are multiple redirects that occur if I type it in the address bar, and I've used FOLLOWLOCATION, 1, but that doesn't seem to help.

The latest iteration of code attempts to request the page that sets the cookies, and then make subsequent requests that include the cookies, but it still winds up kicking me back to the first page.

$cookie_jar = tempnam('/tmp','cookie');

$c = curl_init('https://my.example.com/authentication.php?user=Username&pass=PW');
curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($c, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($c, CURLOPT_COOKIEJAR, $cookie_jar);
$page = curl_exec($c);
curl_close($c);


$c = curl_init('https://someurl.example.com/Something');
curl_setopt($c, CURLOPT_POST, 1);
curl_setopt($c, CURLOPT_POSTFIELDS, 'field=value');
curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($c, CURLOPT_COOKIEFILE, $cookie_jar);
$page = curl_exec($c);
curl_close($c);

Can anyone help?

Recommended Answers

All 4 Replies

the site is probibly using sessions to store login information. Did you try to post the variables using:

$vars = 'user=Username&pass=PW'
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $vars);

It might help..

Thanks for the reply.

I think it actually had to do with the SSL certificate of the server I was posting to.

In any case, I was able to figure out the first part... I am now able to retrieve the form I want to post to, but now, I can't figure out how to actually *post* to that form....


Here's the code thus far:

$cookie_jar = tempnam('tmp/','cookie');

$url = "https://my.example.com/authentication.php";
$POSTFIELDS = 'username=UNAME&password=PW&destination=https://my.example.com/someotherpage
$reffer = "https://my.example.com/login/login.htm?destination=https://my.example.com/someotherpage";
$agent = "Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.4) Gecko/20030624 Netscape/7.1 (ax)";
$cookie_file_path = "tmp/"; 

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_USERAGENT, $agent);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,$POSTFIELDS);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_REFERER, $reffer);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file_path);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file_path);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
$result = curl_exec($ch);

curl_close($ch);

Now, I can pass the username and password, and it will redirect me to the form (https://my.example.com/someotherpage), but I can't figure out how to post to that form.

Any ideas?

Dear U Got Thsi Solution For The Curl Undefined Function Error

Bco Am Facing The Problem...
Curl Undefined

I Tried All The Step..

hi friends..

i am using cURL function for my project and its run great to pass username and password to re-directly on inner page of site by sending url like "www.exampele.com/mainpage.php".
but there is one problem when i pass Url having value to $_get as "www.exampele.com/result.php?key=anyvalue"

please anyone suggest me to solve this probleme to send Url with value using cURL funtion.


thank u....


santy.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.