0

Having issue getting this to work. I have an array in 'key.php' and I want to include it in 'processor.php' and check if a posted value is in it. I either get 500 errors or it returns the array as a string.

key.php

<?php $foo = array('thing1', 'thing2'); ?>

processor.php

<?php
    $bar = include('/path/to/key.php');
    $email = $_POST('email');
    if (in_array($email, $bar)) {
       echo('in array');
    } else {
        echo('not in array');
    }
?>
Dirty Bird Design
  • 5,333
  • 13
  • 64
  • 121
  • 3
    Please read what include DOES and what it RETURNS... hint: it's not what you think – Honk der Hase Mar 19 '19 at 18:38
  • You should look into what causes the 500 errors. Those are usually hidden by default so users won't see them on a production environment. To cause errors to show up, you can do: `error_reporting(E_ALL);ini_set('display_errors', 1);` Put that code at the top of your PHP script. Here is another resource that can help: https://stackoverflow.com/questions/1053424/how-do-i-get-php-errors-to-display – Cave Johnson Mar 19 '19 at 18:40
  • Will having the '' in key.php cause problems? I don't think you can embed php block inside another php block? – CharlesEF Mar 19 '19 at 18:50

2 Answers2

2

Try changing if (in_array($email, $bar)) with if (in_array($email, $foo)), and instead of including the file like this $bar = include('/path/to/key.php'); just include it like this include '/path/to/key.php';.

The variable name you want to check for is named $foo , not $bar. When including other php files, you don't need to set them as variables. Let me know how it goes :)

L L
  • 1,440
  • 11
  • 17
1

Documentation says:

The include statement includes and evaluates the specified file.

This is equivalent to copy/paste the entire file into that point in the calling script. You should simply do:

<?php
    include('/path/to/key.php'); //here you are defining $foo
    $bar = $foo; 
    //now you can continue with the rest of your original script
    $email = $_POST('email');
    if (in_array($email, $bar)) {
       echo('in array');
    } else {
        echo('not in array');
    }
?>

(or directly checking $foo, not $bar)