2009-06-10 131 views
1

循环我通过这个阵列要循环:如何通过这个数组在PHP

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

$pagename="admin.php" 

那么如果admin.php的发现,然后执行此代码:

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

我怎么会放在一起这个循环语句?

回答

14
if (in_array("admin.php", $securePages)) { 
    header("location:index.php"); 
    exit(); 
} 
1
foreach($securePages AS $page) 
    { 
     if ($page == "admin.php") 
     { 
      header("location:index.php"); 
      exit(); 
     } 
    } 
+0

我投这件事,因为它是正确的。但是,这不是最好的方法。看到上面的其他人。 – 2009-06-11 06:27:56

6
if (in_array($pagename, $securePages)) { 
    header("Location: http://example.com/index.php"); 
    exit();  
} 
2
if (in_array($pagename,$securePages)) { 
    header("location:index.php"); 
exit(); 
} 
1

,以防万一你想知道如何通过一个数组实际上循环。

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

    //$value is an item in the array. 

} 
3

我想这可能做你想做的事......

$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(); 
} 
+0

是的,正是我的目标,我现在学习PHP非常感谢有用的提示。 – Deyon 2009-06-10 21:47:57