Forum Moderators: coopster

Message Too Old, No Replies

Including files

         

Marcia

1:19 am on Dec 28, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member




System: The following 2 messages were cut out of thread at: http://www.webmasterworld.com/php/3534911.htm [webmasterworld.com] by eelixduppy - 2:28 am on Dec. 28, 2007 (est -5)


I don't know if this is related to this topic in particular, but wanted to share something I just tried which does relate to using (or not using) the parenthesis in the include statement.

The following code worked fine when it was right on the page, and is now working with the include statement written like this:

<td id="footer"><?php include "somefile.php";?></td>

This is what's in the include file:


<p> Copyright &copy;
<?php
$startYear = 2005;
$thisYear = date('Y');
if ($startYear == $thisYear)
{echo $startYear; }
else
{echo "{$startYear}-{$thisYear}"; }
?>
SomeSite. All rights reserved.<br>
<a href="http://www.example.com/">This disaster</a> has been created by me, no one else is to blame.

<p><a href="http://www.example.com/sitemap.html">Site Map</a></p>

<p><?php echo "Last modified: ",date ("l, F d, Y", getlastmod());?></p>

All my includes always work perfectly using exactly this for the include:

<?php include ("../includes/somefile.php");?>

But: they have not ever included any code or functions in the included file before. This time, when I used the parenthesis for including {"somefile.php") for the above inclusion, I got a parsing error and it didn't work until I removed the parenthesis, and then it did.

Same server, same site: the difference was from including some code and a function instead of just plain text as usual, so it was a different syntax that waa needed.

Incidentally, what the above does, in addition to the outbound link for credit,

1) It automatically updates the copyright date from the beginning year to the current year automatically, without having to update it when a year rolls around. Like Copyright 2002-2007 will automatically change to 2002-2008 January 1st of 2008.

2) Last updated doesn't have to be tinkered with or typed in manually; when a file is updated and uploaded by FTP to the server, it will automatically pick up the new date for when it was last modified.

Less work for Mother: the footer section can stay the same, maintenance free, and still always be up to date.

I'm just learning and trying everything "technically challenging" (just simple stuff), and THIS is why I read this forum (and the Apache forum) regularly. I'd never spot things like this otherwise, and now I know how to use this and have it work. So thank you!

[edited by: Marcia at 1:36 am (utc) on Dec. 28, 2007]

JAB Creations

7:23 am on Dec 28, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi Marcia,

These all work fine for me using PHP 5, what version of PHP are you using? Also what errors are you getting (please quote them minus exact file locations please). Your PHP script to keep the current year for the copyright is awesome!

- John

<?php
include "test-includes.php";
include ("test-includes.php");
include realpath(dirname(__FILE__) . "/test-includes.php");
?>

[edited by: JAB_Creations at 7:24 am (utc) on Dec. 28, 2007]

eelixduppy

8:05 am on Dec 28, 2007 (gmt 0)



Yup, it should definitely work with or without the parenthesis. It was probably a very minor syntax error that you overlooked that caused it to not work in the past. I'm curious, now, as to what it was that it happened in the same manner every time, because it seems odd; maybe you remake the same error each time? A closer look will tell...

Marcia

10:31 am on Dec 28, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



It's always worked with the parenthesis, which I've used all the time for a few years on all sites. It's just this time that it didn't work with parenthesis, but a very minor syntax error is EXACTLY what it was.

My PHP errors are always that a comma or semi-colon is either missing (expected) or unexpected - but this one didn't give a clue, it just said "Syntax error."

I redid it, and it's now working both ways at three different hosting companies on 4 different servers with different versions of PHP, including PHP 4.4.6, 4.4.7 and 5.25. The one with 5.25 also has a completely different setup requirement for using PHP on .html or .htm pages, which wasn't at all a syntax error - it was a configuration difference that had to be adjusted for (in .htaccess), and was.

Your PHP script to keep the current year for the copyright is awesome!

Oh it is, I'm loving it! I hunted all over for how to do that - and believe it or not, one of the places was a university site's instructions for their users (staff and students) that had the wrong syntax in the code for one of those two features I mentioned, using a period (full stop) instead of a comma so that it wouldn't work until corrected.

All the syntax for date() I found included the minutes and seconds, which I didn't want, but with a little cobbling together, it works fine without.

What's so endearing about PHP is that with just a basic going_through the syntax and a halfway decent idea of what the "symbols" mean, and a few dynamic things implemented on pages, you can get a "modular" system incorporated within a static HTML site without being a programmer and without implementing an entire CMS or template system.

Added:
It was the "Last modified" that was wrong at the university site. It had ".date (period/full stop) instead of ",date" (comma). The idea for the automatic copyright update I got out of a book I bought, and that idea alone was worth the price of the book, it'll save so much work and time in the future.

[edited by: Marcia at 11:27 am (utc) on Dec. 28, 2007]

sonjay

11:32 am on Dec 28, 2007 (gmt 0)

10+ Year Member



Yes, the date trick is a good one. I've been doing that for a while. Marcia, you could shorten yours by using the php ternary operator, and you also don't need to define the start year and current year as variables, unless you need those elsewhere. Mine looks like this, where 2007 is the "start year":
Copyright 
<?php
echo date('Y') == 2007? date('Y').' ' : '2007-'.date('Y').' ' ;
?>
MySiteName. All rights reserved.

henry0

12:38 pm on Dec 28, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



For those interested about the ternary op

<?php
/* ternary operator:
a shorter type of comparison operator
first: a simple "if"
then using the ternary
Let's set a value for color*/

$color="blue";
if( $color == "blue" )
{
$str = "the sky is blue";
}
else
{
$str = "Help: The sky's falling on our heads!";
}

// using ternary

$str = ( $color == "blue" )? "the ski is blue" : "Help: The sky's falling on our heads!";

echo $str;

/* using it in a form
if you want to keep a value back and forth */

<input type="checkbox" name="contactmethod[]" id="email_contact"
value="Email" <?php echo (isset($_POST['contactmethod']) && in_array('Email',$_POST['contactmethod']))? 'checked':'';?> />Email

?>

coopster

10:47 pm on Dec 28, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



My PHP errors are always that a comma or semi-colon is either missing (expected) or unexpected - but this one didn't give a clue, it just said "Syntax error."

hehe, don't mean to laugh Marcia, but we've all been there! As you have also likely heard or read before, get yourself a good text editor with syntax checking and highlighting. Loads of options available -- I'm using Eclipse now personally and absolutely love it. It is open source so it is free but it does have a bit of a learning curve, but well worth the investment of time.

BTW, the last modified trick here works great for sending the appropriate headers in dynamically generated pages too ;)

jdMorgan

12:37 am on Dec 29, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Just a comment on copyright notices: It is not necessary to "update" copyright notices unless the content has changed. By posting this, I mean only to dissuade anyone from assuming that just because a page has not been copyrighted in the current year, that it can be freely copied. That's not true: If a work has been copyrighted once and has remained unchanged, then there is no need to update the copyright as long as the author is still alive.

We seem to get a spate of threads around the first of the year from two groups -- Those in a panic to find a fast/easy way to update the copyright notice on all pages of huge static sites, and those who are waiting to 'pounce upon' and copy sites that don't update their copyright notices; The former generally need not bother, and the latter should be looking for a good attorney... :)

Because some works change infrequently, you may find copyright notices for changed content that read something like, "Copyright (C) 1997, 1999, 2003-2005, 2007 The Widget Corporation." But unless your content has changed, it is not necessary to "renew" your copyright notice.

Jim

Marcia

1:25 am on Dec 29, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Jim, it isn't necessary legally, copyright-wise, but it can come in very handy for documentation in the case of disputes.

Like if a person is claiming copyright for a site that they were never given rights to by written transmission, in which case the creator retains copyright - but they've removed credit and said Copyright = them.

Internet archive and screenshots showing the "originator" with Copyright 2002-2006 documents that the usurper did not, indeed, display it for their name (or business) during the entire time period (until the change, which is also documented), and unless they can come up with documentation that written transmission was made (which they can't), they don't have a leg to stand on when DMCA complaints are sent to the host and the search engines.

I've had people pull dirty stuff more than once, one just recently. So if it's easy enough to do, it can pay to.

sonjay

1:59 pm on Dec 29, 2007 (gmt 0)

10+ Year Member



Jim, I know that it's not necessary to update the copyright notice if the copyrighted material hasn't changed -- but pretty much every website that I work on has at least one change during the course of the year. Since the copyright notice is generally in the footer and pulled in through an include (as marcia's original post discussed), I find it far easier to simply have the notice update itself automatically every year. Then there's never any concern about failing to update the notice on pages that have changed. And it has the added benefit of avoiding the perception that the site isn't being maintained or updated.

jbinbpt

2:11 pm on Dec 29, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



That is a sweet piece of code. Thanks. Best thing I got for xmas.