I am passing a variable using GET, and I'd like a fallback if there's nothing there. I've been using
$page = $_get;
if $page = ""
{
include 'home.php';
}
else
{
include $page;
}
But it just breaks my page. Any ideas?
I am passing a variable using GET, and I'd like a fallback if there's nothing there. I've been using
$page = $_get;
if $page = ""
{
include 'home.php';
}
else
{
include $page;
}
But it just breaks my page. Any ideas?
First, $_GET
is in capitals.
Second, you need to say which value_name
you want. $_GET['value_name']
Third, you need to check if the value_name
has been set. if(isset($_GET['value_name']))
Fourth, and most important: NEVER DO THIS ! Never include files based on what is given in the url or otherwise user input. You will be hacked ! Rather you match the given value to an array of allowed file names or use a switch
.
Probably just missing the parens on the if statement. Also, you'll need to specify the GET param.
$page = $_GET['param'];
if ($page == "") {
include 'home.php';
} else {
include $page;
}
First of all, $_GET
is an associative array. So you should check against something, or not empty.
On the other hand, here $page = ""
you're assigning; if you want to compare, you need to use ==
or ===
. Also, you could use functions like isset, or empty, that will return you a bool if there is an element or not in the variable.
So, what you need is something like this
include !empty($_GET['param'])?$_GET['param']:'home.php'
? is a ternary operator, is a resumed if-else
As last aclaration, you should never include directly variables you receive from $_GET, and $_POST. This will put risk on your system. example on Stack Overflow
Also, use isset()
to check if you have an input...
But yes what you want to do is a little bit dangerous in term of security... !