简体   繁体   English

PHP:在普通的Web浏览器上使用登录的cURL会话

[英]PHP: use logged-in cURL session on normal web browser

Good evening! 晚上好!

I've an script in PHP which makes a CURL call to a remote host loggin page. 我在PHP中有一个脚本,可以对远程主机登录页面进行CURL调用。 After loggin in and keeping the session via cookiejar opt and cookiefile opt, I use the same CURL connection handler to loggin in on to the immediatly next page wich needs an upload. 登录并通过cookiejar opt和cookiefile opt保持会话后,我使用相同的CURL连接处理程序登录到下一个需要上传的页面。 When it's done, I got the full session parameters and I can call any page I want from the site, but IN CURL! 完成后,我获得了完整的会话参数,并且可以从站点中调用任何我想要的页面,但是在CURL中!

The idea, is that this script wich uses CURL, needs to finally be redirected to one of those pages in the remote host using the CURL session, but this is not possible, because from curl you can not show the results as a redirected page. 想法是,此脚本使用CURL,最终需要使用CURL会话重定向到远程主机中的那些页面之一,但这是不可能的,因为从curl您无法将结果显示为重定向页面。

So I've tried alot of options. 所以我尝试了很多选择。 None of em works at all. em根本不起作用。

Schema: 架构:

  • PHP script on a local server. 本地服务器上的PHP脚本。
  • Call to domain.com/loggin.php (creates curl ch) 调用domain.com/loggin.php(创建curl ch)
  • Keep curl session on cookie.txt file. 将curl会话保持在cookie.txt文件上。
  • Call to domain.com/loggin_2.php with the same ch (non closed last one). 使用相同的ch调用domain.com/loggin_2.php(未关闭最后一个)。
  • Full logged in on the remote site. 已完全登录到远程站点。
  • Back to the PHP script. 回到PHP脚本。 Need to redirect to domain.com/index.php, wich needs Session variables filled in with the full login process. 需要重定向到domain.com/index.php,这需要使用完整的登录过程填充Session变量。

What to do then? 那该怎么办?

1) After having full loggin in, read cookies.txt file to get PHPSESSID. 1)完全登录后,阅读cookies.txt文件以获取PHPSESSID。 Then tried to use setcookie(), or via header("Set-cookie: ...") and immediatly after, using header("Location: domain.com/index.php"). 然后尝试使用setcookie()或通过header(“ Set-cookie:...”)以及之后立即使用header(“ Location:domain.com/index.php”)。

Doesn't work. 不起作用

2) Tried same thing via ajax call and finally document.cookie = ... 2)通过ajax调用并最终document.cookie = ...

Doesn't work. 不起作用

3) Adding a third cURL call to a file in my remote host wich prints a JSONED $_SESSION. 3)向我的远程主机中的文件添加第三个cURL调用,将打印JSONED $ _SESSION。 Getting it on my PHP script, decoding it and loaded on my local session via foreach on any array value (foreach()...$_SESSION[$c] = $v). 将其获取到我的PHP脚本中,对其进行解码并通过任何数组值上的foreach加载到我的本地会话中(foreach()... $ _ SESSION [$ c] = $ v)。 Added a session_start() before this foreach. 在此foreach之前添加了session_start()。 And immediatly after, a header("Loaction: domain.com/index.php"). 紧随其后的是标题(“ Loaction:domain.com/index.php”)。

Doesn't work. 不起作用

4) Added a session_write_close() before the header("Loaction: domain.com/index.php"). 4)在标头(“位置:domain.com/index.php”)之前添加了一个session_write_close()。

Doesn't work. 不起作用

So I don't really know how to use the CURL session. 所以我真的不知道如何使用CURL会话。

I've tried to manually fix the PHPSESSID via Web Developer Firefox plugin. 我试图通过Web Developer Firefox插件手动修复PHPSESSID。 And I wrote down the curl generated session id. 我记下了curl生成的会话ID。 It perfeclty works. 完美的作品。 So, It should be possible to fix it via scripting on my php script! 因此,应该可以通过在我的PHP脚本上编写脚本来修复它! But I can't! 但是我不能!

Give me a hand, please! 请帮帮我!

Thanks! 谢谢!

I may have gotten lost a bit, but I think I understand. 我可能有点迷路,但我想我明白。 You can use CURLOPT_HEADER for some debugging (will contain current redirected page info) and CURLOPT_FOLLOWLOCATION like so: 您可以使用CURLOPT_HEADER进行一些调试(将包含当前重定向的页面信息)和CURLOPT_FOLLOWLOCATION,如下所示:

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://domain.com/login.php');
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);

I also use 我也用

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

to return as a string, which is much more useful for debugging, or parsing. 以字符串形式返回,这对于调试或解析非常有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM