Jump to content
Larry Ullman's Book Forums

Ch 18 - Undefined Index: User_Level


Recommended Posts

Having issues here... All the links show up on the page that should, but I get an error code/array printed to the page.  If the "user_level" is an undefined index shouldn't it be an "index" in the table?  It isn't..  Not sure the issue here.

<!DOCTYPE HTML>
<head>
<meta http-equiv="content-type" content="text/html"; charset="utf-8" />
<title>Welcome to this Site!</title>
<style type="text/css" media="screen">@import "includes/layout.css";</style>
</head>
<body>
	<div id="Header">User Registration</div>
	<div id="Content">
<!-- End of Header --><h1>Welcome!</h1><h1>stuff</h1>
<p>Welcome to !</p>
<p>related roles </p>
	
<!-- START of Header -->
</div><!-- Content -->

<div id="Menu">
	<a href="index.php" title="Home Page">Home</a><br />
	<a href="logout.php" title="Logout">Logout</a><br />
		<a href="change_password.php" title="Change Your Password">Change Password</a><br />
		<div class="error">An error occurred in script '/Applications/MAMP/htdocs/PHP_MYSQL/includes/footer_ch18.html' on line 17:Undefined index: user_level<br />
Date/Time: 1-7-2014 01:38:45<br />
<pre>Array
(
    [_GET] => Array
        (
        )

    [_POST] => Array
        (
        )

    [_COOKIE] => Array
        (
            [PHPSESSID] => 94e3d43c2a4b7370631f30dfed03bb6c
        )

    [_FILES] => Array
        (
        )

    [page_title] => Welcome to this Site!
    [_SESSION] => Array
        (
            [user_id] => 1
            [user_tz] => America/New_York
            [lid] => 1
        )

)

#0  my_error_handler(8, Undefined index: user_level, /Applications/MAMP/htdocs/PHP_MYSQL/includes/footer_ch18.html, 17, Array ([_GET] => Array (),[_POST] => Array (),[_COOKIE] => Array ([PHPSESSID] => 94e3d43c2a4b7370631f30dfed03bb6c),[_FILES] => Array (),[page_title] => Welcome to this Site!,[_SESSION] => Array ([user_id] => 1,[user_tz] => America/New_York,[lid] => 1))) called at [/Applications/MAMP/htdocs/PHP_MYSQL/includes/footer_ch18.html:17]
#1  include(/Applications/MAMP/htdocs/PHP_MYSQL/includes/footer_ch18.html) called at [/Applications/MAMP/htdocs/PHP_MYSQL/index.php:29]
</pre></div><a href="view_users.php" title="View All Users">View Users</a><br />
			<a href="#">Some Admin Page</a><br />
			<a href="#">Some Page</a><br />
	<a href="#">Another Page</a><br />
	</div><!-- Menu -->
	
</body>
</html>

Link to comment
Share on other sites

Somebody.....help!!! :)  Here is what is printed out within the menu, between buttons CHANGE PASSWORD and SOME PAGE.  Again, I just don't know what it's reffering to. I looked up "undefined index" on stack overflow and it mentioned that the error can be caused by not using ISSET for the array/variable, but I'm assuming the code is correct in the book.

 

 

An error occurred in script '/Applications/MAMP/htdocs/PHP_MYSQL/includes/footer_ch18.html' on line 17:Undefined index: user_level
Date/Time: 1-7-2014 23:59:25

Array(    [_GET] => Array        (        )    [_POST] => Array        (        )    [_COOKIE] => Array        (            [PHPSESSID] => 94e3d43c2a4b7370631f30dfed03bb6c        )    [_FILES] => Array        (        )    [_SERVER] => Array        (            [HTTP_HOST] => localhost            [HTTP_USER_AGENT] => Mozilla/5.0 (Macintosh; Intel Mac OS X 10.6; rv:26.0) Gecko/20100101 Firefox/26.0            [HTTP_ACCEPT] => text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8            [HTTP_ACCEPT_LANGUAGE] => en-US,en;q=0.5            [HTTP_ACCEPT_ENCODING] => gzip, deflate            [HTTP_DNT] => 1            [HTTP_COOKIE] => PHPSESSID=94e3d43c2a4b7370631f30dfed03bb6c            [HTTP_CONNECTION] => keep-alive            [HTTP_CACHE_CONTROL] => max-age=0            [PATH] => /usr/bin:/bin:/usr/sbin:/sbin            [SERVER_SIGNATURE] =>             [SERVER_SOFTWARE] => Apache/2.2.23 (Unix) mod_ssl/2.2.23 OpenSSL/0.9.8r DAV/2 PHP/5.4.10            [SERVER_NAME] => localhost            [SERVER_ADDR] => 127.0.0.1            [SERVER_PORT] => 80            [REMOTE_ADDR] => 127.0.0.1            [DOCUMENT_ROOT] => /Applications/MAMP/htdocs            [SERVER_ADMIN] => you@example.com            [SCRIPT_FILENAME] => /Applications/MAMP/htdocs/PHP_MYSQL/register_ch18.php            [REMOTE_PORT] => 55947            [GATEWAY_INTERFACE] => CGI/1.1            [SERVER_PROTOCOL] => HTTP/1.1            [REQUEST_METHOD] => GET            [QUERY_STRING] =>             [REQUEST_URI] => /PHP_MYSQL/register_ch18.php            [SCRIPT_NAME] => /PHP_MYSQL/register_ch18.php            [PHP_SELF] => /PHP_MYSQL/register_ch18.php            [REQUEST_TIME_FLOAT] => 1389157165.39            [REQUEST_TIME] => 1389157165            [argv] => Array                (                )            [argc] => 0        )    [page_title] => Register    [_SESSION] => Array        (            [user_id] => 1            [user_tz] => America/New_York            [lid] => 1        ))#0  my_error_handler(8, Undefined index: user_level, /Applications/MAMP/htdocs/PHP_MYSQL/includes/footer_ch18.html, 17, Array ([_GET] => Array (),[_POST] => Array (),[_COOKIE] => Array ([PHPSESSID] => 94e3d43c2a4b7370631f30dfed03bb6c),[_FILES] => Array (),[_SERVER] => Array ([HTTP_HOST] => localhost,[HTTP_USER_AGENT] => Mozilla/5.0 (Macintosh; Intel Mac OS X 10.6; rv:26.0) Gecko/20100101 Firefox/26.0,[HTTP_ACCEPT] => text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8,[HTTP_ACCEPT_LANGUAGE] => en-US,en;q=0.5,[HTTP_ACCEPT_ENCODING] => gzip, deflate,[HTTP_DNT] => 1,[HTTP_COOKIE] => PHPSESSID=94e3d43c2a4b7370631f30dfed03bb6c,[HTTP_CONNECTION] => keep-alive,[HTTP_CACHE_CONTROL] => max-age=0,[PATH] => /usr/bin:/bin:/usr/sbin:/sbin,[SERVER_SIGNATURE] => ,[SERVER_SOFTWARE] => Apache/2.2.23 (Unix) mod_ssl/2.2.23 OpenSSL/0.9.8r DAV/2 PHP/5.4.10,[SERVER_NAME] => localhost,[SERVER_ADDR] => 127.0.0.1,[SERVER_PORT] => 80,[REMOTE_ADDR] => 127.0.0.1,[DOCUMENT_ROOT] => /Applications/MAMP/htdocs,[SERVER_ADMIN] => you@example.com,[SCRIPT_FILENAME] => /Applications/MAMP/htdocs/PHP_MYSQL/register_ch18.php,[REMOTE_PORT] => 55947,[GATEWAY_INTERFACE] => CGI/1.1,[SERVER_PROTOCOL] => HTTP/1.1,[REQUEST_METHOD] => GET,[QUERY_STRING] => ,[REQUEST_URI] => /PHP_MYSQL/register_ch18.php,[SCRIPT_NAME] => /PHP_MYSQL/register_ch18.php,[PHP_SELF] => /PHP_MYSQL/register_ch18.php,[REQUEST_TIME_FLOAT] => 1389157165.39,[REQUEST_TIME] => 1389157165,[argv] => Array (),[argc] => 0),[page_title] => Register,[_SESSION] => Array ([user_id] => 1,[user_tz] => America/New_York,[lid] => 1))) called at [/Applications/MAMP/htdocs/PHP_MYSQL/includes/footer_ch18.html:17]#1  include(/Applications/MAMP/htdocs/PHP_MYSQL/includes/footer_ch18.html) called at [/Applications/MAMP/htdocs/PHP_MYSQL/register_ch18.php:116]
Link to comment
Share on other sites

Ok, so I took a chance and added ISSET to the footer file, and I think it worked!

 

Can somebody explain why it now works and shows no errors?  Is this errata or not? 

 

 

This version works - to make it work I added:

 

            if (isset($_SESSION['user_level'])) {

 

}

<!-- START of Header -->
</div><!-- Content -->

<div id="Menu">
	<a href="index.php" title="Home Page">Home</a><br />
	<?php # Script 18.2 - footer_ch18.html
	// This page completes the HTML template
	
	// Display links based upon the login status
	if (isset($_SESSION['user_id'])) {
		
		echo '<a href="logout.php" title="Logout">Logout</a><br />
		<a href="change_password.php" title="Change Your Password">Change Password</a><br />
		';
		
			// Add links if the user is an ADMIN
			if (isset($_SESSION['user_level'])) {
			
			if ($_SESSION['user_level'] == 1) {
				echo '<a href="view_users.php" title="View All Users">View Users</a><br />
			<a href="#">Some Admin Page</a><br />
			';
			}
		}	
	} else { // Not logged in.
		echo '<a href="register.php" title="Register for the site">Register</a><br />
		<a href="login.php" title="Login">Login</a><br />
		<a href="forgot_password.php" title="Password Retrieval">Retrieve Password</a><br />
';
}
?>
<a href="#">Some Page</a><br />
	<a href="#">Another Page</a><br />
	</div><!-- Menu -->
	
</body>
</html>
<?php // Flush the buffered output.
ob_end_flush();
?>
Link to comment
Share on other sites

Thank you for your time in your response here Larry.  I'll check this out - I'm sure I should have provided extra code but at the end of the book there are so many pages of code I just hoped that the error would prompt somebody familiar to point me in the right direction.

Link to comment
Share on other sites

 Share

×
×
  • Create New...