...

View Full Version : Login



Kineas
11-01-2009, 02:11 PM
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

<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

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

GoodBanners.com
11-02-2009, 10:26 AM
maybe try using switch statement?



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

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

default:
echo "Please login";
break;
}

bucket
11-02-2009, 03:00 PM
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



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum