Results 1 to 3 of 3

Thread: Best way to output html within php?

  1. #1

    Thread Starter
    Frenzied Member Jmacp's Avatar
    Join Date
    Jul 2003
    Location
    UK
    Posts
    1,959

    Best way to output html within php?

    Yup so say i need the right referer before you can view a webpage but i dont want to redirect if the referer is wrong, rather just keep the same page but output different html depending on whether referer is right or not. So something like


    PHP Code:
    if ($_SERVER['HTTP_REFERER'] != "http://www.google.com"){


      <
    html><bodyThis html ouput 1  <html><body>
      }else{


      <
    html><body> OR This html ouput 2  <html><body>
       
      } 
    Its not pratical to use echo to output the html, whats the industry way of doing this?

  2. #2
    PowerPoster sunburnt's Avatar
    Join Date
    Feb 2001
    Location
    Boulder, Colorado
    Posts
    1,403

    Re: Best way to output html within php?

    you can mix php conditionals with HTML code:

    PHP Code:
    <?php
    if ($_SERVER['HTTP_REFERER'] != "http://www.google.com"){
    ?>
      <html><body> This html ouput 1  <html><body>
    <?php
      
    }else{
    ?>
      <html><body> OR This html ouput 2  <html><body>
    <?php
      
    }
    Every passing hour brings the Solar System forty-three thousand miles closer to Globular Cluster M13 in Hercules -- and still there are some misfits who insist that there is no such thing as progress.

  3. #3
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647
    PHP also has an alternative block syntax, which lends itself well to templates:

    PHP Code:
    <?php if ($_SERVER['HTTP_REFERER'] != 'http://www.google.com'): ?>
      <html><body> This html ouput 1  <html><body>
    <?php else: ?>
      <html><body> OR This html ouput 2  <html><body>
    <?php endif; ?>
    Which brings us to the next point: templates are the best way to output HTML, or indeed any form of data, from any non-trivial application. The simplest template is a class that takes several values and handles the output of a document depending on those values. This allows you to cleanly separate presentation and logic and is a large step towards applying the Model-View-Controller (MVC) pattern, which is a useful pattern to apply in web development.

    Also, to make your referrer check a bit more reliable, you should parse the referrer URL and check only the host name.
    Last edited by penagate; Aug 25th, 2007 at 09:24 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width