0

I have a piece of code I just wrote that detects if there is a user logged in and if [1] and [2] have any specific text in the string and then will relocate that person to another page if the values are met.

But I think my code is a little long winded. Is there a way to simplify what I have or is this the best I'll get?

if (!isset($_SESSION['user_id'])){  
    $dir =  dirname($_SERVER['PHP_SELF']);
    $dirs = explode('/', $dir);
    if(isset($dirs[1])){
        if (($dirs[1] == "account") || ($dirs[1] == "admin")){
            header('Location: /');
        }
    }
    if(isset($dirs[2])){
        if(($dirs[2] == "account")){
            header('Location: /');
        }
    }
}

Thanks in advance

2
  • 1
    Perhaps you can use mod_rewrite and specify rewrite rules for those paths? Commented Nov 26, 2012 at 11:47
  • Have amended code, the piece is inside an if statement to check whetehr user is logged in or not Commented Nov 26, 2012 at 11:50

3 Answers 3

2

a simple way is to use a closure

$dir =  explode('/', dirname($_SERVER['PHP_SELF']));

$is = function($pos, $check) use($dir) {
    return array_key_exists($pos, $dir) && $dir[$pos] == $check;
};

if($is->__invoke(1, 'account')
    || $is->__invoke(1, 'admin')
    || $is->__invoke(2, 'account')) {
    header('Location: /');
}
Sign up to request clarification or add additional context in comments.

Comments

1

You could do that for instance:

$dir =  dirname($_SERVER['PHP_SELF']);
$dirs = explode('/', $dir);

if(in_array('account',$dirs) || in_array('admin', $dirs)){
    header('Location: /');
}

4 Comments

and now a combination of Bgi & James: if (in_array(array('account', 'admin'), $dirs)) header('Location: /');
I replaced header with an echo to test the page response. It returns nothing.
Have a var_dump($dirs) before the if to check that the $dirs is correctly created
Dump returns array(2) { [0]=> string(0) "" [1]=> string(7) "account" }
0

One of a few simpler solutions could be to use PHP's array_intersect($array1, $array2) function. This is well documented on the php.net website, but here's a little example:

// Define all the 'search' needles
$needles = array('account', 'admin');

// Get all the dirs
$dirs = explode('/', dirname( $_SERVER['PHP_SELF'] ));

// Check for needles in the hay
if( array_intersect($needles, $dirs) )
{    
    // Redirect
    header('Location: /');    
}

ADDED: You could of course make the above very simple by combining multiple lines into one, this would leave you with:

if( array_intersect(array('account', 'admin'), explode('/', dirname($_SERVER['PHP_SELF']))) )
{
    header('Location: /');
}

5 Comments

I replaced header with an echo to test the page response. It returns nothing.
What is your URL for the site? It would return only one array item: '/' if your URL was something like: site.com/index.php, however if the URL was site.com/admin/index.php, you'd get two: '/' & 'admin'
OK, I've done some further research, and found another PHP function which is more reliable with comparing two arrays - array_intersect(). I'll update my post to use this instead.
A dump returns Dump returns array(2) { [0]=> string(0) "" [1]=> string(7) "account" }
The above edited post will now work with the data you've got. It might also be a good idea to ensure the $dir variable is forced to lower-case using PHP's strtolower($str) function, in case you have account & Account for what ever reasons.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.