Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > PHP/MySQL > Beginning PHP
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old June 19th, 2003, 01:51 PM
Registered User
 
Join Date: Jun 2003
Location: San Luis Obispo, CA, USA.
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
Reply With Quote
  #2 (permalink)  
Old June 19th, 2003, 02:06 PM
Friend of Wrox
Points: 2,570, Level: 21
Points: 2,570, Level: 21 Points: 2,570, Level: 21 Points: 2,570, Level: 21
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: San Diego, CA, USA
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/
Reply With Quote
  #3 (permalink)  
Old June 19th, 2003, 03:17 PM
Registered User
 
Join Date: Jun 2003
Location: San Luis Obispo, CA, USA.
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:)
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


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



All times are GMT -4. The time now is 03:59 PM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.