Checking for Keys and Looking Up Values in Arrays, Restricting Files in .htaccess

February 24, 2016
Category: TIL
Tags: PHP and Infrastructure

Today I learned:

Checking for a key in an array

<?php
key_exists($key, $array)

For the Toggl Slack slash command, I’m using it to check if someone’s Toggl API key is in the array in variables.php:

<?php
if (key_exists($user_name, $toggl_api_key_mapping) == FALSE) {
	echo ":warning: You haven't set up your Toggl API key with this command yet. Get it at the bottom of https://toggl.com/app/profile and send it  to @$your_admin_name.";
}

Notes:

  • Case sensitive. If you need it to be insensitive, use strtolower() on the $key
  • Boolean (returns TRUE or FALSE)

Looking up a key and returning a value in an array

<?php
$array['key']

For the Toggl Slack slash command, I’m using it to set someone’s API key from the array in variables.php:

<?php
$toggl_api_key = $toggl_api_key_mapping[$user_name];

Blocking access to files via .htaccess

If you are serving something on your webserver that you don’t want anyone else to be able to access, you can restrict access to by adding this snippet to your site’s .htaccess file:

<files log.csv>
	order allow,deny
	deny from all
</files>

If you need only a certain IP address, you can achieve this by adding allow from 0.0.0.0 (replace that number with your IP address):

<files log.csv>
	order allow,deny
	allow from 0.0.0.0
	deny from all
</files>

Find this post useful?

Buy me a coffeeBuy me a coffee