Hi everyone,
I am new at PHP and am in need of some help. I have a simple template system that also uses sessions as a member system.
I have an array that is used to construct my navigation and that is also used to specify which include files are valid include files. People in the forums may recognize some code as I borrow bits and pieces from all over.
My php code is below.
First the members page. Auth.php determines if the can even view it depending on login
<?php
//members.php
include("auth.php"); //needed to password protect pages
// begin content
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>title</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link href="css/layout.css" rel="stylesheet" type="text/css">
<link rel="stylesheet" media="screen" type="text/css" href="css/k1style.css">
<link href="css/menu.css" rel="stylesheet" type="text/css">
</head>
<body>
<div id="top">
<img src="img/bob.jpg" width="388" height="81">
</div>
<div id="left">
<table class="menu">
<?php
include($_SERVER['DOCUMENT_ROOT'].'/membersnav.php');
?>
</div>
<br><br>
<div id="main">
<a name="top"></a>
<center>
<?php require($_SERVER['DOCUMENT_ROOT'].'/memberscontent.php'); ?>
</center>
</div>
<br><center><a href="#top">Back to Top</a><br>
</body>
</html>
membersnav.php
Below is my simple navigation
<?php
// Login & Session example by sde
// membersnav.php
?>
<?php
include ('membersarray.php');
//The foreach breaks apart the array and defines the key ($k) and value ($v) pairs.
foreach($valid_pages AS $k => $v)
{
// If the value is equal to 1 and the key name is not a numerical value, then we echo the key name
//if($v == 1 && !is_numeric($k)){
//echo $k."<br />";
//echo $v."<br />";
//echo $_GET['p']."<br />";
if ($_GET['p'] == $v)
{
$view = "view";
}
else
{
$view = "notview";
}
$ufirst = ucfirst($v);
echo('<tr><td><a class="'.$view.'" href="members.php?p='.$v.'">'.$ufirst.'</a></td></tr>');
}
?>
<?php
echo('<tr><td><a class="notview" href="index.php">Home</a></td></tr>');
?>
<?php
echo('<tr><td><a class="notview" href="logout.php">Logout</a></td></tr>');
?>
</table>
<br><br><br>
the array that both membersnav.php and memberscontent.php reference to generate the menu and define a valid include page
<?php
//membersarray.php
$valid_pages = array(); // Declare $validPages as array (Good practice).
$valid_pages = array('members','fred','ted','contact'); // Put valid pages into array for inclusion.
?>
This page decides what content is included.
<?php
//memberscontent.php
include ('membersarray.php');
if(isset($_GET['p'])) {
$page = $_GET['p'];
} else {
$page = 'home';
}
if(eregi("^[A-Z0-9]+$", $page)) { // Make sure $page is alphanumeric.
$dir = '/includes/'; //not strictly necessary, can be blank.
$ext = '.php'; //.php, .html, .txt, whatever
if(file_exists($_SERVER['DOCUMENT_ROOT'].$dir.$page.$ext))
{
if(in_array($page, $valid_pages)) {
include($_SERVER['DOCUMENT_ROOT'].$dir.$page.'.php');
} else { echo 'Page is not set as a valid page.'; }
}
else { echo 'This page cannot be found.'; } // Or something similar.
} else { echo 'Naughty Naughty, very Naughty.'; }
?>
My problem is that I want multiple users to be able to login. Say i have two users fred and bob. I want if fred logs in then fred has an entry in the menu and bob does not. I only want the person who is logged to have a menu entry. At the moment if someone is logged in all users have a menu entry as they are in the array . They cannot view each others pages by passing get variables in the url but they can still see the name of the other users. I need some code to generate the array dynamically. Or a way to comment out parts of the array. Incidently my list of valid users comes from a mysql database.
Would it be possible to generate the array from the directory listing in the includes directory. The idea being if I wanted to add a page I only need to upload a php file into the includes directory and the menu is updated dynamically.
Any help appreciated. As well as any ideas to improve the script.