0

I am trying to load my content dynamically via PHP. Here is my code, which for some reason doesnt work. Can you give me some hint, what am I doing wrong?

The goal is :

  1. Click on Hypertext in nav

  2. Get dynamically content from file page-statistiky in folder /podstranky

Code:

<nav>
    <ul>
        <li><a href="index.php?stranka=page-statistiky">statistiky</a></li>

    </ul>
</nav>

<?php
if (isset($_GET['stranka']))
    $stranka = $_GET['stranka'];
else
    $stranka = 'page-statistiky';
if (preg_match('/^[a-z0-9]+$/', $stranka)) {
    $vlozeno = include('podstranky/' . $stranka . '.php');
    if (!$vlozeno)
        echo('Podstránka nenalezena');
} else
    echo('Neplatný parametr.');?>
Paolo Forgia
  • 6,572
  • 8
  • 46
  • 58
ragulin
  • 224
  • 2
  • 18

1 Answers1

0

You regex doesn't include provision for the dash i.e. -' Change this:

if (preg_match('/^[a-z0-9]+$/', $stranka))`

to

if (preg_match('/^[a-z0-9\-]+$/', $stranka))
Amit Joshi
  • 1,334
  • 1
  • 8
  • 10
  • A return with `include` is possible: https://stackoverflow.com/a/1314198/1461181 – odan Aug 06 '17 at 15:44
  • I stand corrected. You learn something new everyday :). The way @ragulin is using that feature seems to be still incorrect. Based on PHP manual, you need to use this in this way `if ((include 'vars.php') == TRUE)` and not expect include to return value as a function. – Amit Joshi Aug 06 '17 at 15:54