Site Personalization With PHP
Take any HTML file you have and rename it to a PHP extension. (So for example, if your HTML file is named sales.html, rename it to sales.php).
Put sales.php on your web server and run it in your browser. You get the exact same result as you did with the HTML page. Now, for the good part.
Say you wanted to personalize one of your sales pages. Let's call that sales.php. Now, if you had a newsletter or wanted to give this "personalized link" to someone, your visitor's name could be added on-the-fly to your sales page.
Your link should look something like this:
http://www.your.host/sales.php?firstname=Big&lastname=Bird
Let's try it out:
http://www.jumpx.com utorials/1/example.php?f=Myfirstname
Click that link and your name should be near the top of the page.)
In this example, yourdomain.com represents your domain name and sales.php your HTML file turned PHP script. This would be the link you could give Big Bird when you ask him to visit your sales page.
Now, edit sales.php and find where you want Big Bird's name to appear. The first and last names are given to the script separately so you could have anything from "Hi Big Bird!" to "Dear Mr. Bird..." For simplicity let's just stick with showing both the first and last names for now.
Insert this anywhere into your "script":
<?php echo "$firstname $lastname"; ?>
Now try that sample link I gave you earlier on that sales page of yours. You should see the phrase "Big Bird" anywhere on your page.
Remember that you can stick this in anywhere on the page. So for example if you wanted it to say "Dear Big Bird," you would just do this (with the comma at the end):
Dear <?php echo "$firstname $lastname"; ?>,
If you wanted to show only the first name, use
<?php echo "$firstname"; ?> instead. The same applies to the last name.
If you want to go ahead and shorten the URL even further, you can even use the letters "F" and "L" instead of firstname and lastname.
For example, try this in your page:
<?php echo "$f $l"; ?>
And then use this URL (substituting with your correct URL, of course):
http://www.your.host/sales.php?f=Oscar&l=Grouch
The result is the same. I think we want our URLs to look a little better, though.
The format I used in that above example was:
http://www.your.host/sales.php?f=Big&l=Bird
Now, the problem with this is that this link looks really ugly. Another setback with this format is that anything to the right of the "?" won't be indexed by some of the smaller search engines.
Here's a way to change this:
http://www.your.host/sales.php?f=Firstname&l=Lastname
To this:
http://www.your.host/sales.php/f=Firstname/l=Lastname
It's really easy. Just change your script to this:
<?php
$myvars = explode("/",$REQUEST_URI);
for ($i=0;$i
${$holder[0]} = $holder[1];
}
echo "$f $l";
?>
And in just a few lines of code, your personalized PHP script's URL just looked a whole lot better.
I won't bore you with the details of what that script does, but it basically chops up the URL you gave it and picks out the pieces it wants using array exploding and variable-variables.
This snippet can also be used on almost any PHP script as well. (I used this method when I coded Brian Garvin's Lightning Track ad tracker... if you ever see a URL anywhere with "go.php/etc" in it, that's my script.)
Hopefully we all learned something today. This mini-tutorial was brought to you by the letters "P-H-P."
About the Author
Article by Robert Plank
Subscribe to the free opt-in newsletter "JumpX E-Magazine" for lots more PHP help.
http://www.jumpx.com/newsletter