-2

I'm trying to include a php file (imgupload.php) to another (addbusiness.php). My addbusiness.php uses the imgupload.php file to upload data to my database, and also upload images to a folder. I'm using a form, but I must include it because I want to display error, and succes messages on my 'addbusiness' page to the users. When I include it, my page doesn't display anything, it just becames an empty white page. Here's the first part of my addbusiness.php file:

<?php
header('Content-type: text/html; charset=utf-8');
require_once("db_connect.php");
include('imgupload.php');




if (!isset($_SESSION["loggedin"]) || $_SESSION["loggedin"] === false) {
  header("Location: login.php");
} else {

?>

//HTML part starts here
  • 1
    `imgupload.php` File might contain some error... Check that – Riad Apr 04 '20 at 09:44
  • If I don't include it, it works perfectly, but I can't display any messages then. – kristof12301 Apr 04 '20 at 09:46
  • Use phpchecker online if you can't find the error.. Or paste the code to any source and link here – Riad Apr 04 '20 at 09:52
  • Enable error_reporting/error_display to show any errors you might get on screen. The server/error log might also have more detail on what went wrong – brombeer Apr 04 '20 at 09:59
  • If you're unsure how to check for errors, see [this post](https://stackoverflow.com/questions/1053424/how-do-i-get-php-errors-to-display). – El_Vanja Apr 04 '20 at 10:19
  • Here's the source code of imgupload.php https://pastebin.com/1RXNMwQQ – kristof12301 Apr 04 '20 at 10:28
  • @El_Vanja Tried that by adding ```ini_set('display_errors', 1); ini_set('display_startup_errors', 1); error_reporting(E_ALL);``` to my php.ini but I don't get any errors. – kristof12301 Apr 04 '20 at 10:34

2 Answers2

1

The file you're including calls exit on line 13. This is triggered if no POST request is made, which is always true. You need to refactor your code so it doesn't terminate the execution of the script just because a POST parameter is missing.

Usually, one would do that by simply checking if the name of the submit button is present in POST. So assuming you have something like this:

<input type="submit" name="mySubmitButton" />

you would structure your code like this:

if (isset($_POST['mySubmitButton']) {
    // do all the logic
}

This way if the form is submitted, the actions will be performed, otherwise nothing will happen and the rest of the script will execute normally, while exit completely terminates the execution - you're including this file, thus it becomes a part of the one where you're doing the inclusion.

Also, if you check if the submit button is set, you don't have to individually check all the other fields - when a single field in a POST form is set, then all of them will be (either all form elements are submitted or none, it can't happen that some are and some are not, it's not like GET).

El_Vanja
  • 2,622
  • 4
  • 13
  • 20
  • That makes sense. But how else can I check if those values exist, and exit if not? – kristof12301 Apr 04 '20 at 12:30
  • @kristof12301 I've added more information to my answer. And I suggest you add the included code directly into the question, the pastebin link might be broken in the future and then it'd be hard for anyone reading the question to realize what happened. – El_Vanja Apr 04 '20 at 12:39
  • I changed it, and it tries to load now, but I get a couple of errors about undefinied variables. Here's the new pastebin link: https://pastebin.com/36Ka6fM4 (I'll edit my question with the good code if u can help me with this) – kristof12301 Apr 04 '20 at 12:45
  • You didn't understand it, you need to wrap the *entire* logic of that script in an `if` statement checking whether a POST request has been made (I'm guessing a form submission is happening). Also, don't edit the question with just the correct code, then it won't be clear what the problem was in the first place. You can add both the wrong (initial) code and the correct code, but just the correct one would make it a bit unclear. – El_Vanja Apr 04 '20 at 12:49
  • Well, I just put my whole imgupload code inside of your isset method, and it loads fine now. Can't test it if everything works or not, but will do it later and I'll inform you about it. – kristof12301 Apr 04 '20 at 12:53
0

Please include below two lines as the first in your addbusiness.php file to enable error reporting

error_reporting(E_ALL);

ini_set('display_errors',1);

OR ELSE

You can check log file for any error. For ubuntu check logs on path /var/log/apache2/error.log, For windows check on path c:/xampp/apache/logs