Low cost ecommerce web development India flash website design
Multipurpose Pages
Let's say you wanted to construct your site so that it showed the visitor's name
at the top of every page. With our custom welcome message example above, we're
halfway there already. Here are the problems we'll need to overcome to extend
the example into what we need:
. We need the name on every page of the site, not just on one.
. We have no control over which page of our site users will view first.
The first problem isn't too hard to overcome. Once we have the user's name in a
variable on one page, we can pass it with any request to another page by adding
the name to the query string of all links4:
<a href="newpage.php?name=<?php echo(urlencode($_GET['name']));
?>"> A link </a>
Notice that we've embedded PHP code right in the middle of an HTML tag. This
is perfectly legal, and will work just fine. A short cut exists for those times when
you simply want to echo a PHP value in the middle of your HTML code. The
short cut looks like this:
4If this sounds like a lot of work to you, it is. Don't worry; we'll learn much more practical methods
for sharing variables between pages in Chapter 12.
63
Multipurpose Pages
<a href="newpage.php?name=<?=urlencode($_GET['name'])?>"> A link
</a>
The tags <?= ... ?> perform the same function as the much longer code <?php
echo( ... ); ?>. This is a handy short cut that I'll use several times through
the rest of this book.
You're familiar with the echo function, but urlencode is probably new to you.
This function takes any special characters in the string (for example, spaces) and
converts them into the special codes they need to be in order to appear in the
query string. For example, if the $name variable had a value of "Kevin Yank",
then, as spaces are not allowed in the query string, the output of urlencode (and
thus the string output by echo) would be "Kevin+Yank". PHP would then automatically
convert it back when it created the $_GET variable in newpage.php.
Okay, so we've got the user's name being passed with every link in our site. Now
all we need is to get that name in the first place. In our welcome message example,
we had a special HTML page with a form in it that prompted the user for his or
her name. The problem with this (identified by the second point above) is that
we couldn't—nor would we wish to—force the user to enter our Website by that
page every time he or she visited our site.
The solution is to have every page of our site check to see if a name has been
specified, and prompt the user for a name if necessary5. This means that every
page of our site will either display its content, or prompt the user to enter a name,
depending on whether the $name variable is found to have a value. If this is beginning
to sound to you like a good place for an if-else statement, you're a
quick study!
website designer freelance ASP PHP ecommerce web developer
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110