0

my code:

        <?php 
        if (isset($_GET['stranka']))
                $stranka = $_GET['stranka'];
        else
                $stranka = 'index';
        if (preg_match('/^[a-z0-9]+$/', $stranka))
        {
                $vlozeno = include('stranky/' . $stranka . '.php');
                if (!$vlozeno)
                        echo('Chyba. Stránka nenalezena.');
        }
        else
                echo('Chyba. Neplatný parametr.');
    ?>            

I have this warnings:

Warning: include(stranky/uzivatel.php): failed to open stream: No such file or directory in C:\xampp\htdocs\test\index.php on line 96

Warning: include(): Failed opening 'stranky/uzivatel.php' for inclusion (include_path='.;C:\xampp\php\PEAR') in C:\xampp\htdocs\test\index.php on line 96
Chyba. Stránka nenalezena.

And i want to ask for how to disable them.

It checks and write Error. Page is not found, but warnings :/

Thanks for help

Krancik
  • 25
  • 5
  • 4
    You should never disable warnings, you should code to handle them gracefully. – Jay Blanchard Jan 16 '15 at 15:01
  • 1
    [`file_exists`](http://php.net/file-exists) – Niet the Dark Absol Jan 16 '15 at 15:01
  • Like Jay mentioned, you should never ignore warnings, but here, you can find answer on this link http://stackoverflow.com/questions/1987579/remove-warning-messages-in-php And please use search function next time as there are quite few questions about this. – Chilipepper Jan 16 '15 at 15:06
  • You can use @include() but its not recommended. It will suppress the errors of just that function call. – zgr024 Jan 16 '15 at 15:10

2 Answers2

2

testing if the file exists after actually including the file won't work, you'll always get the warning, which you shouldn't just disable and disregard, the better way to do that if by first testing if the file exists and including it, or if it doesn't exist, display an error.

replace:

$vlozeno = include('stranky/' . $stranka . '.php');
if (!$vlozeno)
    echo('Chyba. Stránka nenalezena.');

By:

if(is_file('stranky/' . $stranka . '.php')){
   include('stranky/' . $stranka . '.php');
}
else {
   echo('Chyba. Stránka nenalezena.');
}
CodeBird
  • 3,883
  • 2
  • 20
  • 35
0

You want to check to make sure that the file actually exists. As the comments above say, disabling warnings won't help you to make sure your code functions the way it does. I don't speak the language of the text in the post, but I'm assuming that if $stranka doesn't match the Regex, you want to say "Invalid parameter; doesn't exist." Therefore, in addition to checking the variable matches the Regex, you want to make sure that it exists before including it.

 <?php 
        if (isset($_GET['stranka']))
                $stranka = $_GET['stranka'];
        else
                $stranka = 'index';
        $includeFile = 'stranky/' . $stranka . '.php';
        if (preg_match('/^[a-z0-9]+$/', $stranka) && file_exists($includeFile))
        {
                $vlozeno = include($includeFile);
                if (!$vlozeno)
                        echo('Chyba. Stránka nenalezena.');
        }
        else
                echo('Chyba. Neplatný parametr.');
?>
Alec Deitloff
  • 447
  • 4
  • 12