Reputation: 85
Hey guys Im having problem with if else if with $_GET. Im trying to include page based on GET on page. Navigation code
<a href="index.php?page=setting">Setting</a>
<a href="index.php?page=post">Post Article</a>
Php Code
<?
if($_GET['page']=='post' && $_SESSION['status'] > 0 || $_GET['page']=='setting' && $_SESSION['status'] > 0)
{
include "page/post.php";
}else if{
include "page/setting.php";
}else{
include "page/home.php";
?>
How do I fix this?
Upvotes: 2
Views: 557
Reputation: 9552
Slightly improved version of Pupil's answer:
if ($_SESSION['status'] > 0) {
$pages = array(
'post',
'setting',
'whatever' // add pages
);
if (isset($_GET['page']) && in_array($_GET['page'], $pages)) {
$page = $_GET['page'];
include "page/" . $page . ".php";
}
}
else {
include "page/home.php";
}
Upvotes: 1
Reputation: 23958
Correct the syntax of else if
else if
need brackets around it.
e.g else if ($test == TRUE)
Corrected Code:
if($_GET['page']=='post' && $_SESSION['status'] > 0) {
include "page/post.php";
}
else if ($_GET['page']=='setting' && $_SESSION['status'] > 0) {
include "page/setting.php";
}
else {
include "page/home.php";
}
?>
More optimised code:
if ($_SESSION['status'] > 0) {
$pages = array();
$pages['post'] = 'post';
$pages['setting'] = 'setting';
// You can add more pages, by adding entries in this array.
//e.g. $pages['about'] = 'about';
if (isset($_GET['page']) && isset($pages[$_GET['page']])) {
$page = $pages[$_GET['page']];
include "page/" . $page . ".php";
}
}
else {
include "page/home.php";
}
Upvotes: 3
Reputation: 653
Try this. And be sure that the session status isset and > 0.
if($_GET['page']=='post' && $_SESSION['status'] > 0)
{
include "page/post.php";
} else if($_GET['page']=='setting' && $_SESSION['status'] > 0) {
include "page/setting.php";
} else {
include "page/home.php";
}
Upvotes: 0
Reputation: 1256
You have to set a condition for the else if:
<?
if(isset($_GET['page']) && $_GET['page']=='post' && $_SESSION['status'] > 0){
include "page/post.php";
}else if(isset($_GET['page']) && $_GET['page']=='setting' && $_SESSION['status'] > 0){
include "page/setting.php";
}else{
include "page/home.php";
}
?>
I have added as well a security check, just to be sure that the $_GET['page'] variable exists (isset returns true if the variable is set).
If you don't have this variable in your url, you could have problems without this check.
UPDATE: If you have more pages, I would suggest using a router ;-) (Laravel framework has one).
But to keep it simple, this is like this:
<?
$pageArray = [
'post' => 'page/post.php',
'home' => 'page/home.php',
'test' => 'page/test.php'
];
if(isset($_GET['page'])){
$page = $_GET['page'];
}else{
$page = null;
}
if($page && isset($pageArray[$page]) && $_SESSION['status'] > 0){
include $pageArray[$page];
}else{
include "page/home.php";
}
?>
Always make sure that you NEVER do include $_GET['page'];
, or someone could just do something like http://youwebsite.com?page=http://hackerwebsite.com
and you would include his page in your server, potentially causing damages.
So always check that you know the requested page (that's what I check with isset($pageArray[$page])
)
Upvotes: 1