我使用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;
如何从第一个脚本
访问会话数据