Jump to content

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


azdrian21

Recommended Posts

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/login_functions.inc.php'); 
    $url = absolute_url(); 
    header("Location: $url"); 
    exit(); 
} 

$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/login_functions.inc.php'); 
    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_start(); 
        $_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"); 
        exit(); 

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

    mysqli_close($dbc); 

} // End of the main submit conditional. 

include ('includes/login_page.inc.php'); 
?>

 

thanks in advance guys ! 8)

oh sorry this is the login page:

 

<?php   //login_Page.inc.php

$page_title ='Login';
include('includes/header.html');

if (!empty($errors)) {
     echo '<h1>Error!</h1><p class="error">The following error(s) occurred:<br />';
   foreach ($errors as $msg) {
     echo " - $msg<br />\n";
   }
echo '</p><p>Please try again. </p>';
}
?>

<h1>Login Page</h1>

<!-- Start of FORM -->
<form method="post" action="login.php">
<p>Username: <input type="text" name="user_name" size="20" maxlength="80" value=" <?php if (isset($_POST['user_name'])) {echo $_POST['user_name'];} ?> "></p>
<p>Password: <input type="password" name="user_pass" size="20" maxlength="20"></p>
<p><input type="submit" name="submit" value="login"></p>
<input type="hidden" name="submitted" value="TRUE">
</form>
<!-- End of FORM -->
<?php include('includes/footer.html'); ?>

This is the problem:

list ($check, $data) = check_login($dbc, $_POST['user_name'], $_POST['user_pass']); 

    if ($check) { // OK! 

        // Set the session data:. 
        session_start(); 

 

You called session_start() AFTER you set session data. session_start() should always be at the top of a PHP file.

Giving us the error message helps a lot. 'still not working' doesn't. :shrug:

 

absolute_url is not a php thing.

change:

$url = absolute_url ('loggedin.php');

        header("Location: $url");

 

to header("Location: loggedin.php");

 

need to do this for login.php and loggedin.php (where its not defined at all).

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.