Search code examples
phprequire

Require PHP doesn't work


I am writing a code in PHP which is not fully completed. When I open this register.php script in my browser there is only a blank page, because I can't get over require('config.php'). When I delete require('config.php') everything will appear in my browser.

Could you please help me and tell what is wrong with my require?

<?php

require('config.php');
if (isset($_POST['submit'])) {
} else {
    $form = <<<EOT
<form action="register.php" method="POST">
First Name: <input type="text" name="name" /> <Br/>
Last Name: <input type="text" name="lname" /> <Br/>
Username: <input type="text" name="uname" /> <Br/>
Email: <input type="text" name="email1" /> <Br/>
Email2: <input type="text" name="email2" /> <Br/>
Pass: <input type="password" name="pass1" /> <Br/>
Pass2: <input type="password" name="pass2" /> <Br/>
<input type="submit" value="register" name="submit" />      
</form>
EOT;
    echo $form;
}

Solution

  • This is because the require function will look for the file config.php. If this is not found, it will give you an error. This error might be only visible if you put the following lines in your document:

    ini_set("display_errors", 1);
    error_reporting(E_ALL);
    

    Taken from the PHP documentation:

    require is identical to include except upon failure it will also produce a fatal E_COMPILE_ERROR level error. In other words, it will halt the script whereas include only emits a warning (E_WARNING) which allows the script to continue.

    See the include documentation for how this works.


    If you want PHP to not give you an error when the file is not found, you could replace require with include. This means when it doesn't find the file, it still runs the code afterwards.

    For solving the actual issue, you could check if config.php is in the right place. If this isn't the case, create a file called config.php in the same folder where your register.php is.

    If your config.php is in the right place, check if that file doesn't has any errors, if it does, the require function isn't going to function aswell.