nwind
nwind

Reputation: 89

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;
}

Upvotes: 0

Views: 4775

Answers (2)

Akshat Dhiman
Akshat Dhiman

Reputation: 61

Description:-Its happens because if you use require('config.php'); that's mean its necessary to have the file "config.php",if php is not able to find the file than its going to give you an fatal error but as you told it doesn't display anything that's mean in your php.ini file either display_error is off or error_reporting is not set.

so you need to set error_reporting = E_ALL or error_reporting = E_ALL | E_STRICT and display_errors = On in php.ini file or either you can use command in your php code to turn it on ini_set("display_errors", 1);
error_reporting(E_ALL);.

Require:-require will produce a fatal error (E_COMPILE_ERROR) and stop the script.
include :-include will only produce a warning (E_WARNING) and the script will continue.

Upvotes: 0

Bas
Bas

Reputation: 2210

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.

Upvotes: 8

Related Questions