tags:

views:

941

answers:

7

I want to loop through this array:

$securePages=array("admin.php","addslot.php","classpost.php");

$pagename="admin.php"

Then if admin.php is found then execute this code:

header("location:index.php");
exit();

How would I put together this looping statement?

+13  A: 
if (in_array("admin.php", $securePages)) {
    header("location:index.php");
    exit();
}
Emil H
+1  A: 
  foreach($securePages AS $page)
  {
      if ($page == "admin.php")
      {
           header("location:index.php");
           exit();
      }
  }
Doug T.
I voted this up because it IS correct. However, it isn't the best way. See the others above.
fiXedd
+5  A: 
if (in_array($pagename, $securePages)) {
    header("Location: http://example.com/index.php");
    exit();    
}
SilentGhost
A: 

check out for and if

Greg B
+2  A: 
if (in_array($pagename,$securePages)) {
  header("location:index.php");
 exit();
}
Erik
+1  A: 

just in case you wanted to know how to actually loop through an array.

$securePages=array("admin.php","addslot.php","classpost.php");
foreach ($securePages as $value) {

  //$value is an item in the array.

}
Stan R.
+3  A: 

I am thinking this might do what you want to do...

$securePages = array("admin.php","addslot.php","classpost.php");
$url = "http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
$url = parse_url($url);
$path = $url['path']; // bar.php

if (in_array($path, $securePages)) {
    header("location:index.php");
    exit();
}
JoshFinnie
Yep, exactly what I was aiming for, I'm now learning php thanks a lot for the helpful hints.
Deyon