6edie8 Posted May 5, 2017 Share Posted May 5, 2017 (edited) 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 $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</a>"; } else { $name = $name; } if(!empty($_POST["spam"])) { die("<p><strong>Fuck you, bot!</strong></p>"); } else {} if (empty($_POST["name"])) { $errname = " error"; } elseif (empty($_POST["comment"])) { $errcomm = " error"; } else { Middle <?php include "code1.txt"; ?> $file = file("comments/001.txt"); $foo = fopen("comments/001.txt", "w+"); fwrite ($fopen, "<div class=\"comment\">\n<strong>$name ➜ $date</strong><br/>\n$comment\n</div>\n\n"); foreach ($file as $new) { fwrite($foo, "$new"); } fclose ($foo); header ("Location:http://localhost/mine/rh/001.php"); <?php include "code2.txt"; ?> Code2 } } ?> Edited May 5, 2017 by 6edie8 Quote Link to comment Share on other sites More sharing options...
Jacques1 Posted May 5, 2017 Share Posted May 5, 2017 You fundamentally misunderstand how script inclusion works. The included code isn't somehow copied into the main script. It's parsed like any other script, which means it must be syntactically valid. You cannot start a statement in one file and end it in another file. Those inclusion gymnastics are bad anyway. If you want to reuse code, use a proper function. Also get rid of ancient stuff like stripslashes(). The last time this made any sense at all was in the 90s when "Magic Quotes" still existed. That was ~20 years ago. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.