Taking User Inputs to Create Personalized Pages

You might probably have seen this type of URLs:


http://somepage.com/index.php?name=noname&age=18


It of course is a dynamic web page. As a matter of fact the string after the
‘?’, the data, is what makes it dynamic. The page is dynamic because
most pages which take data this way, create the page according to the data passed.
In this post we’re going to see how pages take data, we’ll also
create a simple dynamic page which can be personalized to display user’s
name.


First, let’s understand what is in the URL


http://somepage.com/index.php?name=noname&age=18



index.php is a PHP page and everything after the ‘?’ is the data
being passed, which is


name=noname&age=18


here name and age are the two variables having the values noname and 18 respectively
which are being passed. Each variable is separated by a ‘&’
sign.


That’s it; now let’s see how we can catch these variables from
a PHP script.


This method of sending data is known as GET method. PHP has a $_GET[] array
which holds all the variables passed via this GET method.


So, if we invoke index.php as below:


http://somepage.com/index.php?name=noname&age=18


index.php will have access to the variables passed in the $_GET[] arrays as:


$var1=$_GET[‘name’];

$var2=$_GET[‘age’];


$var1 and $var2 will contain ‘noname’ and ‘18’ respectively.


Yeah, it’s that simple.


Now let’s create a simple dynamic page which can be personalized to show
the visitors name.



<?php
// have the data being passed
$name=$_GET['name'];

// dot operator combines two strings
// output of PHP statements can be HTML tags
echo "<h1>Welcome ".$name."</h1>";
?>


I don’t think there is much to explain.


Now if you save the file as index.php request the above page like


index.php?name=Richard


You’d see a welcome message with the name Richard.


This method of customizing pages/site to specific users is very common and
useful. Forums, email service, social networking sites and many other types
of sites employ this method to give personalization and registered-user specific
features.


For now, we only need to give personalization to visitors and not registered
user-specific features, so let’s expand the script a bit like this



<html>
<head>
<title>Personalized Page Example</title>
<body>

<?php
// have the data being passed
$name=$_GET['name'];

if ($name=="") echo "<h1>Welcome Guest</h1>";
else echo "<h1>Welcome ".$name."</h1>";
?>

<p>Wow, this is an example of personalized page.
You can see that no matter who request this page
THESE TEXT appear to everyone but logged in users
are shown more personalized page.</p>
</body>
</html>


Now, if you request the page without any data, it’d show “Welcome
Guest” and when you request it with your name it shows “Welcome
Richard” or whatever you provided as your name.


Example of creating personalized pages and sites in PHP


Great isn’t it! But wait, isn’t it cumbersome to write URLs that
way to request pages. Yeah because that’s not the way, it was just to
explain the working. As a matter of fact we’ve got the form (HTML) which
would do all the data sending stuff for us and that is the topic of our next
post.


[Update: Read the next part of this post Taking
User Inputs to Create Personalized Pages II
]


Related Articles:


Check out this stream