0

I have create 6 php files which are teacher.php, student.php, login.php, logout.php, index.php, and config.php

I have done some coding and it shows out the:

username:
password

I could input my account name and password that i have registered in database.

but after I input my account name and password or wrong password or input nothing. Just press enter

it shows blank page that show nothing.

what's wrong with it, is that i miss some coding or any error?

Sorry for that because I am new on php and hope you to give me some suggestion :)

As i think the main problem is in login.php and these is the code:

<?php
include "config.php";
if(isset($_POST['log'])){   
    $user = $_POST['user'];
    $pass = md5($_POST['pass']);
    $hsl = mysql_query("select * from login where user='$user' and pass='$pass'");
    $data = mysql_fetch_array($hsl);
    $username = $data['user'];
    $password = $data['pass'];
    $type = $data['type'];
    $name = $data['name'];
    if($user==$username && $pass=$password){
            session_start();
            $_SESSION['name']=$name;
            if($type=='student'){
                echo "<script>location.assign('student.php')</script>";
            }else if($type=='teacher'){
                echo "<script>location.assign('teacher.php')</script>";
            } else{
                echo "<script>location.assign('wrong page.php')</script>";
            }
    }
}
?>
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252
tommy
  • 59
  • 5

1 Answers1

0

Note:-

mysql_* was deprecated in PHP 5.5.0, and it was removed in PHP 7.0.0. Instead, the MySQLi or PDO_MySQL extension should be used.

Try this code:-

// add this line always first
session_start();
include "config.php";
if(isset($_POST['log'])){   
    $user = $_POST['user'];
    $pass = md5($_POST['pass']);
    $hsl = mysql_query("select user,type,name from login where user='$user' and pass='$pass'");

    if (!$hsl) {
        // Debug query result by below code
        //echo 'Could not run query: ' . mysql_error();
        //exit;
        echo '<script language="javascript">';
        echo 'alert("Username / Password Seems Wrong !")';
        echo '</script>';
    }else{
      $row = mysql_fetch_row($hsl);
      $username = $row[0];
      $type =  $row[1];
      $name =  $row[2];
      $_SESSION['name'] = $name;
         if($type=='student'){
                echo "<script>location.assign('student.php')</script>";
            }else if($type=='teacher'){
                echo "<script>location.assign('teacher.php')</script>";
            } else{
                echo "<script>location.assign('wrong page.php')</script>";
            }
    } 

if you want to redirect to file then change your condition as below:-

if($type=='student'){
                //echo "<script>location.assign('student.php')</script>";
                header("Location: student.php");
            }else if($type=='teacher'){
                //echo "<script>location.assign('teacher.php')</script>";
                header("Location: teacher.php");
            } else{
                //echo "<script>location.assign('wrong page.php')</script>";
                header("Location: wrong page.php");
            }
    }

Hope it will help you :)

Ravi Hirani
  • 6,511
  • 1
  • 27
  • 42
  • thank you for your suggestions! i have tried and it let me to load into a http://localhost:8080/FYP/student.php ,but after I input a teacher account and password into the column, it still login back to http://localhost:8080/FYP/student.php but not http://localhost:8080/FYP/teacher.php – tommy Jan 29 '16 at 09:06
  • Please check my updated answer. – Ravi Hirani Jan 29 '16 at 09:17