I have a proxy server written in PHP for a browser in a browser project, but when inside something like google.com and I tried doing a search inside the proxy server, it’s going to take me to “proxyserver/search?q=hi” instead of the expected result which is “proxyserver/proxy.php?url=https://google.com/search?q=hi”
Basically the main problem is if a site rendered inside the proxy server tried taking me from /pageone to /pagetwo, it takes me from “proxyserver/proxy.php?url=https://example.com/pageone” to “proxyserver/pagetwo”
I tried pretty much everything to fix this, but it for some reason wouldn’t work so I just reverted back to the old code that still has the bug.
Full code:
ob_start("ob_gzhandler");
ini_set('memory_limit', '128M');
session_start();
$cookieFile = 'cookies.txt';
if (!isset($_COOKIE['PHPSESSID'])) {
clearCookies();
}
if ($_SERVER['REQUEST_METHOD'] === 'GET' && isset($_GET['url'])) {
$url = $_GET['url'];
proxyRequest($url);
} elseif ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['url'])) {
$url = $_POST['url'];
$postData = http_build_query($_POST);
echo "Debug: POST URL - " . $url . "<br>";
echo "Debug: POST Data - " . $postData . "<br>";
if (isset($_GET['url'])) {
$url = $_GET['url'];
echo "Debug: Using GET URL - " . $url . "<br>";
}
if (!filter_var($url, FILTER_VALIDATE_URL)) {
echo "Error: Invalid URL.";
exit;
}
proxyRequest($url, $postData);
} else {
echo "Debug: Invalid request received. ";
echo "Debug: GET Data - " . json_encode($_GET) . "<br>";
echo "Debug: POST Data - " . json_encode($_POST) . "<br>";
echo "Error: Invalid request.";
}
function proxyRequest($url, $postData = null) {
header('Content-Type: text/html');
header('Access-Control-Allow-Origin: *');
global $cookieFile;
$customDNS = [
'example.com:80:1.1.1.1',
'example.com:443:8.8.8.8',
];
$ch = curl_init($url);
$options = [
CURLOPT_USERAGENT => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/117.0.0.0 Safari/537.36',
CURLOPT_HTTPHEADER => [
'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9',
'Accept-Language: en-GB',
'Accept-Encoding: gzip, deflate',
'Dnt: 1',
'Sec-GPC: 1',
'Sec-Fetch-Dest: document',
'Sec-Fetch-Mode: navigate',
'Connection: keep-alive',
'Upgrade-Insecure-Requests: 1',
],
CURLOPT_RETURNTRANSFER => true,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_MAXREDIRS => 10,
CURLOPT_AUTOREFERER => true,
CURLOPT_ENCODING => '',
CURLOPT_TIMEOUT => 10,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_SSL_VERIFYHOST => false,
CURLOPT_RESOLVE => $customDNS,
];
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$options[CURLOPT_POST] = true;
$options[CURLOPT_POSTFIELDS] = $postData;
}
if (isset($_COOKIE['PHPSESSID'])) {
$options[CURLOPT_COOKIEJAR] = $cookieFile;
$options[CURLOPT_COOKIEFILE] = $cookieFile;
}
curl_setopt_array($ch, $options);
$content = curl_exec($ch);
if ($content !== false) {
preg_match_all('/Set-Cookie: (.*?);/i', $content, $matches);
$receivedCookies = implode('; ', $matches[1]);
file_put_contents($cookieFile, $receivedCookies);
error_log('Received content: ' . $content);
echo $content;
} else {
error_log('Error: Unable to fetch content from the external website. cURL Error: ' . curl_error($ch));
echo "Error: Unable to fetch content from the external website. cURL Error: " . curl_error($ch);
}
curl_close($ch);
}
function clearCookies() {
global $cookieFile;
if (file_exists($cookieFile)) {
unlink($cookieFile);
}
}
?>