Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3

Thread: Login

  1. #1
    New Coder
    Join Date
    Oct 2009
    Location
    Walsall, UK
    Posts
    38
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Login

    I currently have login code working, you type your user name and password and then it searches the mysql database for the account info, and then says whether the loginw as successful or not. This is what I've got so far:

    login.html
    Code:
    <TABLE><TR><TD><FONT face="Arial">
    Username</FONT>
    </TD>
    <TD><FONT face="Arial">
    Password</FONT>
    </TD>
    </TR>
    <TR>
    <TD>
    <form name="form1" method="post" action="checklogin.php">
    <input name="myusername" type="text" id="myusername">
    </TD>
    <TD>
    <input name="mypassword" type="password" id="mypassword">
    <BR>
    </TD>
    <TD>
    <input type="submit" name="Submit" value="Login">
    </form>
    </TD></TR>
    <TR><TD>
    <A href="register.html"><FONT face="Arial">Register</FONT></A>
    </TR></TD>
    </TABLE>
    check_login.php
    Code:
    <?php
    $host="localhost";
    $username="";
    $password="";
    $db_name="";
    $tbl_name="members";
    
    mysql_connect("$host", "$username", "$password")or die("cannot connect");
    mysql_select_db("$db_name")or die("cannot select DB");
    
    $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);
    
    $count=mysql_num_rows($result);
    
    if($count==1){
    
    session_register("myusername");
    session_register("mypassword");
    header("location:login_success.php");
    }
    else {
    echo "Wrong Username or Password";
    }
    ?>
    And I have a final page that just says "Login Successful", as the user is only redirected there if it was successful. At the moment, I want it to write to a cookie if it was successful, but also writing what type of user is logged in, admin or normal user.

    Additionally, the login form is on every page, so I don't want it to display if the user is logged in. But instead it would display "You are logged in as [username]".

    For the first bit, I know I'd need to start a session on each page, and when logging in, for it to be login=0 if username = admin, login=2 else login=1 and be written in the cookie.

    And for whether the login box shows, I don't know the best way to write the IF statement, but maybe IF login = 0 display login box else echo you are logged in as [username].


    Any help with these would be great, thanks.

  • #2
    New Coder
    Join Date
    Nov 2009
    Location
    Australia
    Posts
    78
    Thanks
    0
    Thanked 7 Times in 7 Posts
    maybe try using switch statement?

    PHP Code:
    switch($login){
     case 
    "0":
       echo 
    "hello admin";
     break;

     case 
    "1":
      echo 
    "hello user 1";
     break;

     default:
      echo 
    "Please login";
     break;


  • #3
    Regular Coder
    Join Date
    Sep 2008
    Posts
    716
    Thanks
    8
    Thanked 17 Times in 16 Posts
    You will need to add another column to your table that will check that column row to see if the rank is lets say 1 for admin 2 for regular


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •