2

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?

0

7 Answers 7

14
if (in_array("admin.php", $securePages)) {
    header("location:index.php");
    exit();
}
Sign up to request clarification or add additional context in comments.

Comments

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

Comments

3

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

1 Comment

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

Comments

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

1 Comment

I voted this up because it IS correct. However, it isn't the best way. See the others above.
1

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.

}

Comments

0

check out for and if

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.