View env vars on your server in PHP

Learn how to view and log environment variables in your PHP server while tracking access events with Operational.

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.

Operational in action

Setting up Operational

  1. Go to https://app.operational.co and create an account.
  2. Log in and create a new project.
  3. Copy the API key shown in your project settings.
  4. 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.