Log in

View Full Version : need urgent help in creatin a login page



sukanya.paul
03-23-2007, 07:19 AM
hi
i am fairly new to php n mysql.i hv been asked to do a login page using sessions.i hv tried but cant find the correct code and cant understand sessions.i hv to do this by monday.
i hv a table called user which has 3 fields id(primary),username and password.

my form shud authenticate the user and if the username and password is correct den it shuld be directed to a pae called admintasks.php.

i will be really greatful is some1 can help me with this,,
thanks in advance
suk
p.s:i kno twey has worked on this but somehow i cant follow his code..simply usin his code is not workin...and i cant understand wht changes to make..
plsssssssssssss help

thetestingsite
03-24-2007, 04:41 AM
Try this (not tested but should work):



<?php
session_start();

####### Configuration ########
$DBserver = "localhost"; //server for Database
$DBuser = "USERNAME"; //user for MySQL Database
$DBpass = "PASSWORD"; //password for above mentioned username
$DB = "DATABASE"; //database the tables are located in

$usersTable = "user"; //Table the user information is in

$redirect = "admintasks.php"; //page to redirect to on successful login
####### End Config ########

$conn = mysql_connect($DBserver, $DBuser, $DBpass);
mysql_select_db($DB, $conn);

if (@$_POST['act'] == "do_login") {

$username = $_POST['username'];
$password = $_POST['password'];

$userInfo = mysql_query("SELECT * FROM `$usersTable` WHERE `username`= '$username'");

if (mysql_num_rows($userInfo) < 1) {
echo 'There are no users with that username in the database. Please go back and enter a valid username/password combination.';
}

else {
$q = mysql_fetch_array($userInfo);

if ($q['password'] != $password) {
echo 'The password is invalid. Please go back and enter a valid username/password combination.';
}

else {
$_SESSION['id'] = $q['id'];
$_SESSION['username'] = $q['username'];
$_SESSION['password'] = $q['password'];

header('Location: '.$redirect);
}
}
}

else {
?>

<html>
<head>
<title>Login</title>
</head>
<body>

<form action="<?php echo $_SERVER['PHP_SELF'];?>" method="POST">
<input type="hidden" name="act" value="do_login">

Username: <input type="text" name="username" value=""> <br>
Password: <input type="password" name="password" value=""> <br>
<input type="submit" value="Login">
</form>
</body>
</html>

<?php
}
?>


Edit as you see fit.
Hope this helps.

sukanya.paul
03-26-2007, 01:20 AM
thanks..the code is perfectly workin... :)