PHP - How to make a GET pull an include?

1

If I access a page such as link

  

Using the following GET:

<?php echo  ($_GET["cor"]) ; ?>

The name Blue will be printed.

Is it possible to get a include instead of the name " Blue ?"

Example: If the person types: link

  

Instead of just printing the word " Green ", a <div> that is inside the file that has been pulled into the include, example (green.php) :

<div>A cor selecionada foi <b>verde</b> </div>

The question is simple, is it possible to have a GET pull an include?

    
asked by anonymous 17.10.2016 / 10:11

2 answers

5

Yes it is possible, in my projects I use it like this:

$page = $_GET['page'];
if (file_exists($page.".php")) {
    include($page.".php");
} else if (file_exists($path_paginas . "/".$page.".html")) {
    echo stripslashes(file_get_contents($path_paginas . "/".$page.".html"));
} else {
    include("principal.php");
}
    
17.10.2016 / 10:51
2

It is possible, but you should be careful, this method is not recommended by the community.

You can do this as follows:

function view($params = array()){
    /**
    * @params[0] retorna nome do arquivo
    * @params[1] retorna extensão do arquivo
    */

    if(file_exists($params[0].$params[1])){
        require_once($params[0].$params[1]);
    }else{
        require_once("404.php");
    }
}

view( array( $_GET["arquivo"], ".html" ) ); //executa função

Following this example template, you can create something a bit more thorough. Nowadays there are several standardized methods to work with this type of system, but as it is for knowledge, follow it.

    
20.10.2016 / 22:47