3 Replies - 523 Views - Last Post: 14 September 2017 - 10:30 PM Rate Topic: -----

#1 hannibalbarca1994  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 14-September 17

Can I please get some help with this php code?

Posted 14 September 2017 - 03:44 PM

I am trying to build a dynamic website for a school project by using wamp server, but I am new to programming and I am not too sure if what I am doing is correct so I need some assistance here. What I am trying to do is this:

When localhost is typed into the browser and the name of the folder lets say woodly_database is selected it takes them to a page called index.php with the following code:

<?php
     session_start();
     if (empty($POST['username'])){
         header ('location:login.php');
     }
     if (isset($SESSION['username'])){
         header ('location:usermain.php');
     }else{
         $_SESSION['user_type'] = $_POST['user_type'];
         header ('location:home.php');
     } 
  ?>


Now, what this code is suppose to do is that if someone is not logged in then it keeps them on the login page login.php but if they have the user_type of user then it is suppose to take them to a page called usermain.php and if they have a user_type of admin then it takes them to a page called home.php. But within home.php and usermain.php there is another page called navigation.php which was done using this : and within navigation.php the following code exist:

<?php
     session_start();
     if (empty($_SESSION['username'])){ 
         header('location:login.php');  
     }
     if (isset($_SESSION['user']) && $_SESSION['user']['user_type'] =='admin'){
         header ('location:home.php');
     }else{
         $username = $_SESSION['username'];
         echo "Welcome ". $username ."! "; 
     }
 ?>


Next thing is that I think that the second code above is suppose to process what is to be done from the index.php page but the thing is I am not too sure what is actually happening here and I have been doing a lot of trial and error. So the overall result I want is that when a person with a user_type of user logs in through login.php they go to the usermain.php page and when a person with the user_type of admin logs in they go to the home.php page.

Is This A Good Question/Topic? 0
  • +

Replies To: Can I please get some help with this php code?

#2 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3714
  • View blog
  • Posts: 13,468
  • Joined: 08-August 08

Re: Can I please get some help with this php code?

Posted 14 September 2017 - 03:59 PM

Since the user type is stored in $_SESSION['user']['user_type'] you want to use that to determine where to send them.
I prefer to store them in an array so that I can avoid a series of if...elseif... statements.
$locations = ['admin'=>'location:home.php', 'user'=>'location:usermain.php'];
if(array_key_exits($_SESSION['user']['user_type'], $locations)) {
	header ($locations[$_SESSION['user']['user_type']]);
} else {
	header ($locations['location:login.php']);
}

Was This Post Helpful? 0
  • +
  • -

#3 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 5073
  • View blog
  • Posts: 13,697
  • Joined: 18-April 07

Re: Can I please get some help with this php code?

Posted 14 September 2017 - 04:18 PM

I also noticed that line 2 from your first snippet is missing an underscore. It should be $_POST and not $POST. :)
Was This Post Helpful? 0
  • +
  • -

#4 andrewsw  Icon User is online

  • the case is sol-ved
  • member icon

Reputation: 6377
  • View blog
  • Posts: 25,768
  • Joined: 12-December 12

Re: Can I please get some help with this php code?

Posted 14 September 2017 - 10:30 PM

... and $SESSION ;)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1