tags:

views:

85

answers:

3

Hi,

I've seen this asked in several ways but can't find an answer that works.

I've created a page that logs into a remote .NET-based site using PHP/cURL. Works great as far as it goes -- I can get the data back from the logged-in page via curl_exec and do whatever with it but what I really need to do is to redirect the user to that page. Simply redirecting gives me a logged-out page on the remote site. I know the answer is in getting the cookie that's returned when cURL logs in, then passing it in a second call . . . somehow. I;m certain that someone must be doing this and can provide a working example. Please help -- this is driving me nuts!

Thanks,

Mark

A: 

If the site use cookies you could use cURL.

$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, "User=$user&passwd=$passwd");
curl_setopt ($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_NOBODY, TRUE);

$header = curl_exec($ch);

if (preg_match("/set-cookie:(.*)\n", $header, $m)) {
  $cookies = preg_replace('/(expires|path|domain)=[^;]*;?/i', '', $m[1]);
  header("Cookies: " . $cookies);
  header("Location: $url");
}

this should work but I don't test it.

jcubic
What is that? A Hollywood blockbuster scenario?
Col. Shrapnel
Quite glib, old thing.
mpemburn
+1  A: 

The problem is that the login will come from your PHP server, not from the end-user's IP address, so when you redirect the user, the site probably won't see them as logged in.

You can set do stuff like passing the cookie provided to the PHP server's login back to the end user, but if the redirected site is even slightly well-written, it won't fall for that.

If you have full control of both sites and they can access each-other's databases, you could write some back end code so the PHP server tells the .net site to expect the incoming user; that might work. But you'd need to do some back-end work in both systems.

If you don't have back-end access to the .Net site, then the only way I can see to do it would be to use the PHP site as a full proxy for the .Net site -- ie rather than doing a redirect, just display the output of the .net site directly via your PHP. This will have all kinds of other problems associated with it though.

Spudley
What if curl denies cookies, thus forcing the site to pass the session id around in the url? Ofcourse this assumes the site doesn't require cookies enabled, and that it doesn't check for changed ips as it should.
Fanis
I hate to hear that what I want to do isn't possible. CURL is saving valid cookie information on my server -- it matches the cookie that the remote site sends when you login in directly. So, I can't parse that up into appropriate format, set and send out in with a header("Cookies: " . $cookies); ?? Never say never.
mpemburn
The inability to read/write cookies across different domains is a fundamental protection against cross site scripting attacks.
konforce
A: 

No wonder you can't find an answer that works.
That's impossible.

Col. Shrapnel