We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,047 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

If File Name is X then include Y

I am trying to make my website display a different header depending on the page. If the page is 'index.php', it should include 'index-header.php'. If it isn't 'index.php', then it should include 'page-header.php'.

So far, I have the following code:

<?php
if($_SERVER[SCRIPT_NAME] == '/index.php'){include (index-header.php);}
else { include ('page-header.php');
?>

When I upload it, I get the following error:
'Parse error: syntax error, unexpected $end in /home/a3808hos/public_html/test/header.php on line 4'

Thanks for the help :D

5
Contributors
4
Replies
15 Hours
Discussion Span
1 Year Ago
Last Updated
5
Views
MichaelBerh
Newbie Poster
2 posts since Oct 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

Have you tried `beutifiying` your code?
eg:

/* Code: */
session_start(); if(isset($_SESSION['somevar'])){echo $_SESSION['somevar']; }else{echo 'othervar'; }
/* Would Be:*/
session_start();
if(isset($_SESSION['somevar'])) {
    echo $_SESSION['somevar'];
} else {
    echo 'othervar';
}

This often helps you to find errors in your code.
Also, use single quotes or double quotes unless using a variable or a variable created using

define();

.
And, if you don't notice it, you missed out the last curly brace to close the if...else statement.

Matthew N.
Junior Poster
101 posts since Jul 2010
Reputation Points: 10
Solved Threads: 10
Skill Endorsements: 0
if($_SERVER[SCRIPT_NAME]...

change to

if(basename($_SERVER['PHP_SELF']) == 'index.php'){
diafol
Keep Smiling
Moderator
10,655 posts since Oct 2006
Reputation Points: 1,628
Solved Threads: 1,510
Skill Endorsements: 57

the source of this error is the missing closing brace in line 3 as matthew wrote, errors are usually just before the reported line number
the missing quotes (per the OP) $_SERVER[[b]'[/b]variable[b]'[/b]] will cause another set of problems later
format is not optional

good code editors provide code highlighting that will negate this occurring again

the PHP manual is available online, as a downloadable text,chm ,any number of file types

perhaps, get the php manual and as a last resort, and, when nothing else works RTFM

almostbob
Nearly a Senior Poster
3,280 posts since Jan 2009
Reputation Points: 585
Solved Threads: 399
Skill Endorsements: 7
<?php
if(basename($_SERVER['SCRIPT_NAME']) == 'index.php'){
include ('index-header.php');
} else {
include ('page-header.php');
}
?>
cwarn23
Occupation: Genius
Team Colleague
3,033 posts since Sep 2007
Reputation Points: 413
Solved Threads: 260
Skill Endorsements: 13

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page rendered in 0.0877 seconds using 2.7MB