473,386 Members | 1,705 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,386 software developers and data experts.

login page php wouldnt redirect

20
hi, im trying to make a login page that call the user from two different table (admin & user). it doesnt have any error but it somehow doesnt link to the required page.


login.php
Expand|Select|Wrap|Line Numbers
  1. if(isset($_POST['submit']))
  2.     {    
  3.         include 'connection.php';
  4.  
  5.         $username = trim(addslashes($_POST['username']));
  6.         $password = trim(addslashes($_POST['password']));
  7.  
  8.         if ($username != '' && $password != '') 
  9.         {
  10.  
  11.             $sql = "SELECT * FROM admin
  12.                         WHERE admin_username = '$username' AND admin_password = '$password'";
  13.             $result = mysql_query($sql) or die('Query failed. ' . mysql_error());
  14.             $row = mysql_fetch_array($result, MYSQL_ASSOC);
  15.  
  16.             if (mysql_num_rows($result) == 1) 
  17.             {
  18.                 $_SESSION['admin_name']=$row['admin_name'];
  19.                 $_SESSION['admin_addr']=$row['admin_addr'];
  20.                 $_SESSION['admin_position']=$row['admin_position'];    
  21.                 $_SESSION['admin_ic']=$row['admin_ic'];
  22.  
  23.                 if(isset($_SESSION['admin_username']) && $_SESSION['admin_username'] === TRUE )
  24.                 {
  25.                     print "<script>";
  26.                     print "window.alert('Welcome Admin'); self.location='admin.php';"; 
  27.                     print "</script>";                        
  28.                 }
  29.                 else
  30.                 {
  31.                 echo "<script languange = 'Javascript'>
  32.                 alert('please check again!!');
  33.                 location.href = 'login.php';</script>";
  34.                 }                                                                    
  35.             }
  36.         }
  37.     } 
  38.  
  39.  
  40.         if(isset($_POST['submit']))
  41.         {    
  42.             include 'connection.php';
  43.  
  44.             $username = trim(addslashes($_POST['username']));
  45.             $password = trim(addslashes($_POST['password']));
  46.  
  47.             if ($username != '' && $password != '') 
  48.             {
  49.  
  50.                 $sql = "SELECT * FROM staff
  51.                             WHERE staff_username = '$username' AND staff_password = '$password'";
  52.                 $result = mysql_query($sql) or die('Query failed. ' . mysql_error());
  53.                 $row = mysql_fetch_array($result, MYSQL_ASSOC);
  54.  
  55.                 if (mysql_num_rows($result) == 1) 
  56.                 {
  57.                     $_SESSION['staff_name']=$row['staff_name'];
  58.                     $_SESSION['staff_addr']=$row['staff_addr'];
  59.                     $_SESSION['staff_position']=$row['staff_position'];    
  60.                     $_SESSION['staff_ic']=$row['staff_ic'];
  61.  
  62.                     if ($_SESSION['staff_username']=='staff_username')
  63.                     {
  64.                         print "<script>";
  65.                         print "window.alert('Welcome user'); self.location='user.php';"; 
  66.                         print "</script>";                        
  67.                     }
  68.                     else
  69.                     {
  70.                     echo "<script languange = 'Javascript'>
  71.                     alert('Please check again!!');
  72.                     location.href = 'login.php';</script>";            
  73.                     }
  74.                 }
  75.             }            
  76.         }
  77.  
  78.  
  79. ?>

the outcome will always be "please check again!" eventhough the username and password is correct.

why? :(
Dec 15 '14 #1

✓ answered by Exequiel

It did not work because the $_SESSION['staff_username'] is empty, you did not set a value for that session.
put this code first
Expand|Select|Wrap|Line Numbers
  1. $_SESSION['staff_username']='staff_username';
before your
Expand|Select|Wrap|Line Numbers
  1.  if ($_SESSION['staff_username']=='staff_username')
and also for admin.
try it.

5 1649
Exequiel
288 256MB
It did not work because the $_SESSION['staff_username'] is empty, you did not set a value for that session.
put this code first
Expand|Select|Wrap|Line Numbers
  1. $_SESSION['staff_username']='staff_username';
before your
Expand|Select|Wrap|Line Numbers
  1.  if ($_SESSION['staff_username']=='staff_username')
and also for admin.
try it.
Dec 15 '14 #2
leesyaa
20
thanks! it work well :D

but now it doesnt respond to the

Expand|Select|Wrap|Line Numbers
  1. <?php 
  2. session_start();
  3. if(!isset($_SESSION['admin_username'])) {
  4.     header("Location: error.php");
  5.     }
  6.  
  7. ?>
that i set to secure the page.

the user could still open the admin page :(
Dec 15 '14 #3
Exequiel
288 256MB
Try this code, in this logic we need to check if the session is empty or not, if the session is not empty redirect for admin/staff page, if the session is empty redirect to login page.

Expand|Select|Wrap|Line Numbers
  1. <?php
  2. session_start();
  3. ob_start();
  4. include 'coonnectionToDB.php';//your connection to your database
  5.  
  6. if(!empty($_SESSION['admin_username']))
  7. {
  8.  header("Location: adminpage.php");
  9. }
  10. else if(!empty($_SESSION['staff_username']))
  11. {
  12.  header("Location: staffpage.php");
  13. }
  14. else
  15. {
  16.  header("Location: login.php");
  17. }
  18. ?>
  19.  
  20.  
  21.  
Dec 15 '14 #4
Rabbit
12,516 Expert Mod 8TB
You never check to see if they are an admin before showing the admin page. You merely set a variable do it's always true.
Dec 16 '14 #5
Exequiel
288 256MB
leesyaa's database design is not ok for me, she can make only 1 table for an account for admin and staff and determine what user type the user is.
Dec 18 '14 #6

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

Similar topics

5
by: Simon | last post by:
Hi, I have a Login.php page that logs the user in and out. I has two forms within the page, (depending on what we are trying to do), either one to log in or out. The form calls itself using a...
1
by: Stu | last post by:
Hi All, I have an ASP.NET application to which I have implemented forms authentication to handle security. It is a relatively straight forward solution with all aspx pages residing in the root...
12
by: ACaunter | last post by:
Hi all, I was wondering how i could write some code which would automatically open the Login Page once the session has expired? -- AdamPC@hotmail.com
10
by: GreggTB | last post by:
I've got an page (LOGIN.ASPX) that receives the user's login information. During the page load, it checks the credentials against a database and, if validation is successful, creates an instance of...
1
by: Andrew | last post by:
Hello, friends, I am using Forms Authentication in our asp.net app. However, I found that sometimes, the ReturnURL was not in QueryString. For example, I could redirect from login page without...
2
by: Morgan Cheng | last post by:
In VS2005, I create a ASP.NET website project.One page named Default.aspx with LoginView and LoginStatus controls; and one page named Login.aspx with Login control in it. After setup of users, I...
4
by: jobs | last post by:
Hello. If my users are logged in, and try to access restricted pages I want to direct them to a custom 403 page. If they are not logged in, I would like to continue to direct them to the login...
8
pentahari
by: pentahari | last post by:
Redirect to login page if session("UserName") is expired when the page useraccount.aspx load event My useraccount.aspx code : If Session("UserName") Is Nothing Then ...
3
by: =?Utf-8?B?QmlsbHkgWmhhbmc=?= | last post by:
I have a asp.net app. When session is invalid, how to redirect user to the login page? I don't want to add the code to redirect user to the login page into every page. Thanks, -Billy
2
by: Reza Ruslan | last post by:
I'm trying to create a simple login page with php using session. when i want to access the index.php, the page successfully redirect to login page. then I put username and password based on the table...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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...

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.