What are environment variables in PHP
Environment variables store sensitive data like API keys or database credentials outside your code.
In PHP you can access them with getenv() or the $_ENV array.
Printing these values can help debug configuration issues on your server.
For example, showing a database host or API key during local testing.
What is Operational and how it helps
Operational is an open source tool for tracking events in your application.
You can send events whenever someone views environment variables.
This helps you monitor access and improve security.
Setting up Operational
- Go to https://app.operational.co and create an account.
- Log in and create a new project.
- Copy the API key shown in your project settings.
- Keep your API key safe for use in your code.
Code example: Tracking env var access in PHP
This example shows how to fetch environment variables and send an event to Operational using curl in PHP.
<?php
// Get all environment variables
$envVars = getenv();
// Prepare event payload
$payload = [
'name' => 'Viewed server env vars',
'avatar' => '🔑',
'content' => 'Dashboard fetched environment variables for debugging',
];
// Send event to Operational
$ch = curl_init('https://api.operational.co/v1/events/ingest');
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Content-Type: application/json',
'Authorization: Bearer API_KEY'
]);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($payload));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
// Print environment variables
print_r($envVars);
This code retrieves all env vars, logs an event to Operational, and displays the variables.
Conclusion
Using Operational to track when environment variables are viewed saves time and boosts security.
Learn more at https://operational.co and try the playground at https://operational.co/playground.