I'm getting an error as i'm trying to create a login page which will allow users to be directed to the right page. e.g a student will be directed to the student page and the staff will be directed to the staff page but i don't know whats wrong with my code..help

<?php
ob_start();
$host="localhost"; // Host name
$username="amy"; // Mysql username
$password="annie"; // Mysql password
$db_name="pa"; // Database name
$tbl_name="user"; // Table name
 // Get info from the session

// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");

// Define $myusername and $mypassword and $myusertype
$myusername=$_POST['myusername'];
$mypassword=$_POST['mypassword'];
$myusertype=$_POST['myusertype'];


//$sql="SELECT * FROM $tbl_name WHERE username='$myusername' and //password='$mypassword'";
//$result=mysql_query($sql);


$sql=("SELECT user_type FROM $tbl_name WHERE username='$myusername' and password='$mypassword' and user_type ='$myusertype'");

//$result2=mysql_query($sql);


// Mysql_num_row is counting table row
$count=mysql_num_rows($result2);
// If result matched $myusername and $mypassword, table row must be 1 row

if($count==1 ){
Register $myusername, $mypassword and redirect to file "login_success.php"
session_register("myusername");
session_register("mypassword");
session_register("myusertype");
header("location:login_success.php");
}
else {
echo "Wrong Username or Password";
}

//ob_end_flush();
?>

this is the error message
Parse error: syntax error, unexpected T_VARIABLE in C:\xampp\htdocs\capat\checklogin.php on line 34

line 34. I think thats a comment missing "//"

yes thats the problem. thank you but another problem im still having is dircting the student to the student page, the staff to the staff page and the admin to the admin page. Could someone please help me on the code with how to achieve that please..thank you

How do you differentiate whether a user is an admin or a staff member or a student ?

I have different pages already for them and i put on my main login form for them to type in if they are a staff, student or admin.
This is my main_login.php and login_success.php

<BODY BGCOLOR="Teal">
<table width="900" border="20" align="center" cellpadding="3" cellspacing="6" bgcolor="BLACK">
<tr>
<form name="form1" method="post" action="checklogin.php">
<td>
<table width="100%" border="0" cellpadding="30" cellspacing="5" bgcolor="#FFFFFF">
<tr>
<td colspan="3"><strong>Member Login </strong></td>
</tr>
<tr>
<td width="10">Username</td>
<td width="10">:</td>
<td width="600"><input name="myusername" type="text" id="myusername"></td>
</tr>
<tr>
<td>Password</td>
<td>:</td>
<td><input name="mypassword" type="text" id="mypassword"></td>

<tr>
<td width="10">Usertype</td>
<td width="10">:</td>
<td width="600"><input name="myusertype" type="text" id="myusertype"></td>
</tr>

</tr>
<tr>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td><input type="submit" name="Submit" value="Login"></td>
</tr>
</BODY>
</table>
</td>
</form>
</tr>
</table>

login_success.php

<?
// Check if session is not registered , redirect back to main page.
// Put this code in first line of web page.
session_start();
if(!session_is_registered(myusername)){
header("location:http://www.yahoo.com");
}
else  {
header("location:admin/index.php");
}
?>

<html>
<body>
Login Successful
</body>
</html>

Well, then depending upon the input of the user, you can redirect him to relative page. According to you, the user enters the usertype in the textbox myusertype. When the user submits the page, get the value of myusertype.

$usertype=$_POST['myusertype'];
if($usertype=="student"){
 //redirect to student page
} else if ($usertype=="admin") {
 //redirect to admin page
} else if($usertype=="staff"){
  //redirect to staff page
} else { // none of these 3 users
 //redirect to error page
}

OK, so do i put this in my login_sucess.php page or my checklogin.php page and what line should i put it in

Where are you validating the login ? When the user enters his username and password, where are you checking if he's a valid user ?

In my database

No dude! when the user enters the username and password, how will you know if he's a valid user and not a dummy one ?

Because, i have in put the user type, username and password in my database already. So because this is just something that will work on my laptop i only have about 6 users. 2 students, 2 staffs and 2 admin. Is that what you are asking for?

:) umm.. No.. Can you post the code of check_login.php ?

ok
checklogin.php

<?php
ob_start();
$host="localhost"; // Host name
$username=""; // Mysql username
$password=""; // Mysql password
$db_name="pa"; // Database name
$tbl_name="user"; // Table name
 // Get info from the session

// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");

// Define $myusername and $mypassword and $myusertype
$myusername=$_POST['myusername'];
$mypassword=$_POST['mypassword'];
$myusertype=$_POST['myusertype'];


//$sql="SELECT * FROM $tbl_name WHERE username='$myusername' and //password='$mypassword'";
//$result=mysql_query($sql);


$sql=("SELECT user_type FROM $tbl_name WHERE username='$myusername' and password='$mypassword' and user_type ='$myusertype'");

//$result2=mysql_query($sql);


// Mysql_num_row is counting table row
//$count=mysql_num_rows($result2);
// If result matched $myusername and $mypassword, table row must be 1 row

if($count==1 ){
//Register $myusername, $mypassword and redirect to file "login_success.php"
session_register("myusername");
session_register("mypassword");
session_register("myusertype");
header("location:login_success.php");
}
else {
echo "Wrong Username or Password";
}

//ob_end_flush();
?>

You don't need a login_success.php page. Instead of redirecting to login_success.php page, redirect directly to admin/student/staff page ! This would be your new checklogin.php

<?php
ob_start();
$host="localhost"; // Host name
$username=""; // Mysql username
$password=""; // Mysql password
$db_name="pa"; // Database name
$tbl_name="user"; // Table name
 // Get info from the session

// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");

// Define $myusername and $mypassword and $myusertype
$myusername=$_POST['myusername'];
$mypassword=$_POST['mypassword'];
$myusertype=$_POST['myusertype'];


//$sql="SELECT * FROM $tbl_name WHERE username='$myusername' and //password='$mypassword'";
//$result=mysql_query($sql);


$sql=("SELECT user_type FROM $tbl_name WHERE username='$myusername' and password='$mypassword' and user_type ='$myusertype'");

//$result2=mysql_query($sql);


// Mysql_num_row is counting table row
//$count=mysql_num_rows($result2);
// If result matched $myusername and $mypassword, table row must be 1 row

if($count==1 ) {
//Register $myusername, $mypassword and redirect to file "login_success.php"
session_register("myusername");
session_register("mypassword");
session_register("myusertype");
  if($myusertype=="admin") {
   header("location: admin/index.php");
  } else if($myusertype=="staff"){
  header("location: staff/index.php");
  } else if($myusertype=="student"){
  header("location: student/index.php");  
} else {
 echo "Wrong usertype"; 
exit;  
}
}
else {
echo "Wrong Username or Password";
}

//ob_end_flush();
?>

Try this.

i have tried that but now it is just giving me wrong username and password for everything

Well, You have so many "important" lines commented ! wait.. I ll edit my previous post. And I don't think it was working earlier.

ok

<?php
ob_start();
$host="localhost"; // Host name
$username=""; // Mysql username
$password=""; // Mysql password
$db_name="pa"; // Database name
$tbl_name="user"; // Table name
 // Get info from the session

// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");

// Define $myusername and $mypassword and $myusertype
$myusername=$_POST['myusername'];
$mypassword=$_POST['mypassword'];
$myusertype=$_POST['myusertype'];


//$sql="SELECT * FROM $tbl_name WHERE username='$myusername' and //password='$mypassword'";
//$result=mysql_query($sql);


$sql=("SELECT user_type FROM $tbl_name WHERE username='$myusername' and password='$mypassword' and user_type ='$myusertype'");

$result2=mysql_query($sql);


// Mysql_num_row is counting table row
$count=mysql_num_rows($result2);
// If result matched $myusername and $mypassword, table row must be 1 row

if($count==1 ) {
//Register $myusername, $mypassword and redirect to file "login_success.php"
session_register("myusername");
session_register("mypassword");
session_register("myusertype");
  if($myusertype=="admin") {
   header("location: admin/index.php");
  } else if($myusertype=="staff"){
  header("location: staff/index.php");
  } else if($myusertype=="student"){
  header("location: student/index.php");  
} else {
 echo "Wrong usertype"; 
exit;  
}
}
else {
echo "Wrong Username or Password";
}

//ob_end_flush();
?>

If this doesn't work, I dont think I can help you with this.

awww, you are a star nav..YOU ARE A GENIUS NAV..THANK YOUUUUUUUUUUUU

:) You are welcome!

`hello..i'm new here..
can anybody plez help me solve this error
i've got this error
Parse error: syntax error, unexpected T_VARIABLE in C:\wamp\www\webphp\registerProc.php on line 56

<?php
include 'connection.php';

  $nameInst=$_POST['nameInst'];
  $name=$_POST['name'];
  $address=$_POST['address'];
  $tel=$_POST['tel'];
  $fax=$_POST['fax'];
  $emel=$_POST['emel'];
  $username=$_POST['username'];
  $password=$_POST['password'];

  $t1_name=$_POST['t1_name'];
  $t1_coach_name=$_POST['t1_coach_name'];
  $t1_coach_size=$_POST['t1_coach_size'];

  $t1_nameP1=$_POST['t1_nameP1'];
  $t1_sizeP1=$_POST['t1_sizeP1'];
  $t1_emelP1=$_POST['t1_emelP1'];
  $t1_nameP2=$_POST['t1_nameP2'];
  $t1_sizeP2=$_POST['t1_sizeP2'];
  $t1_emelP2=$_POST['t1_emelP2'];
  $t1_nameP3=$_POST['t1_nameP3'];
  $t1_sizeP3=$_POST['t1_sizeP3'];
  $t1_emelP3=$_POST['t1_emelP3'];

  $t2_name=$_POST['t2_name'];
  $t2_coach_name=$_POST['t2_coach_name'];
  $t2_coach_size=$_POST['t2_coach_size'];

  $t2_nameP1=$_POST['t2_nameP1'];
  $t2_sizeP1=$_POST['t2_sizeP1'];
  $t2_emelP1=$_POST['t2_emelP1'];
  $t2_nameP2=$_POST['t2_nameP2'];
  $t2_sizeP2=$_POST['t2_sizeP2'];
  $t2_emelP2=$_POST['t2_emelP2'];
  $t2_nameP3=$_POST['t2_nameP3'];
  $t2_sizeP3=$_POST['t2_sizeP3'];
  $t2_emelP3=$_POST['t2_emelP3'];

  $t3_name=$_POST['t3_name'];
  $t3_coach_name=$_POST['t3_coach_name'];
  $t3_coach_size=$_POST['t3_coach_size']; 

  $t3_nameP1=$_POST['t3_nameP1'];
  $t3_sizeP1=$_POST['t3_sizeP1'];
  $t3_emelP1=$_POST['t3_emelP1'];
  $t3_nameP2=$_POST['t3_nameP2'];
  $t3_sizeP2=$_POST['t3_sizeP2'];
  $t3_emelP2=$_POST['t3_emelP2'];
  $t3_nameP3=$_POST['t3_nameP3'];
  $t3_sizeP3=$_POST['t3_sizeP3'];
  $t3_emelP3=$_POST['t3_emelP3'];

$query = "insert into advisor values('','$nameInst', '$name', '$address', '$tel','$fax', '$emel', '$username','$password');"
$query2 = "insert into participant values('','$t1_name','$t1_coach_name','$t1_coach_size','$t1_nameP1','$t1_sizeP1','$t1_emelP1','$t1_nameP2','$t1_sizeP2','$t1_emelP2','$t1_nameP3','$t1_sizeP3','$t1_emelP3','$t2_name','$t2_coach_name','$t2_coach_size','$t2_nameP1','$t2_sizeP1','$t2_emelP1','$t2_nameP2','$t2_sizeP2','$t2_emelP2','$t2_nameP3','$t2_sizeP3','$t2_emelP3','$t3_name','$t3_coach_name','$t3_coach_size','$t3_nameP1','$t3_sizeP1','$t3_emelP1','$t3_nameP2','$t3_sizeP2','$t3_emelP2','$t3_nameP3','$t3_sizeP3','$t3_emelP3')";

mysql_query($query) or die(mysql_error());
mysql_query($query2) or die(mysql_error());
echo '<script type = "text/javascript">window.alert("You had registered successfully")</script>';
echo "<meta http-equiv='refresh' content='0;url=registerView.php'>";
?>                                                                                                                                                                                                                                                                                                                                                               

`

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.