Jump to content

Got stuck when using if else statement for user level


shebbycs

Recommended Posts

DATABASE-id(auto increment),username,password and userlevel(int that is 1 for admin and 0 for user)

 

Login.php

 

<?php
    session_start();
    mysql_connect("localhost","root") or die(mysql_error());
    mysql_select_db("cute") or die(mysql_error());
    //session_start();
    $username = $_POST['username'];
    $password = $_POST['pass'];
    if (isset($_POST["submit"]))
    {
     $log = "SELECT * FROM regis WHERE username = '$username'";
     $login = mysql_query($log);
     $number = mysql_num_rows($login);

     if ($number == 0)
     {
      print "That user does not exist in our database. <a href=registration.php><input type='button' value='Register'></a>";
     }
      if ($number > 0)
     {
      $row = mysql_fetch_assoc($log);
      $_SESSION['username'] = $row['username'];
      $_SESSION['userlevel'] = $row['userlevel'];
      $_SESSION['is_logged_in'] = 1;
        if($_SESSION['userlevel']=0)
     {
       echo "<meta http-equiv='refresh' content='0; url=registration.php'>";
     }
     else if($_SESSION['userlevel']=1)
     {
       echo "<meta http-equiv='refresh' content='0; url=form2.php'>";
     }
     }



    }
    else
    {


?>
<html>
<head>
<script type="text/javascript">
function a()
{
   var x = document.login.username.value;
   var y = document.login.pass.value;

   if(x==""&& y=="")
   {
    alert("Please insert all message!");
    return false;
   }
   if(x=="")
   {
     alert("Please insert an username!");
     return false;
   }
   if(y=="")
   {
     alert("Please insert an password!");
     return false;
   }
}
</script>
</head>



<body>
<table border="0">
<form name="login" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>" onsubmit="return a()">
<tr><td colspan=2><h1>Login</h1></td></tr>
<tr><td>Username:</td>
<td><input type="text" name="username" maxlength="40"></td></tr>
<tr><td>Password:</td>
<td><input type="password" name="pass" maxlength="50"></td></tr>
<tr><td><input type="submit" name="submit" value="Register"></a></td>
<td><input type="submit" name="submit" value="Login"></td></tr>
</form>
</body>
<?php

  }



?> </html>



 

 

My problem is whether im login as admin or user the page only redirect to form2.php but will not detect the registration.php which is part am i missing can you recorrect it sir :)

  if($_SESSION['userlevel']=0)

    {

      echo "<meta http-equiv='refresh' content='0; url=registration.php'>";

    }

    else if($_SESSION['userlevel']=1)

 

needs to be..

 

  if($_SESSION['userlevel']==0)

    {

      echo "<meta http-equiv='refresh' content='0; url=registration.php'>";

    }

    else if($_SESSION['userlevel']==1)

<?php
    session_start();
    mysql_connect("localhost","root") or die(mysql_error());
    mysql_select_db("cute") or die(mysql_error());
    $username = $_POST['username'];
    $password = $_POST['pass'];
    if (isset($_POST["submit"]))
    {
     $log = "SELECT * FROM regis WHERE username = '$username'";
     $login = mysql_query($log);
     $number = mysql_num_rows($login);

     if ($number == 0)
     {
      print "That user does not exist in our database. <a href=registration.php><input type='button' value='Register'></a>";
     }
      if ($number > 0)
     {
      $row = mysql_fetch_assoc($log);
      $_SESSION['username'] = $row['username'];
      $_SESSION['userlevel'] = $row['userlevel'];
      $_SESSION['is_logged_in'] == 1;
       if($_SESSION['userlevel']==1)
     {
        echo "<meta http-equiv='refresh' content='0; url=form2.php'>" ;

     }
     else if($_SESSION['userlevel']==0)
     {
      echo "<meta http-equiv='refresh' content='0; url=registration.php'>";
     }
     }



    }
    else
    {


?>
<html>
<head>
<script type="text/javascript">
function a()
{
   var x = document.login.username.value;
   var y = document.login.pass.value;

   if(x==""&& y=="")
   {
    alert("Please insert all message!");
    return false;
   }
   if(x=="")
   {
     alert("Please insert an username!");
     return false;
   }
   if(y=="")
   {
     alert("Please insert an password!");
     return false;
   }
}
</script>
</head>



<body>
<table border="0">
<form name="login" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>" onsubmit="return a()">
<tr><td colspan=2><h1>Login</h1></td></tr>
<tr><td>Username:</td>
<td><input type="text" name="username" maxlength="40"></td></tr>
<tr><td>Password:</td>
<td><input type="password" name="pass" maxlength="50"></td></tr>
<tr><td><input type="submit" name="submit" value="Register"></a></td>
<td><input type="submit" name="submit" value="Login"></td></tr>
</form>
</body>
<?php

  }



?> </html>


 

I had redo this one but still whenever im login its will come to registration.php and the admin user level cannot redirect to the form2.php any correction on that?

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.