Austen
Austen

Reputation: 293

Weird issue with verifying variable in PHP

I have this code that is supposed to check if a page doesn't exist, or if it contains anything it shouldn't, and for some reason it's throwing an error at me, more specifically 406, if I go to any page other than the home page ($_GET = "").

Here's the code and thanks in advance for your help :)

$currentpage = $_GET['a'];
$pages[1] = "";
$pages[2] = "help";
$pages[3] = "work";
$pages[4] = "download";
$pages[5] = "process";
$pages[6] = "safariex";
$pages[7] = "services";

if(isset($_GET) && !ctype_alpha($_GET) && $_GET['a'] != ""){
    header("Location: http://pattersoncode.ca/error.php?ec=406");

}
if (!ctype_alpha($_GET['a']) && $_GET['a'] != "") {
    header("Location: http://pattersoncode.ca/error.php?ec=406");
}
if ( ! in_array( $currentpage, $pages ) )
{
 header("Location: http://pattersoncode.ca/error.php?ec=404");
}

Upvotes: 0

Views: 67

Answers (1)

Luke
Luke

Reputation: 14128

I do believe this is wrong:

!ctype_alpha($_GET)

$_GET is an array, not a string. ctype_alpha($_GET) will always equal false.

You probably want this instead:

if(!isset($_GET["a"]) || !ctype_alpha($_GET["a"])) {
    header("Location: http://pattersoncode.ca/error.php?ec=406");
    exit();
}

Which should take care of both 406 conditions.

In most cases you also want to do an exit() when you do a redirect.

If possible it would be better to send an actual http response code rather than redirect:

header('HTTP/1.1 406 Not Acceptable', true, 406);

Upvotes: 6

Related Questions