Coding help

Soldato
Joined
29 Dec 2004
Posts
5,653
Location
Chatham, Kent
I have a site (index.php) and have made a table in it that i wish to open other files (1.php/2.php etc...)

If they don't exist i want it to open main.php.

I know there is an includes string to use with file_exists but can't for the life of me remember it.

Can anyone help?

Andy
 

Sic

Sic

Soldato
Joined
9 Nov 2004
Posts
15,365
Location
SO16
there's a couple of ways to do it, personally i'd use the php $_GET so that you can differentiate between main and tabled views.

Code:
//get the page that you want from the url
$view = $_GET['view'];

if ($view = "table"){
//table.php needs to include your formatted includes for each row on the table
include ("table.php");
}else{
include ("main.php");
}

then to view the table, it will be visible on www.yourdomain.com/index.php?view=table so link to it that way. that'll work, but it's not what you asked for!
 
Caporegime
Joined
18 Oct 2002
Posts
29,491
Location
Back in East London
List the pages available to the application in an array:
Code:
<?php

$pages = array('1.php', '2.php', '3.php', 'etc.');

?>
Then use a page id, similar to what Sic has suggested, which I'll demonstrate as the array key for $pages, and validate it:
Code:
<?php

$pages = array('1.php', '2.php', '3.php', 'etc.');

if (array_key_exists($_GET['pageid'], $pages)) {
    //pageid is valid input..
    if (file_exists($pages[$_GET['pageid']])) {
        //page exists
        include_once $pages[$_GET['pageid']];
    } else {
        //page does not exist..
        include_once 'main.php';
    }
} else {
    //pageid is not valid input..
    die ('Invalid pageid.');
}

?>
This is more secure than stating the filename in the URI.

HTH :)
 
Back
Top Bottom