473,396 Members | 1,726 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Help with my login script

16
Hi all,

I've got three types of users. When they log in i want them to go to a different page dependent on the type of user.

Its nearly workin but does anyone have any idea why the teacher check is workin but the student check isn't?

Also would using sessions be a better solution than using the include( ' ' ).

Expand|Select|Wrap|Line Numbers
  1. <?php
  2. $host="localhost"; // Host name 
  3. $username="student1"; // Mysql username 
  4. $password="student1"; // Mysql password 
  5. $db_name="wep"; // Database name 
  6. $tbl_name="user_tbl"; // Table name 
  7.  
  8. // Connect to server and select databse.
  9. mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
  10. mysql_select_db("$db_name")or die("cannot select DB");
  11.  
  12. // Define $myusername and $mypassword 
  13. $myusername=$_POST['myusername']; 
  14. $mypassword=$_POST['mypassword']; 
  15.  
  16. // check username and password
  17. $sql="SELECT * FROM $tbl_name WHERE User_id='$myusername' and User_pass='$mypassword'";
  18. $result=mysql_query($sql);
  19. $rows = mysql_num_rows($result);
  20.  
  21. // if match found login
  22. if($rows==1){
  23.  
  24. //check to see if student
  25.     $sql="SELECT * FROM user_tbl, student_tbl WHERE user_tbl.User_id='$myusername' AND student_tbl.User_id='$myusername'";
  26.     $result=mysql_query($sql);
  27.     $rows = mysql_num_rows($result);
  28.  
  29.  //if == 1 user is a student
  30.  if($rows==1){
  31.    include( 'teacher.php' );
  32.    }
  33.  else{
  34.     //check to see if teacher
  35.     $sql="SELECT * FROM user_tbl, teacher_tbl WHERE user_tbl.User_id='$myusername' AND teacher_tbl.User_id='$myusername'";
  36.     $result=mysql_query($sql);
  37.     $rows = mysql_num_rows($result);
  38.       if($rows==1){
  39.         include( 'teacher.php' );
  40.         }
  41.       else{
  42.         //user is an employer
  43.         include( 'employer.php' );
  44.          }
  45.       }
  46. }
  47. //else check username
  48. else{
  49.   $sql="SELECT * FROM $tbl_name WHERE User_id='$myusername'";
  50.   $result=mysql_query($sql);
  51.   $rows = mysql_num_rows($result);
  52.  
  53.   // if == 1 username ok so password wrong
  54.   if($rows==1){
  55.     echo "Invalid password entered";
  56.     }
  57.    // wrong username
  58.    else{
  59.     echo "Invalid username entered";
  60.     }
  61. }
  62.  
  63. ?>
Mar 17 '07 #1
3 1167
quill
12
Hi,

How about this logic:

You have a table called "users"

In that, you have their details, such as first name, last name, username, password and you add "type".

"type" is an INT.

Now you make another table called "types" with the fields: id,name,url.

(I'm doing this off the top of my head).

So when the user submits the form information you have something like:
Expand|Select|Wrap|Line Numbers
  1. if(isset($_POST['submit'])) {
  2.  
  3. $username = $_POST['username'];
  4. $password = $_POST['password'];
  5. $e1 = "SELECT * FROM users WHERE username = '$username' AND password = '$password'";
  6. $e2 = mysql_query($e1,$conn) // conn is your connection variable to mysql
  7. if(mysql_num_rows($e2)==1) {
  8. die('Invalid username and password combination');
  9. }
  10. // If there is a match, continue on
  11. $e3 = mysql_fetch_array($e2);
  12. // Get their type
  13. // 1 = student, 2 = teacher (you would have this in users database
  14. $type = $e3['type'];
  15. // So now we have their type and now we look up to see what to do with them
  16.  
  17. $r1 = "SELECT * FROM types WHERE id = '$type'" // remember type is an INT
  18. $r2 = mysql_query($r1,$conn);
  19. $r3 = mysql_fetch_array($r2);
  20. $url = $r3['url'];
  21. // Now we can take them to whatever page they are set to
  22. header("Location: $url");
  23. }
  24.  
Using this code, you can add as many types as you like, each with a different URL to go to.

Have a look at http://www.devshed.com/c/a/PHP/Creat...-Login-Script/ or just type into google something like: php mysql secure login
Mar 17 '07 #2
JonnyB
16
Cheers Quill,

Thats not quite what i was after the way i'm doin it is to have a user table, student table, teacher table and employer table the later three with forgien keys to user table and working their user type out through that.

I dont think there's anything wrong with the method just the code thats not quite working right.

There's something wrong with the student check sql section and i'm not to sure why as the same is working for the teacher check.

Also i'm not sure if i'm using the best way to direct them to the page. I noticed you used the header() method using laction in it. I've not had much luck using that. How does it work?

Thanks again, Jonny.
Mar 17 '07 #3
JonnyB
16
I manged to sort this problem. Turns out there's nothing wrong with the code I'd forgot to input the student details in to the student table of the database so the user existed but wasnt finding the match.

My bad!
Mar 19 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Ralph Freshour | last post by:
I bought the PHP and MySQL For Dummies book and I'm having trouble understanding how I use PHP to verify and check forms input text data - the book shows snippets of code so I know how to do the...
6
by: Dal | last post by:
Help! I'm trying to get a login script to work. I get this error message MySQL Login Error: You have an error in your SQL syntax near ''jvsd0001_customers` WHERE cust_name='testuser' AND...
3
by: f700b | last post by:
I have the bellow javascript code. I know the username and the password , and I like to modify this code for login without press intro. It is possible?. Thanks --------------------------------...
0
by: Joe | last post by:
i posted similar to vb forum but without success. Can anyone help here I have been wrestling with a problem for days. The solution is probably simple but i am not that experienced with html. I...
23
by: casper christensen | last post by:
Hi I run a directory, where programs are listed based on the number of clicks they have recieved. The program with most clicks are placed on top and so on. Now I would like people to be apple to...
5
eragon
by: eragon | last post by:
I am making a password script, i have most of it done, ill show you, but i hit a brick wall.... lol, I have it so when you log in it takes you to your main page, that works, but when you enter the...
1
by: Tarcetial | last post by:
Im trying to make a personal website with a log in/registration system. I found some help with the log in and now i just need some help with the registration. This is the coding im already using: ...
5
by: silmana | last post by:
hello, i have this problem that i cannot bound the registration script to the login script, what i mean is that when i register, i cannot login in the login script although the information comes to...
6
by: KDawg44 | last post by:
Hi, My responseXML is always null on my AJAX call. When I browse directly to the PHP script I am calling, the XML file shows up just fine. I have read that if a returned XML file is not...
0
by: richard12345 | last post by:
Hi Guys I have problem with site I am building. The sidebar with menu and other thinks is overlapping footer. The footer move with the content and but it dos it dos not move with the sidebar. ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.