0

I want to redirect the user to their proper main page. I also want to keep the sessions that I have know in my login.php. In my users table I store the username, password and type.

login.php

<?php

require ('connect.php');
if (isset($_POST['submit'])) {

$username = $_POST['username'];
$password = $_POST['password'];


if ($username && $password) {
    $check = mysql_query("SELECT * FROM users WHERE username='".$username."' AND password= '".$password."'");
    $rows = mysql_num_rows($check);


    if(mysql_num_rows($check) != 0){

      session_start();
      $run_login =mysql_fetch_array($check);
      $uid = $run_login['id'];
      $_SESSION['uid'] = ['uid'];
      $_SESSION['username']=$_POST['username'];
      header("location:../../statistics/home.php");

    }
    else{
      die("Could not find the Username or password.");
   }
}
 else {
     echo "Please fill all the fields.";
 }
}
?>
aurora
  • 161
  • 4
  • 15
  • What is problem with current code ? – Codelord May 01 '15 at 09:41
  • @ Codelord It leads the user to the `home.php` and they can select any to page to do whatever. I have 4 types of users lecturer, principal, admin, academic drirector. Each type of user should not go to other's user page. That is what I want to solve. – aurora May 01 '15 at 09:44
  • If you have role attribute in table then add that attribute into the session. And access that session on your master page so according that session you will display menu. Or you can check role session on each page and can through 403 error code. – Codelord May 01 '15 at 09:47
  • @Codelord what is a master page do you mean the `home.php` that I have in my code? – aurora May 01 '15 at 09:49
  • Home or page where you have menu and header and footer – Codelord May 01 '15 at 09:50

2 Answers2

3

A. Method 1

You can use the header() function to send a new HTTP header, but this must be sent to the browser before any HTML or text (so before the declaration, for example).

header('Location: '.$newURL);

B. Method 2

die()

header("Location: http://example.com/myOtherPage.php");

die();

C. Method 3

This function doesn't incorporate the 303 status code:

function Redirect($url, $permanent = false)
{
    header('Location: ' . $url, true, $permanent ? 301 : 302);

    exit();
}

Redirect('http://example.com/', false);

This is more flexible:

function redirect($url, $statusCode = 303)
{
   header('Location: ' . $url, true, $statusCode);
   die();
}

For More Information please refer : How to make a redirect in PHP?

Community
  • 1
  • 1
Mahesh Shukla
  • 186
  • 10
0

put die(); after header("location:../../statistics/home.php"); and check session in home.php

Mohit maru
  • 817
  • 8
  • 15