Jump to content

Users Level Login System Leads to Blank Page


lybat

Recommended Posts

I'm creating a users level login system in php & mysql. However, it leads to a white blank page. I can't figure the error. Any help is much appreciated. Thanks.

 

Form.html

 

 

<!DOCTYPE html>
<!--[if lt IE 7]> <html class="lt-ie9 lt-ie8 lt-ie7" lang="en"> <![endif]-->
<!--[if IE 7]> <html class="lt-ie9 lt-ie8" lang="en"> <![endif]-->
<!--[if IE 8]> <html class="lt-ie9" lang="en"> <![endif]-->
<!--[if gt IE 8]><!--> <html lang="en"> <!--<![endif]-->
<head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <title>MIS Electronic Request</title>
  <link rel="stylesheet" href="css/style.css">
 
</head>
<body>
  <br>
 
  <form method="post" action="main.php" class="login">
    <p>
      <label for="login">Username:</label>
      <input type="text" name="myusername" id="myusername" value="Employee id">
    </p>

    <p>
      <label for="password">Password:</label>
      <input type="password" name="mypassword" id="mypassword" value="4815162342">
    </p>

    <p class="login-submit">
      <button type="submit" class="login-button">Login</button>
    </p>

  </form>

</body>
</html>
 

 

Main.php

 

 

<?php
session_start();
$host="localhost";
$username="root";
$password="******";
$db_name="htdocs";
$tbl_name="users";

mysql_connect("$host", "$username", "$password")or die("Unable to connect to MySQL: " . mysql_error());
mysql_select_db("$db_name")or die("Unable to select database: " . mysql_error());

if (isset($_POST['submit']))
{
// username and password sent from form
$myusername=$_POST['myusername'];
$mypassword=$_POST['mypassword'];

$myusername = stripslashes($myusername);
$mypassword = stripslashes($mypassword);
$myusername = mysql_real_escape_string($myusername);
$mypassword = mysql_real_escape_string($mypassword);

$sql="SELECT * FROM $tbl_name WHERE username='$myusername' and password='$mypassword'";
$result=mysql_query($sql);

// Mysql_num_row is counting table row
$count=mysql_num_rows($result);


if($count==1){
   if (user_level == 1) {
        $_SESSION['myusername'] = $myusername;
        $_SESSION['mypassword'] = $mypassword;
        header("Location:Admin_home.php");
        }
   else if (user_level == 2) {
        $_SESSION['myusername'] = $myusername;
        $_SESSION['mypassword'] = $mypassword;
        header("Location:home.php");
   }
}
else {
echo "Wrong Username or Password";
}
}

?>

 

Thanks for the reply. The blank page issue has been resolved. But this time i got this error:

 

Error:

 

Notice: Use of undefined constant user_level - assumed 'user_level' in C:\wamp\www\htdocs\main.php on line 34

Notice: Use of undefined constant user_level - assumed 'user_level' in C:\wamp\www\htdocs\main.php on line 39

Hi i already did that. still error..

 

 

if($count==1){
   if ($user_level == 1) {
        $_SESSION['myusername'] = $myusername;
        $_SESSION['mypassword'] = $mypassword;
        header("Location:Admin_home.php");
        }
   else if ($user_level == 2) {
        $_SESSION['myusername'] = $myusername;
        $_SESSION['mypassword'] = $mypassword;
        header("Location:home.php");
   }
}

and you still should have a space after Location:

header("Location:Admin_home.php");
 header("Location:home.php");

but you don't need the space in else if you can use elseif

 

You don't need the space after "Location:", if you had tried it, you would see it works without the space.

the error message probably did change to an undefined variable message.

 

the php variable $user_level is not defined anywhere in the posted code. where do you expect it to get its value from and where is your code setting it to that value?

no, it's not correct.

 

i recommend that you read a php/mysql book or tutorial as you are currently not aware of even the syntax for a php variable and you will also need to know how to fetch a row from the result set that a query returns.

You don't need the space after "Location:", if you had tried it, you would see it works without the space.

You are right. The space is is ignored and multiple spaces after the colon should work too (according to the RFC). I thought I had an issue a few moths ago that I solved by adding a space after the colon in a header, but I must be remembering it incorrectly. I stand corrected. Sorry.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.