Я хочу сделать удаленный вход на мой второй веб-сайт, но у меня возникли некоторые проблемы, потому что файл действия /user/login.htm и форма находится здесь /user/index.htm
, когда я попробуйте сделать пост на моем первом веб-сайте, я получил сообщение об ошибке, в котором говорится, что имя пользователя и пароль недействительны, но я проверил несколько раз, и все в порядке ... сообщение выглядит примерно так:
continueTo=%2Fuser%2Findex.htm&login=&changeLogInPermanentlyPref=true&email=email%40email.com&password=TESTPASS&submit=&logInPermanently=on&_sourcePage=htYSMjoK0IATah86FF3yMT1zDYzaGsbkGTP5awTC0hykcXEpm9ztqjXVz-aUblAsXV9ykLXP1mm4yo_0TuIk1cjwytYy9KBr&__fp=NhVgRkYa2DMUOPQzpwoBNVSbbb9ZJWU7T8w3OeZ9n2BhRFuQKOPeOQ5rYfieNa75&__at=1584900220._Ai-iNOACLcgaLBOUPwPa551GFQRuWOoDqvLmWby2bU.AXG1Vdh-Vj4V25wzYgcpk3G_HvAZ
скрипт такой:
<?php
session_start();
error_reporting(-1);
include "config.php";
$browser = $_SERVER['HTTP_USER_AGENT'];
$ip = getenv("REMOTE_ADDR");
define('BASE_URL', 'https://www.example.com');
$curl = curl_init();
curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']); //set user agent
curl_setopt($curl, CURLOPT_URL, BASE_URL . $_SERVER['REQUEST_URI']);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($curl, CURLOPT_HEADER, false); //we need headers so we can see where the redirect is
//forward cookies from user to server
$cookies = array();
foreach ($_COOKIE as $cookie_name => $cookie_value) {
array_push($cookies, $cookie_name . '=' . $cookie_value);
}
$cookies = implode('; ', $cookies);
$cookies = 'Cookie: ' . $cookies;
curl_setopt($curl, CURLOPT_HTTPHEADER, array($cookies));
//forward request type to server
if($_SERVER['REQUEST_METHOD'] === "GET")
{
curl_setopt($curl, CURLOPT_HTTPGET, true);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "GET");
}
else if($_SERVER['REQUEST_METHOD'] === "POST")
{
$post_data = http_build_query($_POST);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $post_data);
}
pre_special_handle($curl);
$result = curl_exec($curl);
if($result === false)
{
exit('A CURL error occurred: ' . curl_error($curl));
}
$response = curl_getinfo($curl, CURLINFO_HTTP_CODE);
//forward cookies to end user
preg_match_all('/^Set-Cookie:\s*([^;]*)/mi', $result, $matches);
$cookies = array();
foreach($matches[1] as $item){
parse_str($item, $cookie);
$cookies = array_merge($cookies, $cookie);
}
foreach ($cookies as $cookie_name => $cookie_value){
setcookie($cookie_name, $cookie_value);
}
post_special_handle($curl, $result, $response);
if($response == 302 || $response == 301)
{
$location = get_location($result);
if($location === false)
{
exit('Received a redirect with no location\n\n' . $result);
}
header('Location: ' . $location, true, $response);
curl_close($curl);
exit();
}
echo cleanup_header($result); //Remove the headers and send page to user
curl_close($curl);
///////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////
/* Handles special URLs before request */
function pre_special_handle(&$curl)
{
else if(preg_match("/\//", $_SERVER['REQUEST_URI']))
{
}
}
/* Handles special URLs response */
function post_special_handle(&$curl, &$result, $response)
{
if($_SERVER['REQUEST_URI'] == '/login.html')
{
//Do whatever you want after fetching page here...
}
}
//////////////////////////////////////////////////////////////////////////
/* Function to find the redirect location */
function get_location($header)
{
preg_match("/\b[Ll]ocation: ([A-Za-z0-9:\/\._\?=-]+)/", $header, $location_matches);
if(count($location_matches) <= 1)
{
return false;
}
//die($header);
if(strpos($location_matches[1], "http") === false)
{
if(substr($location_matches[1], 0, 1) !== '/') //first char is not /, add it
{
return '/' . $location_matches[1];
}
else
{
return $location_matches[1]; //correct format already, /page.html
}
}
$location_matches[1] = substr($location_matches[1], strlen('https://'));
$first_slash = strpos($location_matches[1], "/");
if($first_slash !== false)
{
return substr($location_matches[1], $first_slash);
}
return false;
}
function cleanup_header($page)
{
$html_start = strpos($page, '<!DOCTYPE');
if($html_start === false)
{
return $page;
}
return substr($page, $html_start);
}
?>