[SOLVED] PHP Login Page with MySQL Auth

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Janco
Forum Commoner
Posts: 37
Joined: Fri Apr 25, 2008 2:51 am

[SOLVED] PHP Login Page with MySQL Auth

Post by Janco »

Hi all,

Hope someone can help.

I want to write a login page that will redirect users to specific pages.

I habe 1 MySQL DB with 2 tables containing users, the idea is that if users in table1 log in they should be redirected to index1 and if users from table2 log in they have to be redirected to index2 and then a special login for the Admin, not in any DB or table due to security - I haven't figured that one out yet.

I've written a normal login page and it works.

I have done something like:
put each table in a while loop to get name and password and set them as $lname1, lpass1, $lname2 and $lpass2

then check the posted name from the Login page:
$name=$_POST['name']
$pass=$_POST['pass']

which I then match to see which page to call i.e.

if ($name == $lname1 && $pass == $lpass1) {
$_SESSION['basic_is_logged_in'] = true;
mysql_query("insert into table1(last_logged_in) values(SYSDATE());
header(location: index1.php);
} elseif {
$_SESSION['basic_is_logged_in'] = true;
mysql_query("insert into table2(last_logged_in) values(SYSDATE()");
header(location: index2.php);
} else {
echo "Incorrect Username or Password";
}

But in my original Login Page I used a mysql_num_rows() to do the authentication but I don't know how or what to implement to make this work.

Can someone please give me some pointers on how to make this work?
Last edited by Janco on Tue May 27, 2008 1:03 am, edited 1 time in total.
Janco
Forum Commoner
Posts: 37
Joined: Fri Apr 25, 2008 2:51 am

Re: PHP Login Page with MySQL Auth - Redirecting users to their

Post by Janco »

Figured it out:
<?php
session_start();

//Set Login Variables
if (isset($_POST['login']) && isset($_POST['password'])) {
$lname = $_POST['login'];
$lpass = $_POST['password'];
} else {
exit;}
?>
<?
//Open connection to DB for User authentication
mysql_connect("localhost","root","");
mysql_select_db("login");

$query1="select name,password from table1 where name = '$lname' and password = '$lpass'";
$result1=mysql_query($query1);

$query2="select name,password from table2 where name = '$lname' and password = '$lpass'";
$result2=mysql_query($query2);
echo $query2;
if (mysql_num_rows($result1) > 0) {
$_SESSION['basic_is_logged_in'] = true;
mysql_query("update table1 set last_login=SYSDATE() where name='$lname'") or die (mysql_error());
header('Location: main1.php');
} elseif (mysql_num_rows($result2) > 0) {
$_SESSION['basic_is_logged_in'] = true;
mysql_query("update table2 set last_login=SYSDATE() where name='$lname'") or die (mysql_error());
header('Location: main2.php');
}
else {
echo "<center><h4><font color=\"red\">Incorrect Username or Password</font></h4></center>";
}

Now I have to figure out how to create a section for the Admin
Post Reply