URL Friendly: Fatal error: Cannot redeclare parse_path()

Asked

Viewed 267 times

1

I have this code in a vars.php document to implement a friendly url:

<?php
//var_dump($_SERVER);
function parse_path() {
    $path = array();
    if (isset($_SERVER['REQUEST_URI'])) {
        $request_path = explode('?', $_SERVER['REQUEST_URI']);

        $path['base'] = rtrim(dirname($_SERVER['SCRIPT_NAME']), '\/');
        $path['call_utf8'] = substr(urldecode($request_path[0]), strlen($path['base']) + 1);
        $path['call'] = utf8_decode($path['call_utf8']);
        if ($path['call'] == basename($_SERVER['PHP_SELF'])) {
            $path['call'] = '';
        }
        $path['call_parts'] = explode('/', $path['call']);

        $path['query_utf8'] = urldecode($request_path[1]);
        $path['query'] = utf8_decode(urldecode($request_path[1]));
        $vars = explode('&', $path['query']);
        foreach ($vars as $var) {
          $t = explode('=', $var);
          $path['query_vars'][$t[0]] = $t[1];
        }
    }
    return $path;
}

$path_info = parse_path();
echo '<pre>'.print_r($path_info, true).'</pre>';

?>

<?php
switch($path_info['call_parts'][0]) {
    case 'aempresa': include 'aempresa.php';
        break;
    case 'contato': include 'contato.php';
        break;
    case 'cumeeiras': include 'cumeeiras.php';
        break;
    case 'informacoes-tecnicas': include 'informacoes_tecnicas.php';
        break;
    case 'orcamento': include 'orcamento.php';
        break;
    case 'telha-ondulada-natural': include 'telha_ondulada_natural.php';
        break;
    case 'telha-pre-pintada': include 'telha_pre_pintada.php';
        break;
    case 'telha-termoacustica-semi-sanduiche': include 'telha_termoacustica_semi_sanduiche.php';
        break;
    case 'telha-trapezoidal-natural': include 'telha_trapezoidal_natural.php';
        break;
    case 'telhas-termoacusticas-sanduiche': include 'telhas_termoacusticas_sanduiche.php';
        break;
    case 'vantagens': include 'vantagens.php';
        break;
  default:
    include 'index.php';
}

?>

But it is printing the following error:

Fatal error: Cannot redeclare parse_path() (Previously declared in /var/www/html/formaremetais.com.br/web/test/vars.php:4) in /var/www/html/formaremetais.com.br/web/test/vars.php on line 4

Address for analysis: http://www.formaremetais.com.br/teste/

Include the code on all pages of the site using require

Note: the site is not separated in includes.

But the error described above is occurring.

I need some help to repair this error and find out what is going on. Thank you.

  • 1

    simpleVoce is declaring parse_path more than once

  • But how do I do that? I’m a complete beginner.

  • The goal of the code is to get queryString and other parts of the url? You can use parse_str() & parse_url() as in: Get a Youtube video ID from the URL

1 answer

4


Cannot redeclare Nomedafuncao Function

The error is very clear, it says that the function name has been declared twice or more within the same scope in the global case, remember that it is possible to define names of equal functions but in different namespace.

To resolve check the file cited in the error and remove the setting of this function, to improve the organization just leave a file with the creation of functions, do not spread them in different files.

Browser other questions tagged

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