Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What module i need to install to see my header variables in $_SERVER array?

I had a server where i have a php file to which i pass some header variables and read it in the PHP file as follows...

 $_SERVER["HTTP_API_KEY"]

If i pass API_KEY in my header data i get the value in server as $_SERVER["HTTP_API_KEY"]. Now my code has been moved to new server and there i do not see $_SERVER["HTTP_API_KEY"] in my $_SERVER array! It could be either PHP module or Apache module that i may need to configure with server i believe. I tried to find solution but unlucky to get one yet.

Let me know if anyone knows exactly what it is...

like image 988
Joseph Ranjan S Ranjan S Avatar asked Nov 18 '25 14:11

Joseph Ranjan S Ranjan S


1 Answers

I would give the getallheaders function a try!

If you want to add the key back into the $_SERVER array, just add this to the top of your file.

<?php

if(!isset($_SERVER['HTTP_API_KEY']) && isset(getallheaders()['API_KEY']))
    $_SERVER['HTTP_API_KEY'] = getallheaders()['API_KEY'];

?>

To grab the first available key.

<?php

if(isset($_SERVER['HTTP_API_KEY']))
    $key = $_SERVER['HTTP_API_KEY'];
elseif(isset(getallheaders()['API_KEY']))
    $key = getallheaders()['API_KEY'];
else
    die('Key not found.');

?>

Adapted to include keys from Get and Post requests.

<?php

if(isset($_SERVER['HTTP_API_KEY']))
    $key = $_SERVER['HTTP_API_KEY'];
elseif(isset(getallheaders()['API_KEY']))
    $key = getallheaders()['API_KEY'];
elseif($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['API_KEY']))
    $key = $_POST['API_KEY'];
elseif(($_SERVER['REQUEST_METHOD'] === 'GET' || $_SERVER['REQUEST_METHOD'] === 'POST') && isset($_GET['API_KEY']))
    $key = $_GET['API_KEY'];
else
    die('Key not found.');

if($key !== '12345')
    die('Invalid key.');

echo 'Hello world!';

?>
like image 196
Brogan Avatar answered Nov 21 '25 04:11

Brogan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!