且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在PHP中使用file_get_contents时访问$ _SESSION

更新时间:2023-02-23 21:50:03

file_get_contents()不应在需要传输身份验证/会话信息的任何地方使用.它不会发送任何cookie,因此默认情况下将不包括用户的身份验证信息.

file_get_contents() shouldn't be used anywhere you need authentication/session information transmitted. It's not going to send any cookies, so the user's authentication information will not be included by default.

您可以通过在URL中将会话标识符(例如,默认为"PHPSESSID")作为查询参数来解决该问题,并通过其他脚本进行检查.但是即使在同一服务器上,在URL中传输会话标识符也是非常糟糕的做法.

You can kind of hack around it by including the session identifier (e.g. 'PHPSESSID' by default) as a query parameter in the URL, and have the other script check for that. But transmitting session identifiers in the URL is horribly bad practice, even if it's just to the same server.

$contents = file_get_contents("http://.... /send_sms.php?order_id=$order_id&" . session_name() . '=' . session_id());

要正确执行此操作,请使用 CURL 并建立完整的HTTP请求,包括父服务器的cookie信息页面.

To do this properly, use CURL and build a full HTTP request, including the cookie information of the parent page.