Wrox Programmer Forums
Go Back   Wrox Programmer Forums > PHP/MySQL > Beginning PHP
|
Beginning PHP Beginning-level PHP discussions. More advanced coders should post to the Pro PHP forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Beginning PHP section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old June 19th, 2003, 01:51 PM
Registered User
 
Join Date: Jun 2003
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default caching example question

Hey there,

  I'm going through Beginning PHP4 and on pages 51-52 there is an example on caching that I thought I'd try out. It's giving me some strange warnings (code & output is below) and I was hoping someone here could help me out with what is wrong. Up to now the examples in the book have worked fine. My register_globals = on. Is there a specific line in my .ini file that I need to change in order for the header functions to work? I wonder if the solution is just a matter of suppressing the PHP warning messages?
  In this example I set the caching options then echo the current date and time. I am running Windows 2000 and IIS. thanks

<HTML>
<BODY>

<?php
header("Cache-Control: no-cache, must-revalidate");
header("Pragma: no-cache");
header("Expires: Fri, 20 Jun 2003 12:38:00 GMT");
echo "The date today is: ";
echo gmdate("M d Y");
echo " The time is: ";
echo gmdate("h"), ":", gmdate("i"), ":" , gmdate("s"), " ", gmdate("a");
?>
</BODY>
</HTML>

*****OUTPUT*******

Warning: Cannot modify header information - headers already sent by
(output started at c:\inetpub\wwwroot\php\page52.php:4) in c:\inetpub\wwwroot\php\page52.php on line 5

Warning: Cannot modify header information - headers already sent by
(output started at c:\inetpub\wwwroot\php\page52.php:4) in c:\inetpub\wwwroot\php\page52.php on line 6

Warning: Cannot modify header information - headers already sent by
(output started at c:\inetpub\wwwroot\php\page52.php:4) in c:\inetpub\wwwroot\php\page52.php on line 7


The date today is: Jun 19 2003 The time is: 06:13:58 pm
 
Old June 19th, 2003, 02:06 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 836
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Your problem is that you *are* sending output to the browser before calling header().

Any text outside of <?php and ?> tags is considered output text. In your case, you're sending your <HTML> and <BODY> tags (with a few newlines) before ever starting to process your PHP script.

Rewrite yours as:

Code:
<?php
// send headers
header("Cache-Control: no-cache, must-revalidate");
header("Pragma: no-cache");
header("Expires: Fri, 20 Jun 2003 12:38:00 GMT");

//  begin HTML output
echo "<html>\n";
echo "<body>\n";

echo "The date today is: ";
echo gmdate("M d Y");
echo " The time is: ";
echo gmdate("h"), ":", gmdate("i"), ":" , gmdate("s"), " ", gmdate("a");

echo "</body>\n";
echo "</html>\n";

?>
Also note that all tag names should be lower case to conform to new standards.


Take care,

Nik
http://www.bigaction.org/
 
Old June 19th, 2003, 03:17 PM
Registered User
 
Join Date: Jun 2003
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks for the help, Nik. Works great.

  I got confused because all the php scripts in the book begin and end inside the "body" html tags. So I assume that I would use the format you suggest only when I need to turn off caching, or more generally, whenever I need to call header()? Do any other function calls require this adjustment? thanks

jeff:)





Similar Threads
Thread Thread Starter Forum Replies Last Post
Caching stu9820 ASP.NET 3.5 Basics 0 June 20th, 2008 03:20 PM
caching jezywrap ASP.NET 2.0 Professional 0 February 27th, 2007 05:00 PM
Caching Amateur BOOK: ASP.NET 2.0 Website Programming Problem Design Solution ISBN: 978-0-7645-8464-0 2 February 9th, 2007 02:50 PM
caching? freaknfreak HTML Code Clinic 1 January 24th, 2006 12:10 PM
Caching bmains ASP.NET 1.x and 2.0 Application Design 4 February 27th, 2004 09:06 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.