Jump to content

Recommended Posts

I wrote an index page to use with SSI to include .txt files in a divide. I used $id for the array. <!-- ($_GET['id']) --> I also used the array for title. It works OK but I have some trouble with undefined index. I have a page not found include if the 'id' is not in the array. If error reporting is on I get the undefined index warning.

 

This is the index:

<? date_default_timezone_set('America/Chicago'); ?>
<!--
Aurthur:me
Title: SSI Page Management 
License: Unrestricted
-->
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns='http://www.w3.org/1999/xhtml' xml:lang="en">
<head>
<? include('includes/meta.txt'); ?>
<? include('includes/link.txt'); ?>
<title><? include('includes/title.txt'); ?></title>
<? include('includes/script.txt'); ?>
</head>
<body>
<div id='logo'><img src="images/drop.jpg" alt='logo' /></div>
<table class="top"><tr><td style="text-align:left;" valign="middle"><? include('includes/menu.php'); ?> <span class='lef'><? echo date('M d, Y g:i T') ?></span></td></tr></table>
<div id='body'>
<table class='lbody'>
   <tr>
      <td class='lct'><? include('includes/title.txt'); ?></td>
   </tr>
   <tr>
      <td class='ltxt'><? include('includes/array.php') ?></td>
   </tr>
   <tr>
    <td class='lft'> </td>  
   </tr>
</table>
<table class='rbody'>
   <tr>
   <td class='rct'>Articles</td>
   </tr>
   <tr>
      <td class='rtxt'><? include('includes/articles.txt'); ?><br /><br /></td>
   </tr>
   <tr>
   <td class='rft'> </td>   
   </tr>
</table>
<br /><br />
<table class='rbody'>
   <tr>
   <td class='rct'>Site Links</td>
   </tr>
   <tr>
      <td class='rtxt'><? include('includes/siteads.txt'); ?></td>
   </tr>
   <tr>
   <td class='rft'> </td>   
   </tr>
</table>
</div>
<div id='bottom'>sourfly <a href='filemanager/'><img src='images/copy.png' alt='copy' /></a> 2010</div>
</body>
</html>

 

This is the page content array:

 

<?
$id = $_GET['id'];
$page = array(
0 => 'pages/home.txt',
1 => 'pages/home.txt',
2 => 'pages/about.txt',
3 => 'pages/contact.txt',
4 => 'pages/thankyou.txt',
5 => 'pages/downloads.txt',
10 => 'pages/ssipman.txt',
11 => 'pages/usersguide.txt',
403 => 'pages/403.txt',
404 => 'pages/404.txt'
);

if(isset($_GET['id']))
{
$Page=(file_exists($page[$_GET['id']]
))?$page[$_GET['id']]: "pages/404.txt";
include($Page);}

elseif (!isset($_GET['id'])) { 
     include ('pages/home.txt'); 
} 
?>

<!--- title array --->
<? 
$id = $_GET['id'];
$page = array(
0 => 'sourfly.us',
1 => 'Home',
2 => 'About',
3 => 'Contact',
4 => 'Thank You',
5 => 'Downloads',
10 => 'SSI Page Management',
11 => 'Users Guide',
404 => '404 Page Not Found!'
);

if (isset($id)){
echo($page["$id"]);
}
elseif (!isset($_GET['id'])){
echo ('sourfly.us');
}
else 
{
echo('Page Not Found!');
}
?>

 

Now if the page is in the array every thing works, page is include and title echo's the correct page title. But if the page is not in the array the 404 page is included but the title returns the URL. (www.domain.com/?id=# or word)

 

Now if just the query sting is used, (www.domain.com/?# or word) The home page is returned and title. What I would like is a way to if query or not in the array have the 404 page included and title return 404 Page Not Found!

 

I tried many different array codes, and this is as close to it as I can get. I figure it will have to be total rewritten. But I hope not. I was thinking that the home page to be include mite be the problem. If the URL is www.domain.com/.

 

If any Ideas where I'm going wrong at would be nice.  You can veiw the production site here...

I get lost trying to explain.

 

if I use ?id=1 it is in the array. everything works. The array only has 10 "ids" (isset). So if ?id=11 (isnotset) the 404 page is included. But the title does not echo Page Not found! Because. id 11 is not in the title array.

 

I can't make an array 11 threw 99999. Well I could. "lol"

 

same for query, That starts with just the "?" any thing after "?" (?google) it query's returning

elseif (!isset($_GET['id'])) { 
     include ('pages/home.txt'); 
} 

My home page.

 

It seems like the 404 page should be returned. I think that "?id" is what sets (isset). But I guess not.

 

Thanks for the reply. Sorry about the poor explanation. 

OK, try using a different route instead of isset....  Just for the hell of it try somthing along the lines of:

 

Take the id give and dump it into an a variable.

Then take that number and look for that array element and dump it into a variable.

Then check that variable in an if statement like if($temp_array == '') {Spit out error code or whatever}

That way if you have an id that doesn't exist it'll do something with a blank return.

 

Or use a foreach statement to walk through each part of the array to try and compare the key to the id.

Something more like this might work:

if (isset($_GET['id'])) {
  $id = $_GET['id'];
  if (isset($page[$id])) { // the page exists
    include($page[$id]);
  } else { // the page is not in the array
    include ('pages/404.txt');
  }
} else { // No id specified
     include ('pages/home.txt'); 
} 

 

Also, rather than using two separate arrays that you have to keep in sync, I would be tempted to write it this was:

<?php
$pages = array (
  0 => array ('file' =>  'pages/home.txt', 'title' => 'sourfly.us'), 
  1 => array('file' => 'pages/home.txt', 'title' => 'Home'),
  2 => array('file' => 'pages/about.txt', 'title' => 'About'),
// and so forth
404 => array('file' => 'pages/404.txt', 'title' => '404 Page Not Found!')
   );

  if (isset ($_GET['id'])) {
    $pageID = $_GET['id'];
    if (! isset($pages[$pageID])) $pageID = 404;
  } else { // No page specified send them home
    $pageID = 1;
  }

  include($pages[$pageID]['file']);
  echo $pages[$pageID]['title'];  // show the title
?>

 

Then your page list is all in one place.  If  you want to add a page later, you only have to change that one array.

 

Also, use full php tags ( <?php ) some servers do not support the short ones ( <? ) and if you move to a new host you might spend a few hours trying to figure out why your site does not work there.

 

Also, rather than using two separate arrays that you have to keep in sync, I would be tempted to write it this was:

 

 

Well the only way I could get the title to show up in the title head tags was two separate arrays. It should would have been nice to use one file. But I did not get it to work.

 

I am going to try and make the index file a php enable file. I mean start the document with the opening php tag and then close it after all the html tags.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.