Undefined index: url

Asked

Viewed 581 times

1

I created a function php that is returning me a mistake:

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

I’m giving away $_GET in $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');
    }
}
  • I think it is because in the url there is no parameter that is called url. do so to test: www.seusite.com? url=hello/hello. Basically add ?url=ola/hello at the end of the url and see the result

  • Got it, it’s not getting any parameters, so from notice, there would be some way when to nullify the auto url to "? url=index"?

1 answer

2


If you want to check whether or not there is a parameter that calls url url and, if this does not exist 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
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.