Skip to main content

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:

Popular posts from this blog

Fix For Toshiba Satellite "RTC Battery is Low" Error (with Pictures)

RTC Battery is Low Error on a Toshiba Satellite laptop "RTC Battery is Low..." An error message flashing while you try to boot your laptop is enough to panic many people. But worry not! "RTC Battery" stands for Real-Time Clock battery which almost all laptops and PCs have on their motherboard to power the clock and sometimes to also keep the CMOS settings from getting erased while the system is switched off.  It is not uncommon for these batteries to last for years before requiring a replacement as the clock consumes very less power. And contrary to what some people tell you - they are not rechargeable or getting charged while your computer or laptop is running. In this article, we'll learn everything about RTC batteries and how to fix the error on your Toshiba Satellite laptop. What is an RTC Battery? RTC or CMOS batteries are small coin-shaped lithium batteries with a 3-volts output. Most laptops use

The Best Way(s) to Comment out PHP/HTML Code

PHP supports various styles of comments. Please check the following example: <?php // Single line comment code (); # Single line Comment code2 (); /* Multi Line comment code(); The code inside doesn't run */ // /* This doesn NOT start a multi-line comment block /* Multi line comment block The following line still ends the multi-line comment block //*/ The " # " comment style, though, is rarely used. Do note, in the example, that anything (even a multi-block comment /* ) after a " // " or " # " is a comment, and /* */ around any single-line comment overrides it. This information will come in handy when we learn about some neat tricks next. Comment out PHP Code Blocks Check the following code <?php //* Toggle line if ( 1 ) {      // } else {      // } //*/ //* Toggle line if ( 2 ) {      // } else {      // } //*/ Now see how easy it is to toggle a part of PHP code by just removing or adding a single " / " from th

Introduction to Operator Overloading in C++

a1 = a2 + a3; The above operation is valid, as you know if a1, a2 and a3 are instances of in-built Data Types . But what if those are, say objects of a Class ; is the operation valid? Yes, it is, if you overload the ‘+’ Operator in the class, to which a1, a2 and a3 belong. Operator overloading is used to give special meaning to the commonly used operators (such as +, -, * etc.) with respect to a class. By overloading operators, we can control or define how an operator should operate on data with respect to a class. Operators are overloaded in C++ by creating operator functions either as a member or a s a Friend Function of a class. Since creating member operator functions are easier, we’ll be using that method in this article. As I said operator functions are declared using the following general form: ret-type operator#(arg-list); and then defining it as a normal member function. Here, ret-type is commonly the name of the class itself as the ope