0

I'm creating an array for user levels and want to make them secure by adding a secret 10 digit string to the end of each of the user levels in the array UserLevels. I'm using the code below but I can't seem to add a string to an array when using the PHP term define.

My code is below:

//Change the 10 digits of the $secret definition to a randomised string
$secret = '0000000000';

define('UserLevels', array(
    'Owner'.$secret,
    'Admin'.$secret,
    'Staff'.$secret
));

I was going to make it hash / check the user levels to make them more secure then use something to check the hash later in each page.

Benza
  • 175
  • 1
  • 15
  • Check [this answer](https://stackoverflow.com/a/44965614/4265352) on a similar question posted today. – axiac Jul 07 '17 at 18:15

2 Answers2

2

Check your PHP version.

The syntax you're using for defining an array constant should only work in PHP 7.

// Works as of PHP 5.6.0
const ANIMALS = array('dog', 'cat', 'bird');
echo ANIMALS[1]; // outputs "cat"

http://php.net/manual/en/language.constants.syntax.php

You can serialize the array to store the constant as a string then use unserialize to return it to an array when you need to access the values.

<?php

//Change the 10 digits of the $secret definition to a randomised string
$secret = '0000000000';

$user_levels = array(
    'Owner'.$secret,
    'Admin'.$secret,
    'Staff'.$secret
);

define('UserLevels', serialize($user_levels));
var_dump(UserLevels);
var_dump(unserialize(UserLevels));

https://eval.in/829457

Ryan Tuosto
  • 1,941
  • 15
  • 23
0

If you dont use PHP 7

define('ARRAY_EXAMPLE', serialize(array(1, 2, 3, 4, 5)));

unserialize(ARRAY_EXAMPLE);