Hello:
I'm having a little problem with a script and I can't seem to figure out what's wrong. This is what I'm doing:
1. I have a login script where a person enters their username and password - no problem here
2. Then, a welcome page appears showing the user's name and a link to View Files.
The goal is for the user to click on the View Files link and only see the files that belong to him/her. When I click on the link, I receive this message:
Notice: Undefined index: username in C:\Inetpub\fullfocus\Client\view_files.php on line 14
I do not understand what this message is trying to tell me.
I've included the code for all three scripts. If anyone can help me out I'd be greatly appreciative. Thank you in advance.
Login Script:
<?php
// This is the login page for the site.
// Include the configuration file for error management and such.
require_once ('config.inc.php');
// Set the page title and include the HTML header.
$page_title = 'Login';
include ('header3.html');
if (isset($_POST['submitted'])) { // Check if the form has been submitted.
require_once ('mysql_connect.php'); // Connect to the database.
// Validate the email address.
if (!empty($_POST['email'])) {
$e = escape_data($_POST['email']);
} else {
echo '<p><font color="red" size="+1">You forgot to enter your email address!</font></p>';
$e = FALSE;
}
// Validate the email address.
if (!empty($_POST['username'])) {
$u = escape_data($_POST['username']);
} else {
echo '<p><font color="red" size="+1">You forgot to enter your email address!</font></p>';
$u = FALSE;
}
// Validate the password.
if (!empty($_POST['password'])) {
$p = escape_data($_POST['password']);
} else {
$p = FALSE;
echo '<p><font color="red" size="+1">You forgot to enter your password!</font></p>';
}
if ($u && $p) { // If everything's OK.
// Query the database.
$query = "SELECT client_id, first_name FROM clients WHERE (username='$u' AND password=SHA('$p'))";
$result = mysql_query ($query) or trigger_error("Query: $query\n<br />MySQL Error: " . mysql_error());
if (@mysql_num_rows($result) == 1) { // A match was made.
// Register the values & redirect.
$row = mysql_fetch_array ($result, MYSQL_NUM);
mysql_free_result($result);
mysql_close(); // Close the database connection.
$_SESSION['first_name'] = $row[1];
$_SESSION['user_id'] = $row[0];
// 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 .= '/welcome.php';
ob_end_clean(); // Delete the buffer.
header("Location: $url");
exit(); // Quit the script.
} else { // No match was made.
echo '<p><font color="red" size="+1">Either the email address and password entered do not match those on file or you have not yet activated your account.</font></p>';
}
} else { // If everything wasn't OK.
echo '<p><font color="red" size="+1">Please try again.</font></p>';
}
mysql_close(); // Close the database connection.
} // End of SUBMIT conditional.
?>
<h1>Login</h1>
<p>Your browser must allow cookies in order to log in.</p>
<form action="login.php" method="post">
<fieldset>
<p><b>Username:</b> <input type="text" name="username" size="20" maxlength="40" value="<?php if (isset($_POST['username'])) echo $_POST['username']; ?>" /></p>
<p><b>Password:</b> <input type="password" name="password" size="20" maxlength="20" /></p>
<div align="center"><input type="submit" name="submit" value="Login" /></div>
<input type="hidden" name="submitted" value="TRUE" />
</fieldset>
</form>
<?php // Include the HTML footer.
include ('footer3.html');
?>
This is the welcome script:
<?php
// This is the main page for the site.
// Include the configuration file for error management and such.
require_once ('config.inc.php');
// Set the page title and include the HTML header.
$page_title = 'PHP and MySQL for Dynamic Web Sites: Visual QuickStart Guide (2nd Edition)';
include ('header3.html');
// Welcome the user (by name if they are logged in).
echo '<h1>Welcome';
if (isset($_SESSION['first_name'])) {
echo ", {$_SESSION['first_name']}!";
}
echo '</h1>';
?>
<p><a href="view_files.php">View Files</a></p>
<?php // Include the HTML footer file.
include ('footer3.html');
?>
This is the View Files script:
<?php // This page displays the files uploaded to the server.
// Set the page title and include the HTML header.
$page_title = 'View Files';
include ('header3.html');
require_once ('mysql_connect.php'); // Connect to the database.
$first = TRUE; // Initialize the variable.
// Query the database.
$query = "SELECT upload_id, file_name, ROUND(file_size/1024) AS fs, description FROM uploads WHERE username='$_SESSION[username]'";
$result = mysql_query ($query);
// Display all the URLs.
while ($row = mysql_fetch_array ($result, MYSQL_ASSOC)) {
// If this is the first record, create the table header.
if ($first) {
echo '<table border="0" width="100%" cellspacing="3" cellpadding="3" align="center">
<tr>
<td align="left" width="20%"><font size="+1">File Name</font></td>
<td align="left" width="40%"><font size="+1">Description</font></td>
<td align="center" width="20%"><font size="+1">File Size</font></td>
</tr>';
$first = FALSE; // One record has been returned.
} // End of $first IF.
// Display each record.
echo " <tr>
<td align=\"left\"><a href=\"download_file.php?uid={$row['upload_id']}\">{$row['file_name']}</a></td>
<td align=\"left\">" . stripslashes($row['description']) . "</td>
<td align=\"center\">{$row['fs']}kb</td>
</tr>\n";
} // End of while loop.
// If no records were displayed...
if ($first) {
echo '<div align="center">There are currently no files to be viewed.</div>';
} else {
echo '</table>'; // Close the table.
}
mysql_close(); // Close the database connection.
include ('footer3.html');
?>