473,609 Members | 2,296 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Authenticating user in PHP

97 New Member
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
2 1333
Rabbit
12,516 Recognized Expert Moderator MVP
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 New Member
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
2543
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 OpenDSObject() method). Here is the code: --------------------------
3
16431
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 But I am having a problem figuring out the LDAP:// The LDAP:// that I pass looks like this (i substitued generic the
4
2218
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 a neat way to authenticate the scanned barcode without any further user action? The barcodes I'm using to play with the system are the ones on my CDs which have an embedded CR, which nicely press the default ASP button, but I may not be able to...
1
1046
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 of server. Also, I don't want to use WSE. I am sending user name and password in the SOAP header in clear text. But I don't want to send username and password in cleartext form. What is the best way to send username and password over the internet?...
0
1194
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 like this (i substitued generic the following with generic name):
3
2528
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 difficult for me to create more than one login page. In my web.config file I have it set so that unauthenticated users will be directed to the default.aspx page which handles logging in. As part of the default.aspx Page_Load I transfer users to a...
1
1694
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 server how is it possible using C#.net can anyone help ? thanks
0
781
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 very meagre knowledge of that. Iam searching since hours to find some information about this. Any information with WSE3.0 is greatly appreciated. Thanks
5
3121
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 function is authenticating user and thn reading data from datbase server. DOMAIN\WEBSERVER$' is nowhere in the picture but still I am not able to read the database. Here is the error. __________________________________________________________________...
0
8065
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8563
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8524
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8210
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
5507
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4013
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4074
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1656
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1379
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.