select data from php sql tables - php

I have a error with my login script. The connection is successful but it does not display a success message when the login is successful. Still displays my dbconn file message.
Any ideas on how to make this work?
dbconn code
<?php
$user = 'root';
$password = 'root';
$db = 'peerwise';
$host = 'localhost';
$port = 8889;
$link = mysqli_init();
$dbconn = mysqli_real_connect($link, $host, $user, $password, $db, $port);
if (!$dbconn){
echo "Not connected to database";
}else{
echo "Successfully connected";
}
?>
login code
<?php
include_once("includes/dbconn.php");
$username = $_POST['username'];
$password = $_POST['password'];
$sql = "SELECT * FROM Users WHERE username = '$username' AND password = '$password'";
$query = mysqli_query($dbconn, $sql) or die(mysqli_error());
$data = mysqli_fetch_array($query);
if ($data['username'] == $username && $data['password'] == $password) {
echo "success";
} else {
echo "errr";
}
?>

Your using the wrong connection object. Rather than variable $dbconn use $link instead in your query calls, so no mysql error will halt execution of your script, from or die(mysqli_error());
Replace with this:
$query = mysqli_query($link, $sql) or die(mysqli_error());

<div class="login-form-1">
<form id="login" class="text-left" method="POST" action="loginprocess.php">
<div class="main-login-form">
<div class="login-group" id="login">
<div class="form-group">
<input type="text" class="form-control" id="lg_username" name="username" placeholder="Username">
</div>
<div class="form-group">
<input type="password" class="form-control" id="lg_password" name="password" placeholder="Password">
</div>
</div>
<button type="submit" class="login-button" onclick="loginFieldCheck()">Login</button>
</div>
<p id="emptyMessage">Username or Password is incorrect</p>
<p class="create-account"><a class="create-account" onclick="register()">Create a account</a></p>
</form>
</div>

Use mysqli_connect for connection:
<?php
$user = 'root';
$password = 'root';
$db = 'test';
$host = 'localhost';
$port = 3306;
$dbconn = mysqli_connect($host, $user, $password, $db,$port);
if (!$dbconn){
echo "Not connected to database";
}else{
echo "Successfully connected";
}
?>

Related

Someone can check my session?

Please someone can check whether my session is working or not.
I am not sure as i am still beginner.
login.php is the main page for user to login the username and password :
<body>
<form action="" method="post">
<div class="imgcontainer">
<img src="KBR2xN6.jpg" alt="Avatar" class="avatar">
</div>
<div class="container">
<label><b>Username</b></label>
<input type="text" placeholder="Enter Username" name="name" required>
<br />
<label><b>Password</b></label>
<input type="password" placeholder="Enter Password" name="pass" required>
<button type="submit">Login</button>
<button type="reset" class="cancelbtn">Reset</button>
</div>
</form>
</body>
As for connections.php is to connect to the local server :
$host = "localhost";
$username = "root";
$password = "";
$database = "netbook 1 malaysia";
try {
$connect = new PDO("mysql:host=$host; dbname=$database", $username, $password);
$connect->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch(PDOException $ex) {
echo 'Connection Failed : '.$ex->getMessage();
}
As for session.php i am not sure:
session_start();
include('connections.php');
$username = $_POST['name'];
$password = $_POST['pass'];
$sql = "SELECT * FROM pengguna WHERE username = '$username' AND password = '$password'";
$result = $connect->query($sql);
if($result->rowcount()>0){
foreach($result AS $data){
$_SESSION['name'] = $data['name'];
$_SESSION['pass'] = $data['pass'];
echo "<script>alert('Login Success');
window.location.href='view.php';
</script>";
}
}
else {
echo "<script>alert('Login Failed');
window.location.href='login.php';
</script>";
}
Check for me please.
Just add this code to your view.php file.
session_start();
print_r($_SESSION);
If it prints values you saved in session then its working.

Match user name and password with mysql and php not working

I design a log in page and i want to match the username and password with saved data from database and open the dashboard page here is my html code.
> <form class="m-t" role="form" method="post" action="dashboard_4.html">
> <div class="form-group">
> <input type="email" class="form-control" placeholder="Username" class="form-control" required=""
> name="logemail">
> </div>
> <div class="form-group">
> <input type="password" class="form-control" placeholder="Password" class="form-control" required=""
> name="logpass">
> </div>
> <button type="submit" class="btn btn-primary block full-width m-b" id="login" name ="submit">Login</button>
>
> <small>Forgot password?</small>
> <p class="text-muted text-center"><small>Do not have an account?</small></p>
> <a class="btn btn-sm btn-white btn-block" href="register.html">Create an account</a>
> </form>
and here is my php code:
<?php
$servername = "localhost";
$username = "sehnoqta_userbmc";
$password = "u?gQ=uS%t;a?";
$dbname = "sehnoqta_bmc";
if(isset($_POST['submit']))
{
$username = $_POST['logemail'];
$password = $_POST['logpass'];
$con=mysqli_connect("localhost","sehnoqta_userbmc","?gQ=uS%t;a?","rsehnoqta_bmc");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$qz = "SELECT * FROM regis where email1='".$username."' and password3='".$password."'" ;
$qz = str_replace("\'","",$qz);
$result = mysqli_query($con,$qz);
$row = mysqli_num_rows($result);
if($row == 1)
{
header("location:new_page.php");
exit();
}
mysqli_close($con);
}
?>
but it open the page even i type the username and password incorrect, is there any problem with my code or any....
You are using different variables to your connection. Use same username and passwords, otherwise you will have access denied. I just removed your css and simplified few things. (note: this is a solution for the mentioned question with its code provided. It is not a solution for data expose and security reasons)
File: login.php
<html>
<head>
<meta charset="utf-8">
<title>LogIn.php</title>
</head>
<body align="center">
<form name="form" method="post" action="dashboard_4.php">
<div >
<input type="email" placeholder="Username" name="logemail">
</div>
<div >
<input type="password" placeholder="Password" name="logpass">
</div>
<button type="submit" id="login" name ="submit">Login</button>
<a ><small>Forgot password?</small></a>
<p ><small>Do not have an account?</small></p>
<a >Create an account</a>
</form>
</body>
</html>
File dashboard_4.php
<?php
$servername = "localhost";
$username = "sehnoqta_userbmc";
$password = "u?gQ=uS%t;a?";
$dbname = "sehnoqta_bmc";
$con = mysqli_connect("localhost","sehnoqta_userbmc","u?gQ=uS%t;a?","sehnoqta_bmc");
// Check connection
if (mysqli_connect_errno()){
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
if(isset($_POST['submit'])){
$username = $_POST['logemail'];
$password = $_POST['logpass'];
$qz = "SELECT * FROM regis WHERE email1='$username' and password3='$password' ";
//echo $qz."<br/>";
$result = mysqli_query($con,$qz);
//$temp=mysqli_fetch_assoc($result);
//echo $temp['email1']." ".$temp['password3'];;
$row = mysqli_num_rows($result);
if($row == 1){
header("location:new_page.php");
exit();
}//else {echo "no record combination";}
mysqli_close($con);
}
?>
why dont you try:
<?php
$servername = "localhost";
$username = "sehnoqta_userbmc";
$password = "u?gQ=uS%t;a?";
$dbname = "sehnoqta_bmc";
if(isset($_POST['submit']))
{
$username = htmlspecialchars($_POST['logemail']);
$password = htmlspecialchars ($_POST['logpass']);
$con=mysqli_connect("localhost","sehnoqta_userbmc","?gQ=uS%t;a?","rsehnoqta_bmc");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$username = mysqli_real_escape_string($con , $username);
$password = mysqli_real_escape_string($con , $password);
$qz = "SELECT * FROM `regis` where `email1` = '$username' and `password3` ='$password'" ;
$result = mysqli_query($con,$qz);
$row = mysqli_num_rows($result);
if($row == 1)
{
header("location:new_page.php");
exit();
}
mysqli_close($con);
}
?>

PHP Login System with sessions cannot login

I am working on login system. But, i cannot log in. I have set my database table.
login.php
<?php
session_start();
if(isset($_POST['login'])) {
include_once("db.php");
$username = strip_tags($_POST['username']);
$password = strip_tags($_POST['password']);
$username = stripslashes($username);
$password = stripslashes($password);
$username = mysqli_real_escape_string($db, $username);
$password = mysqli_real_escape_string($db, $password);
$password = md5($password);
$sql = "SELECT * FROM users WHERE username='$username' LIMIT 1";
$query = mysqli_query($db, $sql);
$row = mysqli_fetch_array($query);
$id = $row['id'];
$db_password = $row['password'];
if($password == $db_password) {
$_SESSION['username'] = $username;
$_SESSION['id'] = $id;
header("Location: av_pocetna.html");
} else {
echo "You didn't enter the correct details!";
}
}
?>
<html>
<head>
<title>Login</title>
</head>
<body>
<h1 style="font-family: Tahoma;">Login</h1>
<form action="login.php" method="post" enctype="multipart/form-data">
<input placeholder="Username" name="username" type="text" autofocus>
<input placeholder="Password" name="password" type="password">
<input name="login" type="submit" value="Login">
</form>
</body>
</html>
and this is db.php
<? php
$db=mysqli_connect('192.168.1.113:8080','root','hidden','av');
?>
connent of users table
id
username
password
Edit Edit
Copy Copy
Delete Delete
1
a
0cc175b9c0f1b6a831c399e269772661
Your form code look right. Just change like below your login.php code:-
<?php
session_start();
error_reporting(E_ALL);
ini_set('display_errors',1);
$conn = mysqli_connect('host-name','user-name','password','database-name');
if($conn){
if(isset($_POST['username']) && isset($_POST['password'])) {
$username = $_POST['username'];
$password = $_POST['password'];
$username = mysqli_real_escape_string($db, $username);
$password = mysqli_real_escape_string($db, $password);
$password = md5($password);
$sql = "SELECT * FROM users WHERE username='$username' LIMIT 1";
$query = mysqli_query($db, $sql);
if($query){
$row = mysqli_fetch_array($query);
$id = $row['id'];
$db_password = $row['password'];
if($password == $db_password) {
$_SESSION['username'] = $username;
$_SESSION['id'] = $id;
header("Location: av_pocetna.html");
} else {
echo "You didn't enter the correct details!";
}
}else{
echo "query not executed because".mysqli_error($conn);
}
}
}else{
echo "db connection error".mysqli_connect_error();
}
?>
Note:- i have added connection code here only,so change the credentials there. And use this same code to check working or not?
Also if you are working on your local then change ip address to localhost and check. If it will work then it will work with include("db.php") too.I mean to say try with $conn = mysqli_connect('localhost','root','aleksandar','av');
Here is the working login.php
<?php
session_start();
error_reporting(E_ALL);
ini_set('display_errors',1);
$conn = mysqli_connect('localhost','root','aleksandar','av');
$db = new mysqli('localhost','root','aleksandar','av');
if($conn){
if(isset($_POST['username']) && isset($_POST['password'])) {
$username = $_POST['username'];
$password = $_POST['password'];
$username = mysqli_real_escape_string($db, $username);
$password = mysqli_real_escape_string($db, $password);
$password = md5($password);
$sql = "SELECT * FROM users WHERE username='$username' LIMIT 1";
$query = mysqli_query($db, $sql);
if($query){
$row = mysqli_fetch_array($query);
$id = $row['id'];
$db_password = $row['password'];
if($password == $db_password) {
$_SESSION['username'] = $username;
$_SESSION['id'] = $id;
header("Location: av_pocetna.html");
} else {
echo "You didn't enter the correct details!";
}
}else{
echo "query not executed because".mysqli_error($conn);
}
}
}else{
echo "db connection error".mysqli_connect_error();
}
?>
<html>
<head>
<title>Login</title>
</head>
<body>
<h1 style="font-family: Tahoma;">Login</h1>
<form action="login.php" method="post" enctype="multipart/form-data">
<input placeholder="Username" name="username" type="text" autofocus>
<input placeholder="Password" name="password" type="password">
<input name="login" type="submit" value="Login">
</form>
</body>
</html>
Oh Okay.
Lets try debugging one step at a time then.
In your db.php file, use this:
// Connecting to mysql database
$db = new mysqli('192.168.1.113:8080','root','hidden','av');
// Check for database connection error
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
If you get any error, please dump it here for debugging.
Updated.
// Connecting to mysql database
$db = new mysqli('localhost','root','hidden','av');
// Check for database connection error
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

Adding user to MySQL database in php using phpMyAdmin

I think I am successfully connecting to my database by:
<?php
$user = 'root';
$pass = '9KSroMDjEqNmEYY4';
$db = 'chatservice';
$host = '127.0.0.1';
$conn = new mysqli($host, $user, $pass, $db, 3306) or die("Unable to connect");
if ($conn->connect_error){
die("Connection failed: " . $conn->connect_error);
}
?>
My question is how I would use the registration code to successfully add a user to the database. When entering the form I press register I do not get any error messages stating that the registration didn't succeed. It seems that the php code is not being reached after the initial connection. I am new to php and mySQL so any tips on formatting would be nice too!
<?php
require('connect.php');
if(isset($_POST['user']) && isset($_POST['password'])){
$user = $_POST['user'];
$id = $_POST['IDNUM'];
$password = $_POST['password'];
$query = "INSERT INTO 'users' (user ,IDNUM ,password) VALUES('$user', '$id', '$password')";
$result = mysqli_query($query);
if($result){
$msg = "Registered Sussecfully";
echo $msg;
}
else
$msg = "Error Registering";
echo $msg;
}
?>
<div class="register-form">
<title>Chat Page Start</title>
<form action="" methods="POST">
<p>
<label>Username: </label>
<input id="user" type="text" name="user" placeholder="user" />
</p>
<p>
<label>ID: </label>
<input id="IDNUM" type="text" name="IDNUM" placeholder="ID number" />
</p>
<p>
<label>Password: </label>
<input id="password" type="password" name="password" placeholder="password" />
</p>
<a class="btn" href="login.php">Login</a>
<input class="btn register" type="submit" value="Register" />
</form>
</div>
Another thing is how would I check the status of my database connection and where I should be checking this status?
your database connection is mysqli_connect and you execute the query in mysql_query is not proper.
<?php
require('connect.php');
if(isset($_POST['user']) && isset($_POST['password'])){
$user = $_POST['user'];
$id = $_POST['IDNUM'];
$password = $_POST['password'];
$query = "INSERT INTO 'users' (user ,IDNUM ,password) VALUES('$user', ' $id ', '$password')";
$result = mysqli_query($query,$conn);
if($result){
$msg = "Registered Sussecfully";
}
else
$msg = "Error Registering";
}
?>
You are connecting database using mysqli:
$conn = new mysqli('localhost', $user, $pass, $db, 3306) or die("Unable to connect");
And executing query using mysql:
$query = "INSERT INTO 'users' (user ,IDNUM ,password) VALUES('$user', '$IDNUM', '$password')";
$result = mysql_query($query);

Using PDO and sessions to create a login form

I have created a login-logout form using PDO and php session. Here is the entire code for different pages
LOGIN FORM
<form role="form" class="omb_loginForm" action="login.php" autocomplete="off" method="POST">
<div class="input-group">
<span class="input-group-addon"><i class="fa fa-user"></i></span>
<input type="email" class="form-control" name="email" placeholder="Email">
</div>
<span class="help-block"></span>
<div class="input-group">
<span class="input-group-addon"><i class="fa fa-lock"></i></span>
<input type="password" class="form-control" name="password" placeholder="Password">
</div>
<span class="help-block"></span>
<button class="btn btn-lg btn-primary btn-block" name="submit" type="submit">Login</button>
</form>
login.php
<?php
session_start();
if (isset($_POST['submit'], $_POST['email'], $_POST['password']))
{
try
{
$email = $_POST['email'];
$password = $_POST['password'];
$dbhost = "localhost";
$dbname = "abc";
$dbuser = "abc";
$dbpass = "pwd";
$conn = new PDO("mysql:host=$dbhost;dbname=$dbname", $dbuser, $dbpass);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = "SELECT * FROM register WHERE `email` = :email AND `password` = :password ";
$stmt = $conn->prepare($sql);
$stmt->execute(array(':email' => $_POST['email'], ':password'=> $_POST['password']));
$num=$stmt->rowCount();
if($num > 0)
{
header("location:dashboard.php");
}
else
{
header("location:login_form.html");
}
}
catch (Exception $e)
{
echo 'Caught exception: ', $e->getMessage(), "\n";
}
}
?>
session.php
<?php
$dbhost = "localhost";
$dbname = "abc";
$dbuser = "abc";
$dbpass = "pwd";
$conn = new PDO("mysql:host=$dbhost;dbname=$dbname", $dbuser, $dbpass);
session_start();
$user_check=$_SESSION['login_user'];
$result = $conn->prepare("SELECT * FROM register WHERE email= :$user_check");
$result->execute(array(":usercheck"=>$user_check));
$row = $result->fetch(PDO::FETCH_ASSOC);
$login_session =$row['email'];
$user_id =$row['id'];
$user_passwords = $row['password'];
if(!isset($login_session))
{
$conn = null;
header('Location: login_form.html');
}
?>
dashboard.php
<?php
include('session.php');
?>
<!DOCTYPE html>
<html>
<head>
<title>Your Home Page</title>
<link href="style.css" rel="stylesheet" type="text/css">
</head>
<body>
<div id="profile">
<b id="welcome">Welcome : <i><?php echo $login_session; ?></i></b>
<b id="logout">Log Out</b>
</div>
</body>
</html>
logout.php
<?php
session_start();
if(session_destroy())
{
header("Location: index.php");
}
?>
the problem that i am facing is that when the i try to login with the correct credentials the form gets redirected to login_form.html whereas it should go to dashboard.php page.
Your error is :$user_check. Sugget you to change like below.
$result = $conn->prepare("SELECT * FROM register WHERE email = :user_check");
/*^^^*/
$result->execute(array(":usercheck"=>$user_check));
I also see another error in there: I see an undefined index at
$user_check=$_SESSION['login_user'];
Where does 'login_user' come from?

Categories