Weird problem using if else and curly brackets

I have a index.php file with some code in it and at the top I have php include header.php and the bottom I have php include footer.php.

When loading this file thru my web browser I get an error 500, looking at the log it gives me an unspecific error on the last line in the header.php file.

[Fri Mar 24 10:48:47 2017] [error] [client 107.77.85.47] PHP Parse error: syntax error, unexpected end of file in /home/username/public_html/mydomain.com/template/header.php on line 38

But when I copy all the code from the three files into one file it works. Weird cause the code is 100% the same, but I get this error only when they are in three different files.

In the header.php

<?php

include '/home/username/public_html/mydomain.com/template/connection.php';
include '/home/username/public_html/mydomain.com/template/functions.php'; 


echo '<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Cache-Control" content="no-cache, no-store, must-revalidate" />
<meta http-equiv="Pragma" content="no-cache" />
<meta http-equiv="Expires" content="0" />
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="viewport" content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no">
<meta name="HandheldFriendly" content="true">
<title>mydomain.com</title>
<link href="http://www.mydomain.com/template/style.css" rel="stylesheet" type="text/css">
<link rel="stylesheet" type="text/css" href="http://www.mydomain.com/template/circle.css">
<link rel="shortcut icon" href="/favicon.ico?v=1.1"> 
<meta property="og:title" content="Trucker Tools"/>
<meta property="og:type" content="website" />
<meta property="og:url" content="http://www.mydomain.com" />
<meta property="og:image" content="http://www.mydomain.com/photos/1471013330.jpg"/>
<meta property="og:description" content="mydomain"/>
<meta property="fb:app_id" content="1519610851664648"/>
</head>
<body>';

 
include 'usercheck.php'; 

//check if user or not
if (isset($_SESSION['id'])) {
########################
include 'menu.php';

echo "<div class='storytext'>"; 

?>

and footer

<?php

}
else {
	echo "<div align='center'><p>You need to login to access this site!</p><p>".$output."</p></div>";
}


echo '</div>';
echo '</body>';
echo '</html>';

?>

Index.php

<?php
include 'template/header.php';


//Add fuel to database!
if (isset($_POST["liter"])) {
			
			$vehicle_id = $_POST['vehicle_id'];
			$liter = $_POST['liter'];
			$krl = $_POST['krl'];
			$liter = str_replace(',', '.', $liter);
			$krl = str_replace(',', '.', $krl);
	
			$userid = $_SESSION['id']; $dato = $_POST['dato']; $liter = $liter; $krl = $krl; $stasjon = $_POST['stasjon']; $sted = $_POST['sted']; $km = $_POST['km'];
				
		$query=$oDB->Prepare("INSERT INTO diesel (userid, dato, liter, krl, stasjon, sted, km, vehicle_id) VALUES (:userid, :dato, :liter, :krl, :stasjon, :sted, :km, :vehicle_id)");
		$query->execute(array(':userid' => $userid,
                    ':dato' => $dato,
                    ':liter' => $liter,
                    ':krl' => $krl,
					':stasjon' => $stasjon,
					':sted' => $sted,
					':km' => $km,
					':vehicle_id' => $vehicle_id));

// Insert temporary liter value in member table
		$query=$oDB->Prepare("UPDATE members SET temp_liter=:liter WHERE id=:userid");
		$query->execute(array(':userid' => $userid, ':liter' => $liter));

		if (!$query) {
			echo "\nPDO::errorInfo():\n";
			print_r($oDB->errorInfo());
		}
	echo "<p align='center'>New fuel record added! <a href='edit_diesel.php'>Did a mistake ? Click here to edit your records.</a></p>";
} 

//$userid = $_SESSION['id'];
//Bruker info for profil

	$dbQuery = "SELECT d.dato, m.id, m.atruck, m.fname, m.company, m.mobil, m.email, m.atruck, d.sted, d.stasjon 
	FROM members m 
	LEFT OUTER JOIN diesel d ON d.userid = m.id WHERE m.id = ".$userid;
	$data = getContent($dbQuery);
		foreach($data as $row) { 
			//$userid = $row['id'];
			$fname = $row['fname'];
			$company = $row['company'];
			$mobil = $row['mobil'];	
			$email = $row['email'];	
			$atruck = $row['atruck'];	
			$sted = $row['sted'];
			$stasjon = $row['stasjon'];
			$dato = $row['dato'];
			$atruck = $row['atruck'];
			//*******/////
			$DateLimit = date('Y-m-d', strtotime('-30 days'));
		}
				
//Diesel info for brukerprofil	

	$dbQuery = "SELECT dato, liter AS FirstFill FROM diesel WHERE userid = ".$userid." AND vehicle_id = ".$atruck." GROUP BY vehicle_id ORDER BY id DESC limit 1";	
	$data = getContent($dbQuery);
		foreach($data as $row) { 
			$FirstFill = $row['FirstFill'];  
			$FirstDay = $row['dato'];
		}
			
	$dbQuery = "SELECT SUM(liter) AS totalfuel FROM diesel WHERE userid = ".$userid." AND vehicle_id = ".$atruck;
	$data = getContent($dbQuery);
		foreach($data as $row) { 
			$totalfuel = $row['totalfuel'];
		}		
	
	$dbQuery = "SELECT MIN(km) AS minikm, MAX(km) AS maxkm FROM diesel WHERE userid = ".$userid." AND vehicle_id = ".$atruck;
	$data = getContent($dbQuery);
		foreach($data as $row) { 
			$mileage = $row['maxkm'] - $row['minikm'];
		}	
	
//Km info for brukerprofil

	if ($totalfuel > 0) { 
		$averagefuel = $mileage / ($totalfuel - $FirstFill); 
	}
	
	else { 	
		echo "<p class='red'>No fuel have been added to the database for this user</p>"; 
	}

	if ($atruck >= 1) { 
		echo "<h1 align='center'>Welcome ".$fname."</h1>"; 
	}
	
	else { 
		echo "<h1 align='center'>Welcome ".$fname."</h1><p class='red'>Please go to settings and start by adding a truck</p>"; 
	}

	if ($smode == 1) {
		simulatorOverview($userid);
	}
	
echo "<div style='margin-bottom:10px;' align='center'>
		<div id='fuelMeterDiv'></div>
	</div>";


echo "<div align='center'>";



//echo "<p class='storyline'>You have driven a total of ".number_format($mileage)." miles since you registered at the fuelgame and you have added ".number_format($totalfuel)." gallons of fuel! Your last fill up was at ".$stasjon." ".$sted." on ".date("l, F jS Y", strtotime($dato))."</p></div>";

echo "<button class='style-1' type='button'><a href='add_fuel.php'>Add Fuel</a></button>
	<button class='style-1' type='button'><a href='triplog.php'>Add New Trip</a></button>
	<button class='style-1' type='button'><a href='user.php?profile=".$userid."'>My Public Profile</a></button>";

//Popular customers start
echo "<table class='table1'>
<thead><tr><td colspan='4'>Top 10 driver rated customers</td></tr></thead><tbody>";
	
		$dbQuery = "SELECT SUM(r.value) AS totalScore, c.cust_id, c.cust_customer, c.cust_address, c.cust_city, c.cust_state FROM customers c INNER JOIN rating r ON r.objectid = c.cust_id GROUP BY c.cust_id ORDER BY totalScore DESC LIMIT 10";
		$data = getContent($dbQuery);
		
		echo "<tr>
				<th width='35%'>Customer</th>
				<th width='5%'>Score</th>
				<th width='30%'>Address</th>
				<th width='30%'>City</th>
			</tr>";
			foreach($data as $row) {
				$cid = $row['cust_id'];
				$customer = $row['cust_customer'];
				$address = $row['cust_address'];
				$city = $row['cust_city'];
				$state = $row['cust_state'];
				$totalScore = $row['totalScore'];
				
			print "
				<tr>
					<td><a href='customerinfo.php?viewcust=".$cid."'>".$customer."</a>";
				
			echo "</td>
					<td align='center'>";
					echo "<span class='totalScore'>".$totalScore."</span>";
				/*
					for ($x = 1; $x <= $totalScore; $x++) {
						echo "<img src='template/icons/heart.png' /> ";
					} 
				*/
			echo "</td>
					<td>".$address."</td>
					<td>".$city.", ".$state."</td>
				</tr>";
		
		}
echo "</tbody></table>";
  
	if ($mileage == 0) {
		echo "<p class='red'>Not enough data to create your graph. Please drive some more first!</p>";
	}
	else {
		//echo "<div id='one' class='ui-content'>".UserGraph($userid,$DateLimit,$fname,$atruck,$FirstFill,$FirstDay)."<div align='center'><img style='max-width:700px;' width='95%' src='./tmp/average-".$userid.".jpg' /></div></div>";

	
		echo "<div id='two' class='ui-content'>
		<img width='95%' style='max-width:470px;' src='http://www.mydomain.net/tmp/sig_".$userid.".png' />	
		<p>This is your personal signature, please cut and paste the link to use as forum signature etc</p>
		<input type='text' value='http://".$_SERVER['SERVER_NAME']."/tmp/sig_".$userid.".png'></div>"; 
	}



	
include 'template/footer.php';	
?>

It’s rather difficult to make any helpful comment based on just a couple of lines of code. We would need to see the complete files.

ok, let me update the first post.

In my experience, that’s usually a missing bracket somewhere.

yeah thats what I thought too, but can’t find it anywhere. I merged the content of these three files into one by copy and pastse then it works, but not when they are separated.

Hmmm. I’ve never tried anything like that myself. I think I would expect every if … else … clause to be complete within a file. This seems to be bourne out by your experiment to combine the files into one.

1 Like

Just a stab in the dark - but your index.php file starts with a <?php, then it includes header.php which also starts with a <?php. And after your footer.php (which also starts and ends with php tags), you have closed the index.php php tag. Would that be causing the issue?

It was easier for me to include in the header and footer rather than repeat the code in each file. I guess there are other ways to determine whether a user is logged in or not, I just dont know exactly how. lol

When I merged the files into one I left those php open and closing tags intact, but it still worked

It may be easier but I think you may need to repeat it in order for it to work split between 3 files.

Yeah, I’ll try that instead cause this way it wont work. I was pretty sure it was gonna work. Lots of wasted hours in doing something impossible I guess lol

1 Like

The header opens a bracket on line 34

if (isset($_SESSION['id'])) {

The footer starts with a closing bracket. So together they work, but independently they will fail.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.