Saturday, October 30, 2010

Excellent trick for sites, all pages in one.

Introduction


So, ever since I first learned web design, I have wanted to make a tutorial, and in my recent endeavors, I learned a trick that allowed me to streamline my sites design with a couple lines of PHP. Now, I have the ability to get to any page from any other page with ease.


How will it work? Its really quite simple.


**NEWBIE do not read if you know about $_GET**


PHP has a snazzy little trick where you can get information from the URL from the page. Have you ever seen a page that has something like ?login=true, or http://www.youtube.com/watch?v=R-YX1WyP8JY on a site?  It really is just ?[VARIABLE]=[VALUE];


Read up on it here. http://www.w3schools.com/php/php_get.asp
If you still don't understand, no worries. Well use it later on in this tutourial.


**/NEWBIE**


Step One


So, presumably, your website will have one spot on it where the content is, like the spot where whatever content of your site will be read at.


What do you normally do to change the text in that space? Most likely, you have other pages that are exactly similar to it, like blog.php, index.php, or register.php, but with just that part changed. How many times are you repeating things that don't even change? 


Well, forget all of that, and put your main area into a single file. Now do something along these lines. If you don't understand, I will explain it all afterward.

echo "<div class=content>";
switch($_GET["p"])
{
case 2: //?p=2
include "pages/forgot.php";
break;

case 1: //?p=1
include "pages/reg.php";
break;

default: //?p is empty or not 1 or 2
include "pages/blog.php";
break;
}
echo "</div>";




Where to start? Well, echo "<div class=content>"; starts the box, assuming you have set a style for it. That second switch($_GET["p"]) bit says what to do depending on what ?p equals. So, if ?p is at 1, reg.php is put into the box. If its at 2, forgot.php. If it isn't there, always go to the blog. Finally, echo"</div>"; closes the box. Simple enough?

Step Two

Now, the next step is quite similar. What will the <title> tag make of your odd means of doing business? It wont. So, we do a bit of improvising. Based on what ?p is, we can also decide what the title is. The code will be something like this:

echo "<title>";
if(!isset($_GET["p"])) //just makes sure that we are on a page.  
$_GET["p"] = 0;
echo "Test :: ";
switch ($_GET["p"])
{
case 2: //?p=2
echo "Fogot my Password";
break;

case 1: //?p=1
echo "Registry";
break;

default: //?p is empty or not 1 or 2
echo "Blog";
break;
}

echo "</title>";

So, the echoes at the beginning and end obviously are html title tags. echo "Test :: "; is so the title will have the name of your site on it, so that should say: HappySite Central or whatever. The rest is just the old script, but with text that reads the title instead of including what is in the page.

Put simply, the script will put Test :: Forgot my Password if ?p=2 and so on.

Links

Links. Links links links. Forms and links. Its gonna be pretty simple. You want a link to go to your registry? <a href='?p=1'>.

You may think this would work with forms too, and you would be right, because you are one smart cookie.

Conclusion

To finish you all up, ill just tell you what that helped.

You basically just
  1. Saved space
  2. Made it easier to get to anywhere from anywhere
  3. Changing one page applies it to everything
  4. Learned more PHP
  5. Made loading even faster
  6. Made me famous
This is my first tutorial ever, so please do ask questions if I was unclear on any points, or if there are any techniques that may enhance this code.


And as a final note, please make sure that you do not use the name of the file, like ?p=blog.php. It makes it easier for people to maliciously manipulate our magic code, so make sure that you follow this tutorial to the letter and nothing should go wrong.

No comments:

Post a Comment