PHP if statement with isset not working
I presumed this was easy code but this doesnt work as intended:
$id = $_GET['id'];
if (!isset($id)){
header("HTTP/1.1 404 not found");
include("404.php");
开发者_StackOverflow社区}
else {
include('includes/functions.php');
}
So i've taken the get parameters off of the URL yet i dont receive a 404 error?
Any ideas?
You must do the test on $_GET['id']
directly:
if(!isset($_GET['id']) { ... }
Even if $_GET['id']
isn't set, since you tried to assign it to $id
, $id
starts to exist, so the test returns true and thus you don't have a 404 error.
You are checking if $id is set. And it is :
$id = $_GET['id'];
So you must check if the get variable is set:
isset($_GET['id'])
you have created $id variable in first line. You should check $_GET['id']
. Also you can check this variable for empty.
精彩评论