[PHP] Pages with ids Tutorial

nightscream

New Member
Messages
948
Reaction score
0
Points
0
Ok i'll post the first tutorial here, I hope it's a good one
If you are wondering how people do the url/index.php?id=News this is the tutorial for it :)
I hope you understand what I say in this tutorial, I'll try to make it easy.


Create your main file, index.php
create your menu, no links yet
example:
Code:
<table width="125" cellpadding="0" cellspacing="0">
<tr><td>Menu</td></tr>
<tr><td>News</td></tr>
<tr><td>Members</td></tr>
</table>

now go to the part of your file where you want that the content change
and put something like this
Code:
<?php
$id = $_REQUEST['id']; // this will get the id of the link, this will be explained later.
switch($id) { // make a case with the id
default: include('main.php'); //When page is loaded this will be the main content, put the content in main.php(you can change the name)
break; // close this case
case "News": include('news.php'); // The content in news.php will be called when id=News
break; // close this case
case "Members": include('members.php'); // The content in members.php will be called when id=Members
break; // close this case
} // close switch
?>
You can add more ids in there if you want.

Now go back to your menu and we are going to add the links
add this kind of link to your menu
Code:
<a href="index.php?id=ID THAT YOU WANT" target="_self">TEXT</a>

with my example it will be like below.
the id=News is equal to the case "News" in the php function
Code:
<table width="125" cellpadding="0" cellspacing="0">
<tr><td>Menu</td></tr>
<tr><td><a href="index.php?id=News" target="_self">News</a></td></tr>
<tr><td><a href="index.php?id=Members" target="_self">Members</a></td></tr>
</table>

This Was the tutorial, I hope you liked it and you learned something.

Nightscream
 

Fedlerner

Former Adm & Team Manager
Community Support
Messages
12,934
Reaction score
6
Points
38
Nice!!!
I read this one in your website :p
 

Zenax

Active Member
Messages
1,377
Reaction score
4
Points
38
Code:
<?php 


if( isset($HTTP_GET_VARS['page']) || isset($HTTP_POST_VARS['page']) )
{
    $page = ( isset($HTTP_GET_VARS['page']) ) ? $HTTP_GET_VARS['page'] : $HTTP_POST_VARS['page'];
    $page = htmlspecialchars($page);
}
else 
{
    $page = "";
}

switch( $page )
    {   
	
		 case "1":
		include("1.php");
	   break;
	    
         case "2":
        include("2.php");
       break;

         case "3":
        include("3.php");
       break;

         case "4":
        include("4.php");
       break;

      default:
	  include("inc/main.php");
    break;
}

?>

This is the php code I use to do it! Could you tell me what the difference is and which is better?
 

t2t2t

New Member
Messages
690
Reaction score
0
Points
0
His code has everything on one page and uses html function for moving thru page.
 

jaygreentree

Member
Messages
148
Reaction score
1
Points
16
I prefer to use the code below because its more secure:
PHP:
<?
if (isset($_GET['id']) && file_exists('/path/to/page/'.$_GET[id].'.php')) {
include '/path/to/page/'.$_GET[id].'.php';
} else {
include("/path/to/page/main.php");
}
?>
Then the links would looks like
HTML:
<a href="index.php?id=page">Page Name</a>
 

Woolie

Member
Messages
862
Reaction score
0
Points
16
I'm slightly curious to know why everyone loves to use GET globals for navigation when there is no database present? What is the point? You don't gain anything, it just means unnecessary code to edit when you want to add a navigation link.

I mean, it is (usually) necessary to use them for database based content management type systems, but when I create things like that, I always try to use mod_rewrite (where possible) to make them look more friendly to the human.

Anyway, apart from that, I suppose it's a good tutorial... If you really want to waste your time making extra work for yourself, when the idea of using programming languages (especially PHP) is to make it easier for you. Ummm, does that really make sense?
 

jaygreentree

Member
Messages
148
Reaction score
1
Points
16
The way I posted above you just add the link to the menu and create the page. My code doesn't have to be updated everytime like the ones that use case. That's the reason I use mine.
 

dest581

New Member
Messages
348
Reaction score
0
Points
0
For codes where it just changes x to x.php, make sure that you filter out any requests containing ../ or http://
 

Sohail

Active Member
Messages
3,055
Reaction score
0
Points
36
This is a great tutorial! I thought i had seen the exact same thing before and i remembered that it was on your site.
 

Slothie

New Member
Messages
1,429
Reaction score
0
Points
0
Title is misleading... :D

and IDS would be an intrusion detection system :p

IDs would be more appropriate iMO
 
Top