1

What I am trying to do is if an individual has a user_type of 'admin' they would would be able to see two links within a HTML table called 'Edit' and another link called 'Delete' under the 'Actions' column/header and if they have a user_type of 'user' I want to hide or conceal the entire 'Actions' column/header and its contents. Below is the code which was used:

<?php 
 if ($_SESSION['user_type'] == admin) {
 echo '<a href = "edit.php?pid=' . $row['Imageid'] . '">Edit</a> 
       | 
 <a href =  "delete.php?pid=' . $row['Imageid'] . '">Delete </a>';
 }
 ?>

However the specific problem I am experiencing is when the code is executed I am getting the following two errors:

'Notice: Undefined index: user_type in C:\wamp64\www\project\usermain.php on line 74'

and

'Notice: Use of undefined constant admin - assumed 'admin' in C:\wamp64\www\project\usermain.php on line 74'

I tried what was said here: Reference - What does this error mean in PHP? but I was unsuccessful. Can you please assist?

Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
  • 1
    `$_SESSION['user_type'] == admin` should probably be `$_SESSION['user_type'] == 'admin'`, and it does not seem like you set `$_SESSION['user_type']` on login, as you get an undefined index error – JimL Oct 08 '17 at 20:15
  • https://stackoverflow.com/questions/4015729/php-session-start follow this..and as like @JimL said its probably be $_SESSION['user_type'] == 'admin'.. – Mohammedshafeek C S Oct 08 '17 at 20:42
  • @MohammedShafeek I changed it to $_SESSION['user_type'] == 'admin' and the 'Notice: Use of undefined constant admin - assumed 'admin' in C:\wamp64\www\project\usermain.php on line 74' is gone and I did set $_SESSION['user_type'] on login previously on the login page but the 'Notice: Undefined index: user_type in C:\wamp64\www\project\usermain.php on line 74' error still persists. –  Oct 08 '17 at 21:48

1 Answers1

0

At the top of the page C:\wamp64\www\project\usermain.php you need to add

session_start();

When you do session_start() you are telling PHP that you want to use the session. This is made available to you as an array called $_SESSION. You can use that like any other array with the difference that the stuff you put in there stays there from one page to another (provided you use session_start() at the beginning of each page).

Mohammedshafeek C S
  • 1,916
  • 2
  • 16
  • 26
  • Hey, I did use session_start(); but in my login.php page which then logs me into the usermain.php page. However I also have another page called navigation.php which also has session_start(); and echos "Welcome ". $username ."! " when a user logs in. So instead of using ($_SESSION['user_type'] == 'admin') I used ($_SESSION['username']== 'Admin1') which is the actual username of the user and it worked. I do not know if it advisable or safe to do so but it works. Thank you so much for your help. –  Oct 09 '17 at 09:36