# User authentication

**URL:** <https://forum.kirupa.com/t/user-authentication/42038>\
**Category:** Uncategorized\
**Created:** [February 4, 2004, 1:36pm UTC](https://forum.kirupa.com/t/user-authentication/42038 "2004-02-04T13:36:09Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![gam99](https://avatars.discourse-cdn.com/v4/letter/g/ec9cab/32.png) [@gam99](https://forum.kirupa.com/u/gam99)\
**Post date:** [February 4, 2004, 1:36pm UTC](https://forum.kirupa.com/t/user-authentication/42038/1 "2004-02-04T13:36:09Z")

</div>

I have been trying to set up a login system that directs a user to a specific page depending on their access level that is set in the database. I’m still new to PHP and the script I’ve been using keeps returning and error. I have set this up from a sample code I found, but I can’t get it to work. If anyone can see whats wrong with this code, please help me. Or if you have any suggestion on how to achieve what I need I’d love to hear them.

The errors are:  
Notice: Undefined index: auth\_level in c:\inetpub\wwwroot\login\Login.php on line 34

Notice: Undefined index: auth\_level in c:\inetpub\wwwroot\login\Login.php on line 37

Notice: Undefined index: auth\_level in c:\inetpub\wwwroot\login\Login.php on line 40

The code I’ve been using is:

```php
<?php 

//start sessions 
ob_start(); 
session_start(); 

//ie error checking 
header("Cache-control: private"); 

//connect to database 
mysql_connect("localhost","XXX","XXX"); 
mysql_select_db("XXX") or die("could not select db"); 

$username = "";

//extract user information from database 
if ($username && $password) 
{ 

// if the user has just tried to log in 
$query = "SELECT * FROM users WHERE username='$username' AND passwd='$password' "; 
$result = mysql_query($query); 
$row = mysql_fetch_assoc($result); 

  if (mysql_num_rows($result) >0) 
  { 
    // if they are in the database register the user id 
    $_SESSION['valid_user'] = $row['username']; 
    $_SESSION['pass'] = $_POST['password']; 
    $_SESSION['auth_level'] = $row['auth_level']; 
     } 
} 

if ($_SESSION['auth_level']==1){ 
            header("Location: Menu1.php"); 
         } 
         else if($_SESSION['auth_level']==2){ 
          header("Location: Menu2.php"); 
         }
		 else if($_SESSION['auth_level']==3){ 
          header("Location: Menu3.php"); 
         }
?>

```

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [February 4, 2004, 1:44pm UTC](https://forum.kirupa.com/t/user-authentication/42038/2 "2004-02-04T13:44:27Z")

</div>

i am trying to develop a login script for a site. like you im new to php… probly newer since i still have to learn Sessions… i will take a look at this post again… hmm… kirupa should put line numbers when we post codes… 🙂

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [February 4, 2004, 2:04pm UTC](https://forum.kirupa.com/t/user-authentication/42038/3 "2004-02-04T14:04:54Z")

</div>

Try giving another name to the session variable. I think assigning a password to sessions is a bad idea. All you actually need to register the user id is just to take the username and assign it to a session variable. What i would do in your case is assign the auth\_level to a variable(like I would use $auth\_level to achieve this), and then use that variable in the if statements. Or maybe you’re trying o achieve something else…
