PHP Curl身份验证后跟重定向

时间:2016-07-04 06:04:27

标签: php authentication redirect curl

我正在处理一个非常简单的事情,我想首先通过对" http://www.mywebsite.com/login"进行卷曲调用来验证用户,如果用户是真实的,则重定向用户到那个网站仪表板,即" http://www.mywebsite.com/dashboard" 现在认证部分很好,但是当我通过标题重定向用户时('位置....它只是将它带到登录页面,这意味着会话不存在, 以下是使用

的代码
$sessionStarted = session_start();

$username= $_POST['Username'];
$password= $_POST['Pass'];
$data = array("username" => "$username", "password" => "$password");                                                                    
$data_string = json_encode($data);    
$cookie_file_path = dirname(__FILE__)."/cookie_.txt";   

$ch = curl_init('https://www.mywebsite.com/login');                                                                      
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");                                                                     
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);                                                                  
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false); 
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);


curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);       
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file_path);
curl_setopt($ch, CURLOPT_COOKIEFILE,$cookie_file_path);
$strCookie = 'PHPSESSID=' . $_COOKIE[ session_name() ]. '; path=/';

curl_setopt( $curl, CURLOPT_COOKIE, $strCookie );           
curl_setopt($ch, CURLOPT_COOKIESESSION, true);


curl_setopt($ch, CURLOPT_HTTPHEADER, array(                                                                          
    'Content-Type: application/json',                                                                                
    'Content-Length: ' . strlen($data_string)
    )                                                                       
);                                                                                                                   

$result = curl_exec($ch);      
$decoded = json_decode($result);
if (isset($decoded->result) && $decoded->result == 'ok') 
{

    header("Location: https://www.mywebsite.com/dashboard");
    exit;
}
else 
{      

    header("Location: http://www.mywebsite.com/?login=fsfailed" );
    exit;

}

0 个答案:

没有答案