i get username and password from user in login.php to compare with mysql db username and password.here 2 type of user are admin,user,if login a admin go to uregister.php then login the user go to userlink.php

in the databse 3 fields username,password and usertype.

<html>
<head>
<head>
<body>
<form method=post action="login.php">
<table border=1>
<tr>
<td>
UserName</td><td><input type="text" name="name">

</td>
</tr>
<tr>
<td>
Password</td><td><input type="password" name="apass">
</td>
</tr>
<tr>
<td>
</td><td><input type="Submit" name="check" Value="Submit">
</td>
</tr>
</table>
</form>
</body>
</html>

<?php
$name=$_REQUEST['name'];
$pass2=$_REQUEST['apass'];
echo $name;
echo $pass2;
$no=0;
$no1=1;

$host="localhost";
$user="";
$pass="";
$dbid="upload";

$link=mysql_connect($host,$user,$pass);
mysql_select_db($dbid);
$result=mysql_query("select * from login");

while($row=mysql_fetch_array($result))
{
$username=$row['username'];
$pass1=$row['password'];
$type=$row['usertype'];

}

if($username='$name' && $pass1='$pass2' && $type='$no')
{
print "<script>";
print "window.location='http://www.cdsemployment.com/fileload/uregister.php'";
print "</script>";
}

if($username='$name' && $pass1='$pass2' && $type=$no1)
{
print "<script>";
print "window.location='http://www.cdsemployment.com/fileload/userlink.php'";
print "</script>";

}

mysql_close($link);

?>

Recommended Answers

All 18 Replies

send your database....

  1. Since you want to compare the values in 2 variables, you should use comparison operator. ie., if($a == $b) and not if($a = $b). Thats 1 change.
  2. You shouldn't put single quotes around a variable. If you do, php parser will consider it as a string and not compare the values.
  3. Since the user enters username and password, you can modify your query to have a where clause.

ie.,

$query = "select * from table where username='$username' and password='$password'";
$result = mysql_query($query);
if(mysql_num_rows($result) > 0 ) {
//valid user
//proceed with the next step
} else {
//invalid user
} 
  1. You are here for a long time (since Dec 2007). Read guidelines and Use code-tags.

P.S. From next time, mention your problem in detail. :)

send your database....

down the following field in my db...........

username varchar(40)
password varchar(35)
usertype int(1) -------->there are 2 type of user a admin and 10 users

  1. Since you want to compare the values in 2 variables, you should use comparison operator. ie., if($a == $b) and not if($a = $b). Thats 1 change.
  2. You shouldn't put single quotes around a variable. If you do, php parser will consider it as a string and not compare the values.
  3. Since the user enters username and password, you can modify your query to have a where clause.
    ie.,
$query = "select * from table where username='$username' and password='$password'";
$result = mysql_query($query);
if(mysql_num_rows($result) > 0 ) {
//valid user
//proceed with the next step
} else {
//invalid user
} 
  1. You are here for a long time (since Dec 2007). Read guidelines and Use code tags.

hi nav33..
thank you for reply....

i have one more doubt ,my db fields 3 username, password and usertype field , here there are two type of users admin and users. if admin login it will go to admin page and user login it will go to userpage. so i mention admin=0 and users=1 in db .
Next username,password and usertype equal only to authentications page ... Very fast today i want finish this work..

hi satish,
i didn`t understand ur requirement. are you getting errors if yes then post what are they?

commented: I am glad you are learning something from me. Cheers! +8

In your code, there is no need of while loop..
because only one user can enter at a time...

and do like this:
write select query like:

$result=mysql_query("select * from user where username='".$name."' and password='".$pass2."'");
$row=mysql_fetch_array($result);
if($row['usertype']==1)
{
print "window.location='http://www.cdsemployment.com/fileload/uregister.php'";
}
if($row['usertype']==1)
{
print "window.location='http://www.cdsemployment.com/fileload/userlink.php'";
}

try like this and notify if error occurs...

hi ..

When i click the login.php it is automatical redirecting uregister.php and userlink.php
i was not entered username,password usertype are admin =0 and users=1

$result=mysql_query("select * from login where username='$name' and password='$pass2'");

$row=mysql_fetch_array($result);


if($row['usertype']==0)
{
print "<script>";
print "window.location='http://www.cdsemployment.com/fileload/uregister.php'";
print "</script>";
}
if($row['usertype']==1)
{
print "<script>";
print "window.location='http://www.cdsemployment.com/fileload/userlink.php'";
print "</script>";
}

put the above code in this condition:

if($_SERVER['REQUEST_METHOD']=='POST') 
{

$result=mysql_query("select * from login where username='$name' and password='$pass2'");

$row=mysql_fetch_array($result);


if($row['usertype']==0)
{
print "<script>";
print "window.location='http://www.cdsemployment.com/fileload/uregister.php'";
print "</script>";
}
if($row['usertype']==1)
{
print "<script>";
print "window.location='http://www.cdsemployment.com/fileload/userlink.php'";
print "</script>";
}

}

hi satish,
i worked on ur code here are some modifications i have done hope it will help u

<html>
<head>
<head>
<body>
<form method="post" action="">
<table border=1>
<tr>
<td>
UserName</td><td><input type="text" name="name">

</td>
</tr>
<tr>
<td>
Password</td><td><input type="password" name="apass">
</td>
</tr>
<tr>
<td>
</td><td><input type="Submit" name="check" Value="Submit">
</td>
</tr>
</table>
</form>
</body>
</html>

<?php

$host="localhost";
$user="root";
$pass="1234";
$dbid="upload";

$link=mysql_connect($host,$user,$pass);
mysql_select_db($dbid);
if($_SERVER['REQUEST_METHOD']=="POST"){
$qer="select * from login  where username='".$_POST['name']."' and password='".$_POST['apass']."'";
	$res=mysql_query($qer)or die(mysql_error());
$num=mysql_num_rows($res);
	 if($num==1 && $res['usertype']==0)
{
echo'<script language="javascript">window.location.href="welcome.php";</script>';	
}
else{
echo'<script language="javascript">window.location.href="unregister.php";</script>';	
}

if($num==1 && $res['usertype']==1)
{
echo'<script language="javascript">window.location.href="welcome.php";</script>';	
}
else{
echo'<script language="javascript">window.location.href="unlink.php";</script>';	
}
}


mysql_close($link);

?>

hii..

if a admin login go to uregister.php page
if users login go to userlink.php but following code every one go to the uregister.php page only so i nead above the following condition clear my doubt..

if(isset($_REQUEST['check']))

{
if($_SERVER['REQUEST_METHOD']=="POST"){

$qer="select * from login where username='".$_POST['name']."' and password='".$_POST['apass']."'";

$res=mysql_query($qer)or die(mysql_error());

$num=mysql_num_rows($res);

if($num==1 && $res['usertype']==0)

{

echo'<script language="javascript">window.location.href="uregister.php";</script>';

}

else{

echo'<script language="javascript">window.location.href="login.php";</script>';

}

if($num==1 && $res['usertype']==1)
{
echo'<script language="javascript">window.location.href="userlink.php";</script>';

}

else{
echo'<script language="javascript">window.location.href="login.php";</script>';

}

}
}

hii..

if a admin login go to uregister.php page
if users login go to userlink.php but following code every one go to the uregister.php page only so i nead above the following condition clear my doubt..

if(isset($_REQUEST['check']))

{
if($_SERVER['REQUEST_METHOD']=="POST"){

$qer="select * from login where username='".$_POST['name']."' and password='".$_POST['apass']."'";

$res=mysql_query($qer)or die(mysql_error());

$num=mysql_num_rows($res);

if($num==1 && $res['usertype']==0)

{

echo'<script language="javascript">window.location.href="uregister.php";</script>';

}

else{

echo'<script language="javascript">window.location.href="login.php";</script>';

}

if($num==1 && $res['usertype']==1)
{
echo'<script language="javascript">window.location.href="userlink.php";</script>';

}

else{
echo'<script language="javascript">window.location.href="login.php";</script>';

}

}



}
commented: use [code] tags -2

:-/ Why are you posting the same post thrice when you could have edited it ?


P.S. I don't see mysql_connect and mysql_select_db. It would really help if you post your complete code.

hii..

if a admin login go to uregister.php page
if users login go to userlink.php but following code every one go to the uregister.php page only so i nead above the following condition clear my doubt..


if(isset($_REQUEST))

{
if($_SERVER=="POST"){

$qer="select * from login where username='".$_POST."' and password='".$_POST."'";

$res=mysql_query($qer)or die(mysql_error());

$num=mysql_num_rows($res);

if($num==1 && $res==0)

{

echo'<script language="javascript">window.location.href="uregister.php";</script>';

}

else{

echo'<script language="javascript">window.location.href="login.php";</script>';

}

if($num==1 && $res==1)
{
echo'<script language="javascript">window.location.href="userlink.php";</script>';

}

else{
echo'<script language="javascript">window.location.href="login.php";</script>';

}

}

}

i think your if conditions are not correct:

if( $res['usertype']==0)

{

echo'<script language="javascript">window.location.href="uregister.php";</script>';

}

if( $res['usertype']==1)
{
echo'<script language="javascript">window.location.href="userlink.php";</script>';

}
else
{
echo'<script language="javascript">window.location.href="login.php";</script>';
}
<?php
if(isset($_POST['check'])) {
	$host="localhost";
	$user="";
	$pass="";
	$dbid="upload";
	$link=mysql_connect($host,$user,$pass);
	mysql_select_db($dbid);
	$name = mysql_real_escape_string($_POST['name']);
	$password = mysql_real_escape_string($_POST['apass']);
	$result=mysql_query("select * from login where username='$name' and password='$password'");
	if(mysql_num_rows($result) > 0 ) {
		while($row=mysql_fetch_array($result))
		{
			$type=$row['usertype'];
		}
	}
	if($type==0) {
		header("location: uregister.php");
	} else {
		header("location: userlink.php");
	}
	mysql_close($link);
}
?>
<html>
<head>
<head>
<body>
<form method="post" action="login.php">
<table border=1>
<tr>
<td>
UserName</td><td><input type="text" name="name">

</td>
</tr>
<tr>
<td>
Password</td><td><input type="password" name="apass">
</td>
</tr>
<tr>
<td>
</td><td><input type="Submit" name="check" Value="Submit">
</td>
</tr>
</table>
</form>
</body>
</html>

This should work.

commented: i am learning from u.thank u +1

why are you placing the same post again and again...
do your application in different ways according to the posts mentioned above...and notify the errors...
dont place same post again....

this errors is coming
Cannot modify header information - headers already sent by (output started at /home1/cdsemplo/public_html/fileload/login.php:28) in /home1/cdsemplo/public_html/fileload/login.php on line 62

Next i used below if condition not working properly....

print "<script>";
print "window.location='http://www.cdsemployment.com/fileload/fileload.php'";
print "</script>";

$name = mysql_real_escape_string($_POST['name']);

$password = mysql_real_escape_string($_POST['apass']);

$result=mysql_query("select * from login where username='$name' and password='$password'");

if(mysql_num_rows($result) > 0 ) {

while($row=mysql_fetch_array($result))

{

$type=$row['usertype'];

}
}

if($type==0) {

header("location: uregister.php");

} else {

header("location: userlink.php");

}


mysql_close($link);

use ob_start(); funtion as first line of your page like:

<?
ob_start();
?>

psathish2 here are quotation of all rules you carelessly breaking. If you continue with same attitude you will soon find your self banned from this forum by your own ignorance!

For easy readability, always wrap programming code within posts in (code) (code blocks) and (icode) (inline code) tags.

Do not flood the forum by posting the same question more than once (ie in multiple forums).

Use (bbcode) only when it is necessary to the comprehension of your post. Avoid using an excessive amount of (bbcode) to alter font styles or to draw more attention to your post.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.