2009-06-10 7 views
1

Je veux boucle à travers ce tableau:comment boucle à travers ce tableau en php

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

$pagename="admin.php" 

Ensuite, si admin.php se trouve alors exécuter ce code:

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

Comment puis-je mettre ensemble cette déclaration en boucle?

Répondre

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

J'ai voté cette question parce qu'il est correct. Cependant, ce n'est pas la meilleure façon. Voir les autres ci-dessus. –

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

juste au cas où vous vouliez savoir comment créer une boucle à travers un réseau.

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

    //$value is an item in the array. 

} 
3

Je pense que cela pourrait faire ce que vous voulez faire ...

$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

Oui, exactement ce que je visais, j'apprends maintenant php merci beaucoup pour les conseils utiles. – Deyon

Questions connexes