Undefined index: url

1

I created a php function that is returning me an error:

  

Notice: Undefined index: url ... on line 2.

Am I giving $_GET to $url wrongly?

function getHome()
{
    $url = $_GET['url'];
    $url = explode('/', $url);
    $url[0] = ($url[0] == NULL ? 'index' : $url[0]);

    if(file_exists('tpl/'.$url[0].'.php')){
         require_once('tpl/'.$url[0].'.php');
    }elseif(file_exists('tpl/'.$url[0].'/'.$url[1].'.php')){
         require_once('tpl/'.$url[0].'/'.$url[1].'.php');
    }else{
         require_once('tpl/404.php');
    }
}
    
asked by anonymous 25.05.2016 / 16:21

1 answer

2

If you want to check whether or not there is a parameter that calls url in the url and, if it does not exist, it is set to index, then do:

$url = 'index';
if(isset($_GET['url'])) {
    $url = $_GET['url'];
    $url = explode('/', $url);

    if(file_exists('tpl/'.$url[0].'.php')){
        require_once('tpl/'.$url[0].'.php');
    }
    else if(file_exists('tpl/'.$url[0].'/'.$url[1].'.php')){
        require_once('tpl/'.$url[0].'/'.$url[1].'.php');
    }
    else {
        require_once('tpl/404.php');
    }
}
else {
   require_once('tpl/'.$url.'.php'); // index.php
}
    
25.05.2016 / 16:34