Jump to content

Parse error: parse error, unexpected $end in header file


sixpack434

Recommended Posts

I have a website with many pages that should not be viewable unless the user is logged on. I am using sessions to know whether the user is logged or not.

Ok, instead of going to each PHP file which should be viewable except by logged on users and adding the following code at the top:

if(!isset($_SESSION['userName']) && !isset($_SESSION['staff'])){

echo '<p><font color="red" size="+1">Please enter a valid username and password! Click <a href="login.php">here</a> to try again!</font></p>';
} else {

I decided to save me allot of time and just include this in the header file, so my header file (header.inc) has the following lines in the end

//if the the current page requires authentication
if(basename($_SERVER['PHP_SELF']) != 'index.php' && basename($_SERVER['PHP_SELF']) != 'register.php'
&& basename($_SERVER['PHP_SELF']) != 'login.php' && basename($_SERVER['PHP_SELF']) != 'jobs.php'
&& basename($_SERVER['PHP_SELF']) != 'apply.php' && basename($_SERVER['PHP_SELF']) != 'news.php'
&& basename($_SERVER['PHP_SELF']) != 'help.php' && basename($_SERVER['PHP_SELF']) != 'contact.php')
{
//if the user is not logged on
if(!isset($_SESSION['userName']) && !isset($_SESSION['staff'])){

echo '<p><font color="red" size="+1">Please enter a valid username and password! Click <a href="login.php">here</a> to try again!</font></p>';
} else {
//else show the page contents
?>


and all my PHP files would have the following at the top: include('include.inc');

However, I'm getting this error: Parse error: parse error, unexpected $end in C:\webroot\site\includes\header.inc on line 116

Obviously PHP doesn't like the open { not being closed in the header file, but i have closed them in the footer file. Isn't there any way around this so that I do not have to go to each file and adding the code at the top to check if the user is logged on or not. I thought it'll save me allot of time by just adding the code to the header but unfortunately i'm getting this error.
Thanks
Link to comment
Share on other sites

Hi, I know,
The first if statement checks if the current page needs authentication, and the second if statement checks if the user is logged in. They are both closed in the footer file } }

Why can't I do this in the header file. Someone please help
Link to comment
Share on other sites

I've not tried spreading { } across multiple documents. I keep them in the same file, so anyone reading my work can pick it up more easily, and bugs are simpler to track-down.

If I have a dynamic header and footer file, but I'm not using a template class (usually don't, hate em all), I'll usually have a "mother" file specifying details for all of them. You might try something more like:[code]<?php // file name: motherfile.php, created by me when I made it.

// specify which header & footer
if ( foo ) {
  $headerFile = 'header01.inc';
  $footerfile = 'footer01.inc';
} else {
  $headerFile = 'header02.inc';
  $footerfile = 'footer02.inc';
}

// specify which content
$contentFile = 'about_us.inc';
$title = 'About Us';


// specify which content
include_once('doctype_html_strict.inc');

?>[/code]

Then in the document doctype_html_strict.inc:[code]<?php // file name: doctype_html_strict.inc, created by me after I made the other one. ?>
<DOCTYPE blah blah (real strict, I know)>
<html>
  <head><title><?=$title;?><title></head>
  <body>
<?php include_once( $headerFile ); ?>

<?php include_once( $contentFile ); ?>

<?php include_once( $footerfile ); ?>
  </body>
</html>[/code]
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.