Once again, the link factory can come in handy, as it can be configured to generate HTTPS
links only for the sections of the web site that need increased security.
Our link factory will always generate absolute links. Most of the time, it??™s more comfortable
to use relative links inside the web site. For example, it??™s typical for the header image of a site
to contain a link to index.php rather than the full URL, such as http://www.example.com/
index.php. In this case, clicking the header image from a secured page would redirect the user
to https://www.example.com/index.php, so the visitor would end up accessing through a secure
connection a page that isn??™t supposed to be accessed like that (and, in effect, consumes much
more server resources than necessary).
To avoid this problem and other similar ones, we??™ll write a bit of code that makes sure all
the links in the web site are absolute links.
Exercise: Creating the Link Factory
1. Create a new file named link.php in the presentation folder, and add the following code to it:
class Link
{
public static function Build($link)
{
$base = 'http://' . getenv('SERVER_NAME');
CHAPTER 4 ?– CREATING THE PRODUCT CATALOG: PART 1 108
// If HTTP_SERVER_PORT is defined and different than default
if (defined('HTTP_SERVER_PORT') && HTTP_SERVER_PORT != '80')
{
// Append server port
$base .
Pages:
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194