ذیل میں پی ایچ پی میں ایک ویب پراکسی/انانومیزر کا بنیادی نفاذ ہے۔
<?php
// Function to fetch the content from the target URL and return it
function fetchURL($url, $headers, $method, $data = null) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
// Set method and data for POST requests
if ($method === 'POST') {
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
}
$response = curl_exec($ch);
curl_close($ch);
return $response;
}
// Check if URL parameter is provided
if (isset($_GET['url'])) {
$url = $_GET['url'];
// Forward request headers
$headers = getallheaders();
// Forward request method
$method = $_SERVER['REQUEST_METHOD'];
// Forward request data for POST requests
$data = null;
if ($method === 'POST') {
$data = file_get_contents('php://input');
}
// Fetch content from the target URL
$targetResponse = fetchURL($url, $headers, $method, $data);
// Extract response headers and content
list($targetHeaders, $targetContent) = explode("\r\n\r\n", $targetResponse, 2);
// Forward response headers to the client
$headerLines = explode("\r\n", $targetHeaders);
foreach ($headerLines as $header) {
header($header);
}
// Output the content to the client
echo $targetContent;
} else {
// URL parameter is missing
http_response_code(400);
echo "Missing URL parameter";
}
?>
یہ پی ایچ پی اسکرپٹ ایک بنیادی ویب پراکسی/ گمنام کے طور پر کام کرتا ہے۔ یہ درخواستوں کو سنتا ہے اور انہیں مخصوص URL پر بھیج دیتا ہے۔ درخواست کردہ یو آر ایل کو بطور استفسار پیرامیٹر فراہم کیا جانا چاہیے۔ url
.
اس پراکسی کو استعمال کرنے کے لیے، اسکرپٹ کو اپنے سرور پر محفوظ کریں اور براؤزر کے ذریعے اس تک رسائی حاصل کریں یا پروگرام کے ذریعے اس پر HTTP درخواستیں بھیجیں۔ مثال کے طور پر:
GET http://example.com/proxy.php?url=https://example.com
یہ اس سے مواد کو بازیافت کرے گا۔ https://example.com
پراکسی کے ذریعے۔ نوٹ کریں کہ یہ ایک سادہ عمل ہے اور ممکن ہے کہ تمام قسم کی درخواستوں یا جوابات کو ہینڈل نہ کرے۔ مزید برآں، اس میں کیشنگ یا گمنامی جیسی خصوصیات شامل نہیں ہیں۔
آپ پی ایچ پی پراکسی اسکرپٹ میں بنیادی کیشنگ اور گمنامی کی خصوصیات شامل کر سکتے ہیں۔ یہاں اپ ڈیٹ شدہ ورژن ہے:
<?php
// Function to fetch the content from the target URL and return it
function fetchURL($url, $headers, $method, $data = null) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
// Set method and data for POST requests
if ($method === 'POST') {
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
}
$response = curl_exec($ch);
curl_close($ch);
return $response;
}
// Function to cache response content
function cacheContent($url, $content) {
// Implement caching logic here
// For simplicity, you can use file-based caching
$cacheFileName = md5($url) . '.cache';
file_put_contents($cacheFileName, $content);
}
// Function to retrieve cached content
function getCachedContent($url) {
// Implement caching logic here
$cacheFileName = md5($url) . '.cache';
if (file_exists($cacheFileName)) {
return file_get_contents($cacheFileName);
}
return null;
}
// Check if URL parameter is provided
if (isset($_GET['url'])) {
$url = $_GET['url'];
// Check if content is cached
$cachedContent = getCachedContent($url);
if ($cachedContent !== null) {
// Output cached content
echo $cachedContent;
exit();
}
// Forward request headers
$headers = getallheaders();
// Forward request method
$method = $_SERVER['REQUEST_METHOD'];
// Forward request data for POST requests
$data = null;
if ($method === 'POST') {
$data = file_get_contents('php://input');
}
// Fetch content from the target URL
$targetResponse = fetchURL($url, $headers, $method, $data);
// Extract response headers and content
list($targetHeaders, $targetContent) = explode("\r\n\r\n", $targetResponse, 2);
// Forward response headers to the client
$headerLines = explode("\r\n", $targetHeaders);
foreach ($headerLines as $header) {
header($header);
}
// Output the content to the client
echo $targetContent;
// Cache the response content
cacheContent($url, $targetContent);
} else {
// URL parameter is missing
http_response_code(400);
echo "Missing URL parameter";
}
?>
اس اپڈیٹ شدہ ورژن میں، میں نے فائل پر مبنی کیشنگ کا استعمال کرتے ہوئے بنیادی کیشنگ فعالیت کو شامل کیا ہے۔ کیش شدہ جوابات ان فائلوں میں محفوظ کیے جاتے ہیں جن کا نام URL کے MD5 ہیش کے نام پر رکھا گیا ہے۔ مزید برآں، اسکرپٹ ٹارگٹ یو آر ایل سے حاصل کرنے سے پہلے کیش شدہ مواد کی جانچ کرتا ہے، اور اگر مواد کیش میں پایا جاتا ہے، تو اسے براہ راست کلائنٹ کو پیش کیا جاتا ہے۔
تبصرے (0)
یہاں ابھی تک کوئی تبصرہ نہیں ہے، آپ پہلے ہو سکتے ہیں!