headercor.php
A small utility script that enables cross-domain AJAX requests via a PHP proxy and augments jQuery’s AJAX methods. It sets the appropriate CORS header, then injects JavaScript to:
- Log progress to the console
- Globally rewrite AJAX URLs through a proxy
- Override
$.postcalls to maintain session continuity
Purpose 🚀
headercor.php ensures front-end scripts can call external APIs (especially on tech.freightanywhere.com) without CORS errors, while preserving PHP sessions.
CORS Header Configuration ⚙️
At the top of the file, a PHP header allows requests from any origin:
<?php
header("Access-Control-Allow-Origin: *");
?>
- Access-Control-Allow-Origin:
*
Allows all domains to make requests.
Injected JavaScript Logic 🛠️
Immediately after the PHP header, inline JavaScript is injected:
<script>
console.log('cror');
$.ajaxSetup({
beforeSend: function(xhr, options) {
console.log('cror2');
options.url = <?=$header_view_ignore ? '"https://tech.freightanywhere.com/cors.php?url="' : ''?>
+ options.url
+ '&s_id=<?php echo session_id();?>';
}
});
console.log('cror3');
// Store reference to original $.post
var originalPostMethod = $.post;
// Override $.post
$.post123 = function() {
if (!arguments[0].includes("freightanywhere")) {
arguments[0] = <?=$header_view_ignore ? '"https://tech.freightanywhere.com/cors.php?url="' : ''?>
+ arguments[0]
+ '&s_id=<?php echo session_id();?>';
}
console.log('arguments', arguments);
originalPostMethod.apply($, Array.prototype.slice.apply(arguments));
}
console.log(window.parent);
</script>
Key Sections Explained
1. Initial Console Log
console.log('cror')- Confirms the script has loaded.
2. Global AJAX Setup
$.ajaxSetup({
beforeSend: function(xhr, options) {
// ...
}
});
- beforeSend hook fires on every jQuery AJAX request.
- URL Rewrite: Prepends the optional proxy prefix and appends
s_idfor session tracking.
3. Overriding $.post
var originalPostMethod = $.post;
$.post123 = function() {
// Rewrite URL if needed
// Call original $.post
}
- Purpose: Ensure
$.post123uses the same proxy+session logic. - Naming:
.post123avoids colliding with native$.post. - Fallback: Calls the stored
originalPostMethod.
4. Final Console Log
console.log(window.parent)- Useful for debugging in an iframe context.
URL Rewrite Logic 🔗
| Variable | Description |
|---|---|
$header_view_ignore |
If truthy, enables proxy prefix; else no prefix |
https://tech.freightanywhere.com/cors.php?url= |
Proxy endpoint |
session_id() |
PHP session identifier, appended as s_id |
Flow:
- AJAX call initiated
beforeSendrewrites URL →proxy?url=originalURL&s_id=...- Request sent via proxy, preserving session
Usage Guide 📘
- Include this file at the top of your PHP pages (e.g., via
include 'headercor.php';). - Ensure jQuery is loaded before this script.
- Set
$header_view_ignore = truein PHP if you want to enable proxy rewriting. - Use
$.ajax(...)or$.post123(...)normally; URLs will auto-rewrite.
Integration Example
<?php
$header_view_ignore = true;
session_start();
include 'headercor.php';
?>
<!doctype html>
<html>
<head>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
</head>
<body>
<script>
// This call goes through the proxy and carries session ID
$.ajax({
url: 'https://api.example.com/data',
method: 'GET',
success: console.log
});
// Using the overridden post
$.post123('https://api.example.com/save', { key: 'value' }, function(res) {
console.log('Saved', res);
});
</script>
</body>
</html>
Summary 🎯
headercor.php is a lightweight helper that:
- Enables cross-origin AJAX via PHP header
- Proxies requests for domains requiring CORS bypass
- Preserves sessions by appending
s_id - Augments jQuery seamlessly without altering existing calls
This ensures your front-end can communicate reliably with external services while maintaining PHP session continuity.