Notice: Undefined index: user_name in C:\\wamp\\www\\pennacool_forum\\loggedin.php on lin

hello all i am building a small forum website for kids to discuss their school work etc. As this website will be assess by my prospect employer for a job as a entry level web apllication developer.

The problem is am having this error :
Notice: Undefined index: user_name in C:\wamp\www\pennacool_forum\loggedin.php on line 21
when i tried to display the user name in the session of the user who just loggedin.

here is the loggedin.php script:

<?php  // loggedin.php 

// The user is redirected here from login.php.

session_start(); // Start the session.

// If no session value is present, redirect the user:
// Also validate the HTTP_USER_AGENT!
if (!isset($_SESSION['agent']) OR ($_SESSION['agent'] != md5($_SERVER['HTTP_USER_AGENT']) )) {
	require_once ('includes/');
	$url = absolute_url();
	header("Location: $url");

$page_title = 'Logged In!';
include ('includes/header.html');

// Print a customized message:
echo "<h1>Logged In!</h1>
<p>You are now logged in, {$_SESSION['user_name']}!</p>
<p><a href=\\"logout.php\\">Logout</a></p>";

include ('includes/footer.html');

this is the login function:

function check_login($dbc, $user_name = '', $user_pass = '') {

	$errors = array(); // Initialize error array.

	// Validate the email address:
	if (empty($user_name)) {
		$errors[] = 'You forgot to enter your username.';
	} else {
		$u = mysqli_real_escape_string($dbc, trim($user_name));

	// Validate the password:
	if (empty($user_pass)) {
		$errors[] = 'You forgot to enter your password.';
	} else {
		$p = mysqli_real_escape_string($dbc, trim($user_pass));

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

		// Retrieve the user_id and first_name for that email/password combination:
		$q = "SELECT user_id, user_email FROM users WHERE user_name='$u' AND user_pass=SHA1('$p')";
		$r = @mysqli_query ($dbc, $q); // Run the query.

		// Check the result:
		if (mysqli_num_rows($r) == 1) {

			// Fetch the record:
			$row = mysqli_fetch_array ($r, MYSQLI_ASSOC);

			// Return true and the record:
			return array(true, $row);

		} else { // Not a match!
			$errors[] = 'The username and password entered do not match those on file.';

	} // End of empty($errors) IF.

	// Return false and the errors:
	return array(false, $errors);

} // End of check_login() function.


here is the login script:

<?php #login.php 

if (isset($_POST['submitted'])) {
	require_once ('includes/');
	require_once ('mysqli_connect.php');
	list ($check, $data) = check_login($dbc, $_POST['user_name'], $_POST['user_pass']);

	if ($check) { // OK!

		// Set the session data:.
		$_SESSION['user_id'] = $data['user_id'];
		$_SESSION['user_name'] = $data['user_name'];

		// Store the HTTP_USER_AGENT:
		$_SESSION['agent'] = md5($_SERVER['HTTP_USER_AGENT']);

		// Redirect:
		$url = absolute_url ('loggedin.php');
		header("Location: $url");

	} else { // Unsuccessful!
		$errors = $data;


} // End of the main submit conditional.

include ('includes/');

And if you guys have any ideas to what kind of sample website i can build to land me a job . A simple one.
thanks for all your help in advance.

That error message is telling you that

$_SESSION[‘user_name’] in

<p>You are now logged in, {$_SESSION['user_name']}!</p>

does not exist.

So you just need to use some common sense and do some basic debugging to work out why.

Since $_SESSION[‘user_name’] doesn’t exist in the above line, the logical thing to do is back track your code and find the line where $_SESSION[‘user_name’] is set a value. It’s a pretty safe bet that the variable that is assigned to $_SESSION[‘user_name’] does not exist and so you back track your code even further to find where the other variable is set, and so on until you find the root cause of the problem. Then fix it :slight_smile:

hint: you can use echo statements to check values of variables and to check if parts of your code are reached.

$q = “SELECT user_id, user_email FROM users WHERE user_name=‘$u’ AND user_pass=SHA1(‘$p’)”;
$_SESSION[‘user_name’] = $data[‘user_name’];

You didnt select a user_name field. This is an undefined index.

just like StarLion has done for you :slight_smile:

what a silly mistake! i examine my code a million times not knowing i overlooked that SQL line. thanks alot guys it worked. i left out the user_name in the SQL statement

webdev1958 & StarLion :drink: