Open page in PHP

Asked

Viewed 739 times

1

I’m trying to open a page with a link inside the other, but it doesn’t load, and apparently everything seems to be ok.

 <?php
//pagina principal

echo "<a href=\"principal.php\">Link1<?</a>";
echo "<a href=\"principal.php?a=pagina2.php\">Link2<?</a>";
echo "<a href=\"principal.php?a=pagina3.php\">Link3<?</a>";

@$pagina = $GET['a'];

if(isset($pagina)){
    include $pagina;
    }else{
    echo "Bem vindo a página principal";
    }
?> 

The contents of page 2 and page 3 do not appear, what could it be? Thank you from now on

2 answers

2


The basic error is missing the underline between the $ and the GET: would be $_GET instead of $GET.

Another error is that you are putting a tag opening sign on <? before closing the tags <a>, although this browser fixes automatically by closing tags <a>.

Your code would be:

<?php
//pagina principal

echo "<a href=\"teste2.php\">Link1</a>";
echo "<a href=\"teste2.php?a=pagina2.php\">Link2</a>";
echo "<a href=\"teste2.php?a=teste3.php\">Link3</a>";

@$pagina = $_GET['a'];

if(isset($pagina)){
    include $pagina;
    }else{
    echo "Bem vindo a página principal";
}
?> 
  • Show, thank you very much!!!

0

Your code has some errors in the link declaration.

You can also take the contents of else in your code and create a default page and associate the condition of the if in the variable itself $pagina. Behold:

<?php
echo "<a href=\"principal.php\">Link1</a>";
echo "<a href=\"principal.php?a=pagina2.php\">Link2</a>";
echo "<a href=\"principal.php?a=pagina3.php\">Link3</a>";

$pagina = isset($_GET['a']) ? $_GET['a'] : 'pagina-padrao.php';

if( file_exists( $pagina ) ) {
    include $pagina;
}
else {
    echo "Erro 404 página não existe";
}

Browser other questions tagged

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