Jump to content

Dynamic menu not showing on only 1 page


bacarudaguy

Recommended Posts

So I'm working on developing a custom script for a site I manage and I'm still in the very beginnings of it, but seem to be having a problem.

 

I have a header and footer file to handle their tasks. The footer file contains the menu which has dynamically changing links based on $_SESSION variables of being logged in or not.

 

The ONLY page the menu isn't working on is change_password.php. I've concluded that something on that page is interfering with my footer.html page and the menu links. I've included the code to footer, and change_password with hopes of someone spotting my simple (and prolly stupid) mistake... ::);)

 

EDIT: Attached is a screen capture of the menu showing properly and not showing properly... maybe it'll help...

 

footer.html

<!-- begin footer //-->
</div> <!-- end content div //-->

<div id="menu">
[<a href="index.php">Home</a>]
<br />

<?php

//dynamically create login/logout/forgot/change password links
if (isset($_SESSION['user_id']) AND (substr($_SERVER['SCRIPT_NAME'], -10) != 'logout.php')) { //logged in

	echo '[<a href="logout.php">Logout</a>]<br />';

	//require database connection to check for admin
	require ('./connect.php');

	//query db for new pass
	$user = $_SESSION['user_id'];
	$query = "SELECT user_id, newpass FROM users WHERE (user_id = $user) AND (newpass = 1)";
	$result = mysql_query ($query) or trigger_error ("Query: $query\n<br />MySQL Error: " . mysql_error());

	//if it matched
	if (mysql_num_rows($result) == 1) {

		//show change pw link in red, then set new pass in db to 0			
		echo '<span class="error"><b>[</b><a href="change_password.php">Change Password</a><b>]</b></span><br />';
		//don't update quite yet - will remove this, update on chg pw page
		//$query = "UPDATE users SET newpass = 0 WHERE user_id = $user";
		//$result = mysql_query ($query) or trigger_error ("Query: $query\n<br />MySQL Error: " . mysql_error());			

	} else {

		//newpass already 0 - do nothing
		echo '[<a href="change_password.php">Change Password</a>]<br />';

	}

	//close database
	mysql_close();

	echo '[<a href="new_tournament.php">New Tournament</a>]<br />';

	//check for logged in user and see if they are admin		
	if (isset($_SESSION['user_id']) AND ($_SESSION['admin'] == '1')) {
		echo '[<a href="manage_users.php">Manage Users</a>]<br />[<a href="manage_tournaments.php">Manage Tournaments</a>]<br />[<a href="manage_seasons.php">Manage Seasons</a>]<br />';
	}

} elseif ($allowregistration == TRUE) { //not logged in/allowing registrations

	echo '[<a href="register.php">Register</a>]<br />[<a href="login.php">Login</a>]<br />[<a href="forgot_password.php">Forgot Password</a>]<br />';

} else { //not logged in/not allowing registrations

	echo '[<a href="login.php">Login</a>]<br />[<a href="forgot_password.php">Forgot Password</a>]<br />';
}
?>

[<a href="leaderboard.php">View Leaderboard</a>]
<br />
[<a href="help.php">Help</a>]
</div> <!-- end menu //-->
</body>
</html>

<?php
//flush buffered output
ob_end_flush();
?>

 

change_password.php

<?php
//require config file
require_once ('./includes/config.inc.php');

//set page title & include header
$page_title = 'Change Password';
include ('./includes/header.html');

//require database connection to check for admin
require_once ('./connect.php');

// If no first_name variable exists, redirect the user.
if (!isset($_SESSION['first_name'])) {

// Start defining the URL.
$url = 'http://' . $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']);
// Check for a trailing slash.
if ((substr($url, -1) == '/') OR (substr($url, -1) == '\\') ) {
	$url = substr ($url, 0, -1); // Chop off the slash.
}
// Add the page.
$url .= '/index.php';

ob_end_clean(); // Delete the buffer.
header("Location: $url");
exit(); // Quit the script.

} else {

if (isset($_POST['submitted'])) { // Handle the form.

	// Check for a new password and match against the confirmed password.
	if (eregi ('^[[:alnum:]]{4,20}$', stripslashes(trim($_POST['password1'])))) {
		if ($_POST['password1'] == $_POST['password2']) {
			$p = escape_data($_POST['password1']);
		} else {
			$p = FALSE;
			echo '<p class="error">Your password did not match the confirmed password!</p>';
		}
	} else {
		$p = FALSE;
		echo '<p class="error">Please enter a valid password!</p>';
	}

	if ($p) { // If everything's OK.

		// Make the query.
		$user = $_SESSION['user_id'];
		$query = "UPDATE users SET pass=SHA('$p') WHERE user_id=$user";		
		$result = mysql_query ($query) or trigger_error("Query: $query\n<br />MySQL Error: " . mysql_error());
		if (mysql_affected_rows() == 1) { // If it ran OK.

			// Send an email, if desired.
			echo '<h3>Your password has been changed.</h3>';
			mysql_close(); // Close the database connection.
			include ('./includes/footer.html'); // Include the HTML footer.
			exit();				

		} else { // If it did not run OK.

			// Send a message to the error log, if desired.
			echo '<p class="error">Your password could not be changed due to a system error. We apologize for any inconvenience.</font></p>'; 

		}		

	} else { // Failed the validation test.
		echo '<p class="error">Please try again.</font></p>';		
	}

	mysql_close(); // Close the database connection.

} // End of the main Submit conditional.

?>

<fieldset>
<legend>Change Password</legend>
<form action="change_password.php" method="post">
	<fieldset>
	<p><b>New Password:</b> <input type="password" name="password1" size="20" maxlength="20" /> <small>Use only letters and numbers. Must be between 4 and 20 characters long.</small></p>
	<p><b>Confirm New Password:</b> <input type="password" name="password2" size="20" maxlength="20" /></p>
	</fieldset>
	<div align="center"><input type="submit" name="submit" value="Change My Password" /></div>
	<input type="hidden" name="submitted" value="TRUE" />
</form>
</fieldset>

<?php
} // End of the !isset($_SESSION['first_name']) ELSE.
include ('./includes/footer.html');
?>

 

[attachment deleted by admin]

Link to comment
https://forums.phpfreaks.com/topic/184136-dynamic-menu-not-showing-on-only-1-page/
Share on other sites

Question?

 

 

do you have

 

session_start();

 

at the top of both php files? I can't see

Yes - it's in my header file that is included on change_password.php. Here's the code for it...

 

<?php

//output buffering
ob_start();

//begin sessions
session_start();

//check for page title
if (!isset($page_title)) {
$page_title = 'Official Leaderboard';
}

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
<title>[Joker's Poker League Leaderboard] - <?php echo $page_title; ?></title>
<style type="text/css" media="screen">@import "./includes/style.css";</style>
</head>
<body>
<div id="header"><h1>Joker's Poker League Leaderboard</h1></div>

<div id="content">

<!-- end header //-->

Ok - partially solved by adding an IF to check if the database var exists ($dbc). I think I'm on to something here and will post my solved solution when I get it. Here's my fix so far. (This is just a snipit of the code I was having a problem with - FYI)

 

footer.html

<?php
if (!$dbc) {

		//require database connection to check for admin
		require_once ('./connect.php');

		//query db for new pass
		$user = $_SESSION['user_id'];
		$query = "SELECT user_id, newpass FROM users WHERE (user_id = $user) AND (newpass = 1)";
		$result = mysql_query ($query) or trigger_error ("Query: $query\n<br />MySQL Error: " . mysql_error());

		//if it matched
		if (mysql_num_rows($result) == 1) {

			//show change pw link in red, then set new pass in db to 0			
			echo '<span class="error"><b>[</b><a href="change_password.php">Change Password</a><b>]</b></span><br />';
			//don't update quite yet - will remove this, update on chg pw page
			//$query = "UPDATE users SET newpass = 0 WHERE user_id = $user";
			//$result = mysql_query ($query) or trigger_error ("Query: $query\n<br />MySQL Error: " . mysql_error());			

		} elseif (mysql_num_rows($result) == 0) {

			//newpass already 0 - do nothing
			echo '[<a href="change_password.php">Change Password</a>]<br />';

		}

		//free result & close database
		mysql_free_result($result);
		mysql_close();

	} //end if !$dbc check
?>

Solved!

 

Here's the snipit in the footer.html that did it!

 

<?php

//dynamically create login/logout/forgot/change password links
if (isset($_SESSION['user_id']) AND ($_SERVER['SCRIPT_NAME'] != 'logout.php')) { //logged in 

	echo '[<a href="logout.php">Logout</a>]<br />';

	//does db connection exsist
	if ($dbc) {

		//require database connection to check for admin
		//require_once ('./connect.php');

		//query db for new pass
		$user = $_SESSION['user_id'];
		$query = "SELECT newpass FROM users WHERE (user_id = $user) AND (newpass = 1)";
		$result = mysql_query ($query) or trigger_error ("Query: $query\n<br />MySQL Error: " . mysql_error());

		list ($newpass) = mysql_fetch_array ($result, MYSQL_NUM);

		//if it matched
		if ($newpass == '1') {

			//show change pw link in red, then set new pass in db to 0			
			echo '<span class="error"><b>[</b><a href="change_password.php">Change Password</a><b>]</b></span><br />';

		} else {

			//newpass already 0 - do nothing
			echo '[<a href="change_password.php">Change Password</a>]<br />';

		} //end if 

		//free result & close database
		mysql_free_result($result);
		mysql_close();

	} //end if !$dbc check

	echo '[<a href="new_tournament.php">New Tournament</a>]<br />';

	//check for logged in user and see if they are admin		
	if (isset($_SESSION['user_id']) AND ($_SESSION['admin'] == '1')) {
		echo '[<a href="manage_users.php">Manage Users</a>]<br />[<a href="manage_tournaments.php">Manage Tournaments</a>]<br />[<a href="manage_seasons.php">Manage Seasons</a>]<br />';
	}

} elseif ($allowregistration == TRUE) { //not logged in/allowing registrations

	echo '[<a href="register.php">Register</a>]<br />[<a href="login.php">Login</a>]<br />[<a href="forgot_password.php">Forgot Password</a>]<br />';

} else { //not logged in/not allowing registrations

	echo '[<a href="login.php">Login</a>]<br />[<a href="forgot_password.php">Forgot Password</a>]<br />';
}
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.