Click to See Complete Forum and Search --> : Best way to output html within php?
Jmacp
Aug 25th, 2007, 04:48 PM
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
if ($_SERVER['HTTP_REFERER'] != "http://www.google.com"){
<html><body> This 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?
sunburnt
Aug 25th, 2007, 05:46 PM
you can mix php conditionals with HTML 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
}
penagate
Aug 25th, 2007, 09:18 PM
PHP also has an alternative block syntax, which lends itself well to templates:
<?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.
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.