azdrian21 Posted December 2, 2011 Share Posted December 2, 2011 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 ! Quote Link to comment https://forums.phpfreaks.com/topic/252312-notice-undefined-index-user_name-in-cwampwwwpennacool_forumloggedinphp/ Share on other sites More sharing options...
sunfighter Posted December 2, 2011 Share Posted December 2, 2011 To save some time in trouble shooting this, because you never gave us the log in script put session_start(); as the first line [iT SHOULD BE THE FIRST LINE] in login script: and not in line 11 Quote Link to comment https://forums.phpfreaks.com/topic/252312-notice-undefined-index-user_name-in-cwampwwwpennacool_forumloggedinphp/#findComment-1293645 Share on other sites More sharing options...
azdrian21 Posted December 3, 2011 Author Share Posted December 3, 2011 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'); ?> Quote Link to comment https://forums.phpfreaks.com/topic/252312-notice-undefined-index-user_name-in-cwampwwwpennacool_forumloggedinphp/#findComment-1293882 Share on other sites More sharing options...
scootstah Posted December 3, 2011 Share Posted December 3, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/252312-notice-undefined-index-user_name-in-cwampwwwpennacool_forumloggedinphp/#findComment-1293921 Share on other sites More sharing options...
sunfighter Posted December 3, 2011 Share Posted December 3, 2011 Did you move the session_start as I asked you to? Didn't that fix the problem? Quote Link to comment https://forums.phpfreaks.com/topic/252312-notice-undefined-index-user_name-in-cwampwwwpennacool_forumloggedinphp/#findComment-1293939 Share on other sites More sharing options...
azdrian21 Posted December 3, 2011 Author Share Posted December 3, 2011 yes i tried moving it at the top of the file but it still not working! Quote Link to comment https://forums.phpfreaks.com/topic/252312-notice-undefined-index-user_name-in-cwampwwwpennacool_forumloggedinphp/#findComment-1293979 Share on other sites More sharing options...
sunfighter Posted December 4, 2011 Share Posted December 4, 2011 Giving us the error message helps a lot. 'still not working' doesn't. 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). Quote Link to comment https://forums.phpfreaks.com/topic/252312-notice-undefined-index-user_name-in-cwampwwwpennacool_forumloggedinphp/#findComment-1294380 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.