user1847224
user1847224

Reputation:

PHP Include an Array

I am creating a flat file login system for a client (as their IT team does not want to give us a database)

I have worked off this:Easy login script without database which works perfect however...

I need to add a large list of logins and wanted to include them in a septrate file rather than in that script.

I have something like this:

<?php
session_start();

// this replaces the array area of the link above
includes('users.php');

//and some more stuff

?>

And in the users file I have

$userinfo = array(
                'kate'=>'inciterocks!',
                'nick'=>'inciterocks!'
                );

However it just literally outputs the text from that file. Is there a way I can include it so it just runs like it was in the main script?

Cheers :)

Upvotes: 1

Views: 9668

Answers (5)

Marc Cools
Marc Cools

Reputation: 1

users.php :

<?php
function getUsers(){
    return array(
       'user1' => array(...), 
       'user2' => array(...),
    );
}

Some bootstrap file

include('users.php');
$myUsers = getUsers();

Upvotes: 0

Faceles
Faceles

Reputation: 453

In Yii framework (configs for exmaple) it's done like this

$users = include('users.php');

users.php:

<?php
 return array(
       'user1' => array(...), 
       'user2' => array(...),
    );

Upvotes: 1

Amal Murali
Amal Murali

Reputation: 76646

The statement name is actually include, and not includes.

Try the following:

include 'users.php';

And if your code is getting outputted as text, then it's probably because you've missed the opening <?php tags. Make sure they're present.

users.php should look something like this:

<?php

$userinfo = array(
                'kate'=>'inciterocks!',
                'nick'=>'inciterocks!'
                );

However, the closing tag is not a requirement and your code will work fine without it.

Upvotes: 2

Sergey Telshevsky
Sergey Telshevsky

Reputation: 12197

Include in PHP works simply like a reference to other piece of code AND any other content. So you should enclose the contents of the file in the <?php tags so it would be parsed as PHP.

You may as well return anything from an included file (I mention this as this in your case is the best solution):

mainfile.php

<?php
session_start();

// this replaces the array area of the link above
$userinfo = include('users.php');

users.php

return array(
    'kate'=>'inciterocks!',
    'nick'=>'inciterocks!'
);

Upvotes: 3

John Conde
John Conde

Reputation: 219804

Make sure you have an opening PHP tag in the included file:

<?php
    $userinfo = array(
        'kate'=>'inciterocks!',
        'nick'=>'inciterocks!'
    );

Upvotes: 0

Related Questions