473,405 Members | 2,185 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,405 software developers and data experts.

Authenticating user in PHP

97 64KB
I have a login page for an admin area and I want to keep people from being able to access pages if they're not logged in.

This is the script that logs the user in;

Expand|Select|Wrap|Line Numbers
  1. <?php
  2. session_start();
  3. include ('includes/DbCon.php');
  4.  
  5. // username and password sent from form 
  6. $user=$mysqli->real_escape_string($_POST['user']);
  7. $password=$mysqli->real_escape_string($_POST['password']);
  8.  
  9. $sql="SELECT * FROM xyz WHERE user='$user' and password='$password'";
  10. $result=$mysqli->query($sql);
  11.  
  12. // Mysqli_num_rows is counting table rows
  13. if(mysqli_num_rows($result) == 1){
  14. $row = mysqli_fetch_array($result, MYSQLI_BOTH);
  15.  
  16. // Register $user, $password and redirect to admin area
  17.  
  18. $_SESSION['user']=="user";
  19. $_SESSION['password']=="password";
  20.  
  21. echo '<script type="text/javascript">';
  22. echo 'document.location.href = "/pc.v.2/admin.php";';
  23. echo '</script>';
  24.  
  25. }
  26. else {
  27. echo '<script type="text/javascript">';
  28. echo 'alert("Invalid Username or Password");';
  29. echo 'history.back();';
  30. echo '</script>';
  31.  
  32. }
  33. mysqli_close($mysqli);
  34. ?>
  35.  

And this is how I'm trying to authenticate the login;

Expand|Select|Wrap|Line Numbers
  1. <?php
  2. session_start();
  3. if (empty($_SESSION['user'])){
  4. header("location:login.php");
  5. }
  6. else{
  7. echo '<script type="text/javascript">document.getElementsByClassName("login").innerHTML="Logout";';
  8. echo 'document.location.href = "/pc.v.2/admin.php";'; 
  9. echo "</script>";
  10. }
  11. ?>
  12.  

But it redirects back to the login page when the correct username and password are entered. There's something wrong with the script that checks the session, isn't there?
Jun 11 '15 #1

✓ answered by Rabbit

You never set your session variables. 2 equal signs is a comparison operator, 1 equal sign is the assignment operator.

2 1324
Rabbit
12,516 Expert Mod 8TB
You never set your session variables. 2 equal signs is a comparison operator, 1 equal sign is the assignment operator.
Jun 11 '15 #2
tdrsam
97 64KB
Thanks. I also had a redirect in the authentication script that was causing a constant reloading of the page.
Jun 11 '15 #3

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

Similar topics

0
by: DejaUser | last post by:
I have been strugling with this one for few weeks now. I hope to get some help here. Following code works fine, except that I have to hard-code the user's fullpath (2nd parameter of the...
3
by: mrwoopey | last post by:
Hi, I am using the example "Authenticate against the Active Directory by Using Forms Authentication and Visual Basic .NET": http://support.microsoft.com/default.aspx?scid=KB;EN-US;326340 ...
4
by: teddysnips | last post by:
My clients want their employees to log in by scanning a barcode on their passes. I've set up a web page with a text box to capture the scanned barcode. Two questions. 1. Can anyone think of...
1
by: KY | last post by:
Hi, I am using custom authentication using SOAP headers to authenticate users of a web service. I cannot use windows authentication as our client applications need not in the same domain as that...
0
by: Nandagopal | last post by:
Hi, I am using the example "Authenticate against the Active Directory by Using Forms Authentication and C#": But I am having a problem figuring out the LDAP:// The LDAP:// that I pass looks...
3
by: Sleepwalker | last post by:
Hi, I'm currently working on creating a stripped down part of a company web page, that will be accessed from cellphone. The problem I have is that the Windows Forms authentication makes it...
1
by: tim007 | last post by:
hi all, my issue is The sites login user id and password is same as the Qmail Mail servers userid and password so i need to authenticate the user, check if user id / pwd is valid in the Qmail...
0
by: darshptl | last post by:
Hello Friends, Can anyone let me know how to validate the user details in a token fundamentally which has to be persesitent and is used for making calls to web services? Its very urgent. I have a...
5
by: Manikrag | last post by:
Hi Team, I have copied one ASP.NET application on a new server and configured it all.Now I am getting below error when I am trying to authenticate users thru LdapAuthentication function. This...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
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
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
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,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.