TL;DR
URL parameter encryption is often a weak security measure. This guide shows better ways to pass data securely, including using POST requests, cookies (with appropriate flags), and server-side sessions. We’ll cover the pros and cons of each approach.
1. Why URL Encryption Isn’t Enough
Encrypting data in a URL seems like a good idea, but it has several drawbacks:
- History: URLs are often stored in browser history, server logs, and proxy servers.
- Referrer Header: The encrypted data can be passed in the HTTP Referer header when navigating to other pages.
- Length Limits: URLs have length restrictions, limiting the amount of data you can send.
- Complexity: Managing encryption/decryption on both client and server adds complexity.
2. Using POST Requests
POST requests send data in the body of the HTTP request instead of as part of the URL. This is a much more secure approach.
- Form Submission: Use an HTML form with
method="post"to submit data. - JavaScript (Fetch/Axios): Send data using JavaScript’s
fetchor libraries likeaxios.
// Example using fetch
fetch('/your-endpoint', {
method: 'POST',
body: JSON.stringify({ key1: 'value1', key2: 'value2' }),
headers: { 'Content-Type': 'application/json' }
});
Pros: More secure than URL encryption, widely supported.
Cons: Requires a server endpoint to handle the POST request. Data isn’t directly bookmarkable or shareable via URLs.
3. Secure Cookies
Cookies can store data on the user’s browser. Use them carefully with these flags:
- HttpOnly: Prevents JavaScript from accessing the cookie, reducing XSS risks.
- Secure: Only sends the cookie over HTTPS connections.
- SameSite: Controls when cookies are sent with cross-site requests (Strict or Lax recommended).
// Example setting a secure cookie in PHP
setcookie('my_data', 'sensitive_value', [
'expires' => time() + 3600,
'path' => '/',
'domain' => '.example.com',
'secure' => true,
'httponly' => true,
'samesite' => 'Strict'
]);
Pros: Can store more data than URLs, automatically sent with requests.
Cons: Vulnerable to XSS if HttpOnly is not set. Size limits apply (typically around 4KB per cookie). Requires careful handling of security flags.
4. Server-Side Sessions
Sessions store data on the server and use a session ID in a cookie to identify the user. This is generally the most secure option for sensitive information.
- Session Start: Begin a session using your server-side language (e.g.,
session_start()in PHP). - Store Data: Store data in the
$_SESSIONarray (PHP) or equivalent for other languages.
// Example storing data in a session using PHP
session_start();
$_SESSION['user_id'] = 123;
$_SESSION['username'] = 'john.doe';
Pros: Most secure option, stores sensitive data on the server.
Cons: Requires server-side support, can impact performance if not managed correctly. Session IDs are still vulnerable to hijacking (use strong session ID generation and proper security measures).
5. Choosing the Right Method
- Non-Sensitive Data: POST requests are a good choice for form submissions or data that doesn’t require high security.
- Moderately Sensitive Data: Secure cookies can be used, but with careful attention to security flags.
- Highly Sensitive Data: Server-side sessions are the recommended approach.
Always combine these techniques with other cyber security best practices like input validation, output encoding, and regular security audits.

