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 :)

Link to comment
Share on other sites

  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)

Link to comment
Share on other sites

<?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?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.