开发者

How can I log in to remote site with PHP cURL, then redirect to that site

开发者 https://www.devze.com 2023-01-15 17:06 出处:网络
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

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 开发者_如何学Cwhatever 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


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


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消