select content from another page and show at index

Asked

Viewed 49 times

1

Good evening, I’m trying to select a page to show your content inside the index, but it presents an error and I’m not able to resolves.

Can someone please help me?

Notice: Undefined index: p in C: wamp64 www affiliates2 Welcome.php on line 89

CODE

<div id="collapseOne" class="panel-collapse collapse in" role="tabpanel" aria-labelledby="headingOne">
  <div class="panel-body">
    <?php echo " <a href=\"#\"></a> ";
        $pagina = $_GET['p']; if(isset($pagina)){include $pagina;} <!--LINNHA DO ERRO-->
        ?>
  </div>
</div>

1 answer

1

As the mistake itself says:

Index p is not defined.

You are seeking the value of p which should be received via GET. But did not receive!

The correct is to check when searching for the variable $_GET. Behold:

$pagina = isset($_GET['p']) ? $_GET['p'] : false;
// Sintaxe curta (disponível desde o PHP 7):
// $pagina = $_GET['p'] ?? false;

if ($pagina) { include $pagina; }

It’s not the point of the question, but it’s worth warning you: you are "including" a file received via URL. Soon, it is "easy" for someone to inject a script into your server. See this question that reports on this subject. But you can, too, give a "googlada" who will understand more.

  • good evening. The error has been solved but now it is giving blank page, would you tell me why?. I am grateful for your help.

  • So, buddy. When the variable p was not passed via GET, your script has nothing to include. Just you get your URL: http://localhost/index.php?p=pagina_a_ser_incluida.php.

  • @Note that my answer is DIRECTLY related to your question. It will not be interesting for you to change the course of your question. I understand you’re having a hard time understanding the subject. In this question, the most I can do is give you a hint about the new problem, which is actually not a mistake, but a misuse of your application... Watch this video that you will understand how this works, since the goal here is to fix problems, and not create a tutorial to teach you everything about method GET...

  • 1

    thanks for the help again, now it’s all running well, I saw the bug I had and I made the changes.

Browser other questions tagged

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