Php 如何从使用cURL发布的脚本中访问会话数据?

Php 如何从使用cURL发布的脚本中访问会话数据?,php,session,curl,session-cookies,Php,Session,Curl,Session Cookies,我使用curl模拟登录: $url = 'http://localhost/Login.php'; $fields = array( 'username' => urlencode('test_username'), 'password' => urlencode('test_password'), ); //url-ify the data for the POST $fields_string = ''; foreach($fields as $

我使用curl模拟登录:

$url = 'http://localhost/Login.php';
$fields = array(
        'username' => urlencode('test_username'),
        'password' => urlencode('test_password'),
);

//url-ify the data for the POST
$fields_string = '';
foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
rtrim($fields_string, '&');

//open connection
$ch = curl_init();

//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_POST, count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);

// receive server response ...
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

//execute post
$result = curl_exec($ch);

//close connection
curl_close($ch);

// How to access $_SESSION['username'] from here ??
login.php脚本正在将用户名添加到会话:

<?php
namespace Login;
session_start();
// some code
$_SESSION['username'] = $username;

因此,您希望通过将用户名和密码发布到login.php,使用curl登录到脚本。该login.php页面设置要从curl脚本访问的会话['username']。我能问一下为什么吗?您是否已经知道用户名,因为您必须通过curl脚本发布它?在login.php中,我验证用户是否存在于数据库中(以及密码),然后用户名才会保存到会话中……如果您正在测试登录过程,我正在测试登录过程。。我可以建议使用硒吗。您可以通过名为selenium builder.PS的Firefox插件使用它,而无需任何编码——我必须告诉您,您无法通过curl访问另一个页面的会话变量。只是不可能。Curl只是加载一个web地址,然后返回输出。curl所能做的最简单的事情就是保存站点设置的cookies,并在下一篇文章中使用它们。因此,它可以让你在下一篇文章中保持登录状态,如果这是你想要做的,请查看curls-cookiejar选项。因此,你想使用curl登录脚本,方法是将用户名和密码发布到login.php。该login.php页面设置要从curl脚本访问的会话['username']。我能问一下为什么吗?您是否已经知道用户名,因为您必须通过curl脚本发布它?在login.php中,我验证用户是否存在于数据库中(以及密码),然后用户名才会保存到会话中……如果您正在测试登录过程,我正在测试登录过程。。我可以建议使用硒吗。您可以通过名为selenium builder.PS的Firefox插件使用它,而无需任何编码——我必须告诉您,您无法通过curl访问另一个页面的会话变量。只是不可能。Curl只是加载一个web地址,然后返回输出。curl所能做的最简单的事情就是保存站点设置的cookies,并在下一篇文章中使用它们。所以它可以让你在下一篇文章中保持登录状态,如果这是你想要做的,请查看curls cookiejar选项。