how to link pages with an id

Status
Not open for further replies.

nahsorhseda

Member
Messages
116
Reaction score
0
Points
16
does ny one know how to link pages with an id ....i mean if i have the following pages:
a.php
b.php
c.php
d.php
e.php
i want to link to them using a id like this index.php?id=1

so that i can hide the real url
 

aivanov

New Member
Messages
12
Reaction score
0
Points
0
you could put an if or case statement that requires appropriate file depending on the id.
 

Jesse

Active Member
Messages
1,360
Reaction score
0
Points
36
What do you mean by it? try searching on the internet for the php code/tutorial.
 

coolv1994

Member
Messages
508
Reaction score
0
Points
16
i had that exact question anyway heres the code:
PHP:
<?php 
if(!$_GET['id']) include('main.php'); 
if($_GET['id'] == 'a') include('a.php'); 
if($_GET['id'] == 'b') include('b.php'); 
if($_GET['id'] == 'c') include('c.php'); 
if($_GET['id'] == 'd') include('d.php'); 
if($_GET['id'] == 'e') include('e.php'); 
?>
put this code where the content goes and move the existing content to main.php
 
Last edited:

Penguin129

New Member
Messages
68
Reaction score
0
Points
0
If the page you want to include always matches the id, then you can just do:
PHP:
<?php
if($_GET["id"]) {
  include "{$_GET["id"]}.php";
} else {
  include "main.php";
}
?>
Easier than making an if condition for every id you use.
 

mitamata

New Member
Messages
81
Reaction score
0
Points
0
Do not do that. What if the user manually changes the value of "id" to something that you don't have? Then the include will fail and things will go boom! Always assume anything in the GET or POST variable can be tampered with.

You can do as coolv1994 suggested, but I'd add another line after the ifs. Something like:
PHP:
else include('invalid.php');

I use something similar with a game I'm making, only I get the filename from the database. So for example the link index.php?view=home includes a file called home_view.inc. If you have many pages, using a database to store this info is advisable, so that you won't have to go and edit the index page every time you want to add another page/id.
 

DefecTalisman

Community Advocate
Community Support
Messages
4,148
Reaction score
5
Points
38
Maybe something like this would be better for large amounts of links :

PHP:
<?php
$id_array = array(
	'a' => '1',
	'b' => '2',
	'c' => '3',
	'd' => '4'
);

$id = $_GET['id'];

foreach ($id_array as $name => $tmp_id)
{
	if($tmp_id == $id)
	{
		$inc = $name.'.php';
		break;
	}
	else
	{
		$inc = 0;
	}
}

if($inc == TRUE)
{
	include($inc);
}
?>
 
Last edited:

nahsorhseda

Member
Messages
116
Reaction score
0
Points
16
wait you guys i really dont understand whats goin on ok ill make myself clear ....consider i have a directory called folderA IN THAT directory i ahve the foolowin files ...index.php login.php,logout.php,somepage.php,somepage2.php
so iwant to link the rest of my pages using link with ids sp that i may not give out my real url ....ie for safety reasons
 

Penguin129

New Member
Messages
68
Reaction score
0
Points
0
If I'm understanding your correctly, at least one of us has to have answered that above. Even if you clarified that they're in a folder, it's not that hard to add to one of the examples above and use it. Here's a revision of mine.
PHP:
<?php
// Prevents using ../ in the url to access upper directories
$id = str_replace("..", "", $_GET["id"]);
if($id) {
  include "folderA/{$id}.php";
} else {
  include "folderA/main.php";
}
?>
 
Last edited:

DefecTalisman

Community Advocate
Community Support
Messages
4,148
Reaction score
5
Points
38
Using a combination of the above scripts.
This should handle reasonable large amounts of key=>value (url=>id).
There doesn't have to be any association between the link and it destination
PHP:
<?php

// A key=>value hash that contains the key(url to link to) => value(the id that is associated with it)
$id_array = array(
    'a' => '1',
    'b' => '2',
    'c' => '3',
    'd' => '4',
    'login' => 'abc',
    'sub_folder/some_page' => '69is_best'
);

// A link to an error page when the id is not recognised
$error_page = 'error.php';

// Prevents using ../ in the url to access upper directories
$id = str_replace("..", "", $_GET["id"]);

foreach ($id_array as $name => $tmp_id)
{
    if($tmp_id == $id)
    {
        $inc = $name.'.php';
        break;
    }
    else
    {
        $inc = $error_page;
    }
}

if($inc == TRUE)
{
    include($inc);
}
else
{
    $inc = $error_page;
    include($inc);
}

// So from the above code you would link like this 'http://www.domain.com/this_script.php?id=abc' to take
// you to login page
 
Last edited:
Status
Not open for further replies.
Top