//mysql connection
$com = mysql_connect("localhost","root","");
mysql_select_db("usersystem");
if(isset($_POST['Submit'])) {
$userName = $_POST['userName'];
$passWord = $_POST['passWord'];
$result = mysql_query("SELECT userID FROM tbl_users WHERE userName='$userName' AND passWord='$passWord' LIMIT 1");
if(mysql_num_rows($result)==1) {
//"login success";
$data = mysql_fetch_array($result,1);
$_SESSION['userID'] = $data['userID'];
header("location: dashboard.php");
}
else {
//login failed
$error = "login failed";
}
I have set up a database in phpMyAdmin, and I have 2 users in my database. However, when I launch my site, I type in the username and password for both user's and it works! But, if I type in random words for both username and passwords it still logs in, I'm very confused. Please help me.
if(mysql_num_rows($query)==1){
//"login success";
$data=mysql_fetch_array($query,0);
$_SESSION['userID']=$data['userID'];
header("location:dashboard.php");
}
change the zero to 1 and also dont use mysql_ is deprecated use mysqli or PDO
$query=mysql_query("SELECT userID FROM tbl_users WHERE userName='$userName' AND passWord='$passWord' limit 1 ", $com);
and change this.
You are better off with
if(mysql_num_rows($query)) {
..
docs :
The number of rows in a result set on success or FALSE on failure.
$query is an empty variable.
Go for
$result = mysql_query("SELECT userID FROM tbl_users WHERE userName='$userName' AND passWord='$passWord' LIMIT 1");
if(mysql_num_rows($result))
{
//"login success";
$data = mysql_fetch_array($result,0);
...
Related
<?php
require 'includes/common.php';
$email=$_POST['email'];
$password=$_POST['password'];
$email= mysqli_real_escape_string($con, $email);
$password= mysqli_real_escape_string($con, $password);
//md5($password);
$select_user_query="select id , email from users where email='$email' and password= $password";
//removed 'email' to email//
$select_user_result= mysqli_query($con, $select_user_query) or die (mysqli_error($con));
if (mysqli_num_rows($select_user_result==0))
{
echo 'There no such user ';
}
else
{
$row=mysqli_fetch_array($select_user_result);
$_SESSION['email']=$email;
$_SESSION['id']= $row[0];
header('location:products.php');
}
?>
here is the code
**
here i am geting
email and password from post method
From my login page
i think most of the code is right
If i login with a user it logs in with any password
**
Your error is not in the query. it is on your condition
if(mysqli_num_rows($select_user_result) == 0){
echo 'No User found';
}else{
echo 'oK';
}
You ca try this way. I think your error is you inserted the ==0 inside the mysqli_num_rows() it must be outside.
I'm assuming password is a string..
$select_user_query="select id , email from users where email='$email' and password= $password";
is what you wrote,
you've maybe forgotten to put single quotes around $password here.
Please verify this code:
I use this code for login:
if user provides the Correct Credentials-- If part will work,
if not then else part will be work
<?php
// Grab User submitted information
$user = $_POST["username"];
$pass = $_POST["password"];
//$uc_id=$_GET["user_check_id"];
// Connect to the database
$con = mysqli_connect("localhost","root","pass123");
// Make sure we connected successfully
if(! $con)
{
die('Connection Failed'.mysql_error());
}
// Select the database to use
mysqli_select_db($con, "login");
//$query ="SELECT user_check_id from loginpage where username='$user' and password='$pass'";
$query ="SELECT * from loginpage where username='$user' and password='$pass'";
//echo $query;
if ($query)
{
$result = mysqli_query($con,$query);
$row = mysqli_fetch_array($result);
if($row["username"]==$user && $row["password"]==$pass)
{
echo "Welcome Admin";
}
else
{
echo"Please Enter the Correct Username/Password...!!";
}
}
?>
Put single quotes around $password in your query and then try.
I've put username and md5(password) on my MySQL database. Below is my old login PHP code. I want to add some code that can retrieve my md5 password, because on my old code there is no md5 password. Where is should I add md5(password)?
Here is my full login code:
<?
if ($_POST['username']) {
$username=trim($_POST['username']);
$username = mysql_real_escape_string($username);
$password=trim($_POST['password']);
$password=mysql_real_escape_string($password);
//$password = hash('md5','$password');
if ($password==NULL) {
header("Location: login.php?error=2");
}else{
if($_POST['code']!=$_SESSION['string']){
header("Location: login.php?error=1");
}else{
$query = mysql_query("SELECT username,password FROM tb_users WHERE username = '$username'") or die(mysql_error());
if(mysql_num_rows($query) == 0)
{
header("Location: login.php?error=3");
} else {
$data = mysql_fetch_array($query);
if($data['password'] != $password) {
header("Location: login.php?error=4");
}else{
$query = mysql_query("SELECT username,password FROM tb_users WHERE username='$username' ") or die(mysql_error());
$row = mysql_fetch_array($query);
$nicke=$row['username'];
$passe=$row['password'];
setcookie("usNick",$nicke,time()+36000);
setcookie("usPass",$passe,time()+36000);
$lastlogdate=time();
$lastip = getRealIP();
$querybt = "UPDATE tb_users SET lastlogdate='$lastlogdate', lastiplog='$lastip' WHERE username='$nicke'";
mysql_query($querybt) or die(mysql_error());
$query = mysql_query("SELECT akhirupgrade from tb_upgrade WHERE username = '$username' and status='upgraded'") or die(mysql_error());
if(mysql_num_rows($query) > 0) {
$row = mysql_fetch_array($query);
$akhir=$row["akhirupgrade"];
$tgl=time();
if ($tgl > $akhir) {
$query = mysql_query("update tb_upgrade set status='', date='', paket='', akhirupgrade='' WHERE username='$username' and status='upgraded'");
$query = mysql_query("update tb_users set account='' WHERE username='$username'");
}
}
header("Location: member.php");
}
}
}
}
}
?>
I would use password_hash() if you running on php 5.5 or greater
When you send the password to the database simply hash it with the function
$password = password_hash(filter_input(INPUT_POST, "password"));
The when you pull the password back out of the database do the same thing to the password they submitted.
$passwordFromDb = $result['password']; //Password from the database
$passwordFromLoginForm = password_hash(filter_input(INPUT_POST, "password");
//Then when youve got the password to check it agaisnt there input
if($passwordFromDb === $passwordFromForm){
//The password they entered was the same as the password in the database
} else {
//The password was wrong
}
I have not tested this code so there may be errors but hopefully youll get the point :)
PS dont use MD5 please, Very insecure
If you must use md5
$password = md5(filter_input(INPUT_POST, "password"));//Store password
$passwordFromDb = $result['password']; //Password from the database
$passwordFromLoginForm = md5(filter_input(INPUT_POST, "password");
//Then when youve got the password to check it agaisnt there input
if($passwordFromDb === $passwordFromForm){
//The password they entered was the same as the password in the database
} else {
//The password was wrong
}
I've created a below script, which is intentionally not secure, in order to learn a bit more about cyber security.
session_start();
if($_SESSION['userSession']) {
header("location: home.php");
}
if($_POST) {
$username = $_POST['username'];
$password = $_POST['password'];
$con = mysqli_connect("localhost", "myUsername", "myPassword", "myDatabase");
if(!$con) {
die("Error: " . mysqli_connect_error());
}
$query = "SELECT * FROM users WHERE username = '$username' && password='$password'";
$result = mysqli_query($con, $query);
$numResults = mysqli_num_rows($result);
if($numResults == 1) {
$row = mysqli_fetch_array($result, MYSQLI_ASSOC);
$_SESSION['userSession'] = $row['id'];
header("location: home.php");
} else {
echo "Error Logging In";
}
mysqli_close($con);
}
As you can see, I have not escaped the user input and the password has not been hashed.
Therefore, I am presuming that this should be an easily hackable login. However, I have attempted to use the below input in both of the username and password fields, but always get the output "Error Logging In".
password' OR '1' = '1'";
How can I try to bypass/hack my login script?
If we use sql statement directly to fetch username and password field then it can be bypass with ' OR '1' = '1 pattern, because when you put ' OR '1' = '1 in username and password field that values carry forward to sql statement and in that statement ' or '1' = '1 is true for all the cases and that's a reason login can bypass.
I am creating a simple PHP login system using SQLite, when when the user posts the the HTML form, the system guides them to the "members only page" regardless of what they entered. Here is my form processing code:
<?php
$username = $_POST["username"];
$password = $_POST["password"];
$database = new PDO("sqlite:database.sqlite");
$result = $database -> query("SELECT COUNT (*) FROM accounts WHERE username = '$username' AND password = '$password'");
if ($result > 0)
{
setcookie("session", "Cool", time()+3600);
header("location:index.php");
}
else
{
echo "Failure";
}
?>
Help!
I think it should be...
if ($result->rowCount() > 0) ...
AND: use prepared statements to avoid SQL-injection, don't store uncrypted passwords in your DB, it's a huge security-problem.
Try this (You shouldn't need count, just a simple logical statement):
$query = "SELECT * FROM accounts WHERE username = :username AND password = :password";
$stmt = $database->prepare($query);
$stmt->execute(array(":username"=>$username, "password"=>$password));
$result = $stmt->fetch(PDO::FETCH_ASSOC);
if($result)
{
//Success
setcookie("session", "Cool", time()+3600);
header("location:index.php");
}
// Not successful.
return null;
I'm creating a login with different user level. How to get the user_level field fom my table users to comapare if the username and password is admin or staff.
Here's the code:
<?php
include("../config/database.php");
if(isset($_POST["login"]))
{
$username = $_POST['username'];
$password = $_POST['password'];
$username = stripslashes($username);
$password = stripslashes($password);
$username = mysql_real_escape_string($username);
$password = mysql_real_escape_string($password);
$query = "SELECT * FROM users
WHERE username= '$username' OR email= '$username'
AND password='$password'";
$result = mysql_query($query);
$count = mysql_num_rows($result);//counting table rows
if($count==1)
{//check if found username and password
if(user_level == "admin")
{
header("Location: ../views/admin/dashboard.php");
}
elseif(user_level=="patient")
{
header("Location: ../views/default/home.php");
}
}
else
{
echo "WRONG USERNAME OR PASSWORD!";
}
}
?>
Make few changes , it wil work...
$count = mysql_fetch_assoc($result);
and also
if($count==1){//check if found username and password
if($count['user_level'] == "admin"){
header("Location: ../views/admin/dashboard.php");
}elseif($count['user_level']=="patient"){
header("Location: ../views/default/home.php");
}
}
if(user_level == "admin"){
should be
$record=mysql_fetch_assoc($result);
if($record["user_level"] == "admin"){
First of all try to separate using parentheses
$sql = "SELECT * FROM users
WHERE (username= '$username' OR email= '$username')
AND password='$password'";";
And also mysql_* functions are depricated. You can use mysqli_* or PDO for that matter.
And to answer your question, do you have a field that determines the role of the user in your table? if so simply return the field and check if the user is the appropriate role.
Example:
$data = mysql_fetch_assoc($result);
if ($data["user_level"] == "admin" ){
...
}
You should add a field user_level in Database table users. You can assign it value admin or patient at registration level.
After that, use the same query with a new variable $previlage. Then use it to fetch contents from database, Like below,,,
$previlage=mysql_fetch_array($result); //fetch contents from db
if($previlage['user_level'] == "admin"){
header("Location: ../views/admin/dashboard.php"); // if userlevel admin
}elseif($previlage['user_level']=="patient"){
header("Location: ../views/default/home.php"); // if user level is patient
}