-1

Below is the code I have in my index.php file. When I run index.php, an error shows up. Undefined index: page How do I fix this problem? Please point me in the right direction.

Code:

 <?php 
    require 'assets/init.php';
    
    if (require_log_in() == false){
        $page = "login";
    }else{
        $page = "home";
    }
    
    if(!isset($_GET['page']) || $_GET['page'] == ''){
        $page = 'home'; 
            } else {
        $page = $_GET['page'];
    }
    
    
    switch($_GET['page']) 
    {
        case 'home':
            include 'pages/home.php';
            break;
        case 'login':
            include 'pages/login.php';
            break;
        case 'register':
            include 'pages/register.php';
            break;
        default:
            include 'pages/notfound.php';
    
    }
      
    ?>

2 Answers2

0

Since $_GET['page'] is not always set and you are creating a variable $page for it you should use that in your switch statement:

switch($page)
{
....
}
cOle2
  • 4,725
  • 1
  • 24
  • 26
0

You are checking $_GET['page'], which is good and assigning $page depending upon the outcome. So that bit of code deals with $_GET['page'].

BUT THEN you are using switch($_GET['page'])

So you need to change

switch($_GET['page']) 

To

switch($page) 

As $page is the validated value

TimBrownlaw
  • 5,457
  • 3
  • 24
  • 28