Unexpected End Of File

I have a chunk of code that I intend to use for comments on a website. I’m working on this in XAMPP so I don’t have a website to show you the issue live. I’ve split the code into 3 parts. The 1st part is the beginning of the script and is in a separate file included on the page in question. The 2nd part is the part that will be on every page that simply needs a file name changed for each different page. The 3rd part is the end of the script and is in a separate file and included on the page in question. So, include “code1.txt” - middle part - include “code2.txt”. The error I get says “unexpected end of file” on code1. I’d say I’m moderately experienced with PHP and have tried everything in my arsenal to fix this but alas, I cannot. I hope this is clear. Thank you!

Code1

[php]<?php
$errname = $errcomm = “”;
$name = $comment = “”;
$date = date(“F jS Y - g:i”);
if (isset($_POST[“submit”])) {
$name = $_POST[“name”];
$website = $_POST[“website”];
$comment = stripslashes(nl2br($_POST[“comment”]));
if (!empty($_POST[“website”])) { $name = “<a href=”$website" target="_blank">$name"; } else { $name = $name; }
if(!empty($_POST[“spam”])) { die(“

OMITTED you, bot!

”); } else {}
if (empty($_POST[“name”])) { $errname = " error"; }
elseif (empty($_POST[“comment”])) { $errcomm = " error"; } else {[/php]

Middle

[php]<?php include "code1.txt"; ?>
$file = file(“comments/001.txt”);
$foo = fopen(“comments/001.txt”, “w+”);
fwrite ($fopen, “<div class=“comment”>\n$name ➜ $date
\n$comment\n\n\n”);
foreach ($file as $new) { fwrite($foo, “$new”); }
fclose ($foo);
header (“Location:http://localhost/mine/rh/001.php”);

<?php include "code2.txt"; ?>[/php]

Code2

[php]}
}
?>[/php]

You really can’t see the problems here?

Hi,

Not sure if the,code1, was copied & pasted correctly or is it truly the code you’re expecting to do something.
(i am not even moderately experienced programmer but few things missing there in my kind opinion):

1.) can not see closing ,}, for the if statement on line 5 - is it the one in line 2 in code 2??
2.)line 12 ending as else { , again no closing ,}, for this statement. - is it the one in line 1 in code 2??
3.) at the very end of this code i think ,?>, would do some good… - is it the one in line 3 in code2??

If these closing brackets & ?> in code 2 relate to code 1 then it should be in code 1 at appropriate places.Once that is sorted then code2 could look like the following:

For the code 2:

[php]

<?php require_once("code1.php"); $file = file("comments/001.txt"); $foo = fopen("comments/001.txt", "w+"); fwrite ($fopen, "
\n$name &#10140; $date
\n$comment\n
\n\n"); foreach ($file as $new) { fwrite($foo, "$new"); } fclose ($foo); header ("Location:http://localhost/mine/rh/001.php"); ?>

[/php]

Well, that’s mi kind opinion, it may or it may not work.

My question is why are you breaking your php code up?

What I mean there are times that it is advantage to have separate file(s) such as headers and footers for example.

Take an example of one of my website pages:
[php]<?php
require_once ‘lib/includes/utilities.inc.php’;
//echo “

” . print_r($_SESSION[‘user’], 1) . “
”;
use website_project\cms\CMS;
use website_project\calendar\Calendar;

$blog = new CMS();

$cms = $blog->read(‘article1’);

$calendar = new Calendar();
$calendar->phpDate();
require_once ‘lib/includes/header.inc.php’;
?>

<?php echo (isset($_SESSION['user'])) ? 'edit' . "\n" : NULL; ?>

<?php echo $cms->title; ?>

January 2018

<?php echo nl2br($cms->comment); ?>

Pictures of Burroughs Farms <?php echo $calendar->generateCalendar(); ?>
<?php require_once 'lib/includes/footer.inc.php'; [/php]

All of my PHP and HTML is on the same page and in this case it’s the Home Page (index.php). However I have multiple pages and I don’t feel like typing the top portion of my HTML and the header/navigation every time, so I put it in a file called header.inc.php
[php]<?php
$submit = filter_input(INPUT_POST, ‘action’, FILTER_SANITIZE_FULL_SPECIAL_CHARS);
$message = “”;

if (isset($submit) && $submit === ‘login’) {
$username = filter_input(INPUT_POST, ‘username’, FILTER_SANITIZE_FULL_SPECIAL_CHARS);
$password = filter_input(INPUT_POST, ‘password’, FILTER_SANITIZE_FULL_SPECIAL_CHARS);
//echo $username . " " . $password . “
\n”;
$result = $blog->login($username, $password);

if ($result) {
    header("Location: index.php");
    exit();
} else {
    $error_message = "Invald Login Attempt, Please Try Again!";
}

}
?>

Images and More...
Company Logo
  • Pepster's Place Facebook Page
  • Pepster's Twitter Profile
  • John Pepp's LinkedIn Profile
  • John Pepp's Flickr Profile
<?php if (!isset($_SESSION['user'])) { ?> username password <?php } ?>
        </ul>

    </nav>[/php]

and I also have a utilities.inc.php file that I put at the top of every page, so I don’t have to type that all the time. I also do that with the footer (footer.inc.php). What I do when I first start designing and developing a website is start off with a full HTML page and then lop it off the appropriate sections once I get it designed and developed into their separate files. Then on the pages that I develop I try my best to keep the PHP at the top and the HTML at the bottom. Though sometimes PHP is intermingle the HTML, but that usually is for access control or display purposes. My whole point of my jabbering it will be easier for you to develop the page in one whole swoop (the meat and potatoes portion) that way it will be easier to develop and debug. It will especially make life easier for getting help on forums such as this, for people won’t be scratching their heads trying to figure out where to start.

Hope that helped ~ John

P.S. There are even template engines out there that help with the display portion - Smarty (http://www.smarty.net/) is one that comes to mind. However, I haven’t used one myself. (Yet)

P.P.S. OK, I realize that I am using classes on this page, but the principle of what I am saying still holds because functions or classes still technically on top (figuratively speaking), but routines to help you get the results are in a separate file(s). Though they aren’t broken down into 3 or more files themselves. They are 95 percent of the time full functional scripts on their own.

Sponsor our Newsletter | Privacy Policy | Terms of Service