[SOLVED] - Redirecting users to different pages in LOGIN

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
Bbob
Forum Commoner
Posts: 57
Joined: Sat Aug 07, 2010 4:46 am

[SOLVED] - Redirecting users to different pages in LOGIN

Post by Bbob »

Hi

Im having trouble try to figuring out on how to redirect two different types of users to two different pages in a single login form.


Basically what I want is if a regular user logs in, he will be directed to user_mainpage.php but if the website administrator logs in, he will be directed to admin_mainpage.php. Both of them will use the same login form. Is this possible? If so, can you please teach me how to code this.


Thanks in advance.
Last edited by Bbob on Sun Aug 08, 2010 1:10 am, edited 1 time in total.
pbs
Forum Contributor
Posts: 230
Joined: Fri Nov 07, 2008 5:31 am
Location: Nashik, India
Contact:

Re: Please help - redirecting users to different pages in LO

Post by pbs »

Is there any differentiating factor between this 2 users (admin and user). According to that you can redirect the logging user.
Bbob
Forum Commoner
Posts: 57
Joined: Sat Aug 07, 2010 4:46 am

Re: Please help - redirecting users to different pages in LO

Post by Bbob »

Hi, thanks for the reply


Sorry I forgot to add that.

To differentiate an admin from the user, I added a column in the database table named Type. If the type = admin, then person logging in is an administrator. If the type = user, then the person is logging in as a user.

I still couldnt picture how to do that even if I have the type to differentiate the 2 users. All I can think of is to make a PHP if statement then somehow filter by an SQL statement, but I dont think thats possible.
manojsemwal1
Forum Contributor
Posts: 217
Joined: Mon Jun 29, 2009 4:13 am
Location: India

Re: Please help - redirecting users to different pages in LO

Post by manojsemwal1 »

add one more column in sql usertype and put the status of Admin= A and user =u
in log in page when admin login with his username and paasword that time fetch the value form login table againt user name and paasword like

select Username,password,usertype from logintb where username='' and password='';

if the value will be match it will return 1 else 0
then use if statement
if(usertype=='A')
{
header("Admin.php");
}
else
{
header("client.php");
}

Thanks.
Bbob
Forum Commoner
Posts: 57
Joined: Sat Aug 07, 2010 4:46 am

Re: Please help - redirecting users to different pages in LO

Post by Bbob »

Cool!

Thanks it worked!

Thanks guys, for the reply especially to manoj for providing the solution.

============================================================

UPDATE: It worked kinda. Sorry for the false alarm

The code provided worked but the else statement seems to be the only one working.

Here's my code maybe you guys can find out whats wrong:

Code: Select all

$sql="SELECT * FROM $tbl_name WHERE UserName='$username' AND Password='$password'";
$result=mysql_query($sql);

$count=mysql_num_rows($result);

if($count==1)
{
if (type=='u')
	{
		$_SESSION['username'] = $username;

		header("location:user_myaccount.php");
	}
	
else
	{	
		$_SESSION['username'] = $username;

		header("location:admin_main.php");
	}
}
Bbob
Forum Commoner
Posts: 57
Joined: Sat Aug 07, 2010 4:46 am

Re: Please help - redirecting users to different pages in LO

Post by Bbob »

Hi

I manage to solve this puzzle

Here's the code

Code: Select all

if ($count == 1)
{
	$_SESSION['dbusername'] = dbusername;
	
	while ($row = mysql_fetch_assoc($result))
	{
		$usertype = $row['type'];
		
		if ($usertype==user)
		{
			header("location:user_myaccount.php");
		}
		
		else
		{
			header("location:admin_main.php");
		}
	}
}
Post Reply