encryption/decryption not working in laravel 4 - php

On registration, password is getting saved as intended encrypted but while login, it is throwing a message that password is not matching, because encrypted password is not decrypting while login. Can any one help me out with this ? I want to login successfully with encrypted password.
Here is my controller
<?php
namespace App\Http\Controllers;
use App\Http\Requests;
use Illuminate\Http\Request;
use DB;
use Session;
use Redirect;
use App\User;
use App\Http\Requests\UserRequest as UserRequest;
// use App\Http\Requests\CreateArticleRequest as CreateArticleRequest;
use App\Http\Controllers\Controller;
use Illuminate\View\Middleware\ErrorBinder;
use Illuminate\Support\Facades\Input;
use Hash;
use Crypt;
use Bcrypt;
class ctrl extends Controller
{
/**
* Create a new controller instance.
*
* #return void
*/
public function __construct()
{
$this->middleware('auth');
}
public function registration_function(Request $request)
{
$nam_value = $request->nam;
$email_value = $request->r_email;
$password_value = $request->r_password;
$city_value = $request->city;
$login_email = $request->l_email;
$login_password = $request->l_password;
$bcrypt = new Bcrypt(15);
$password_value = $bcrypt->hash($password_value);
$login_password = $bcrypt->verify($password_value, $login_password);
// $password_value = Crypt::encrypt($password_value);
// $login_password = Crypt::encrypt($password_value);
if($email_value)
{
$valid_user = DB::table('registered')
->where('email',$email_value)
->get();
if($valid_user)
{
return redirect('makelogin_page')->with('status_validate','You are already registered with us, Plaese login, Did you forgot your password ?');
}
else
{
$reg=DB::table('registered')->insert(['name' => $nam_value, 'email' => $email_value, 'password'=>$password_value,'city'=>$city_value]);
return redirect('makelogin_page')->with('status','Registered Successfully');
}
}
else
{
$log_display = DB::table('registered')
->where('email',$login_email)
->where('password',$login_password)
->get();
if($log_display)
{
return redirect('dodofront');
}
else
{
return redirect('makelogin_page')->with('status','Please enter the valid credentials, Did you forgot your password ?');
}
}
}
}
Here is my blade.php page
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags -->
<title>Dashboard | Design Dodo</title>
<!-- Bootstrap -->
<link href="main.css" rel="stylesheet"/>
<script src="bootstrap/jquery.min.js"></script>
<script src="bootstrap/bootstrap.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.6/angular.min.js"></script>
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
</head>
<body>
<br/>
<div class="container">
<h3>New user ?</h3>
<!-- Trigger the modal with a button -->
<button type="button" class="btn btn-info btn-lg" data-toggle="modal" data-target="#registration">Register</button>
<!-- Modal -->
<div class="modal fade" id="registration" role="dialog">
<div class="modal-dialog modal-sm">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">Registration</h4>
</div>
<div class="modal-body">
<p>Please Register Yourself Here</p>
<form role="form" action="registration_page" method="get">
<div class="form-group">
<input type="text" class="form-control" name="nam" placeholder="Your Name Please" style="width:265px;" required/>
</div>
<div class="form-group">
<input type="email" class="form-control" name="r_email" placeholder="Your Email Please" style="width:265px;" required/>
</div>
<div class="form-group">
<input type="password" class="form-control" name="r_password" placeholder="Please enter a password" style="width:265px;" required/>
</div>
<div class="form-group">
<input type="text" class="form-control" name="city" placeholder="Please enter your city" style="width:265px;" required/>
</div>
<div class="form-group">
<input type="submit" class="btn btn-info" value="Register">
</div>
</form>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
<h3>Existing user ?</h3>
<!-- Trigger the modal with a button -->
<button type="button" class="btn btn-info btn-lg" data-toggle="modal" data-target="#login">Login</button>
<!-- Modal -->
<div class="modal fade" id="login" role="dialog">
<div class="modal-dialog modal-sm">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">Login</h4>
</div>
<div class="modal-body">
<p>Please Login Here</p>
<form role="form" action="login_page" method="get">
<div class="form-group">
<input type="email" class="form-control" name="l_email" placeholder="Your Email Please" style="width:265px;" required/>
</div>
<div class="form-group">
<input type="password" class="form-control" name="l_password" placeholder="Please enter a password" style="width:265px;" required/>
</div>
<div>
<input type="submit" class="btn btn-info" value="login">
</div>
</form>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
</div>
</body>
And here is route
<?php
/*
|--------------------------------------------------------------------------
| Application Routes
|--------------------------------------------------------------------------
|
| Here is where you can register all of the routes for an application.
| It's a breeze. Simply tell Laravel the URIs it should respond to
| and give it the controller to call when that URI is requested.
|
*/
Route::get('/', function () {
return view('welcome');
});
Route::auth();
Route::get('/registration_page','ctrl#registration_function');
Route::get('/login_page','ctrl#registration_function');
THis code is throwing an error which is
FatalErrorException in ctrl.php line 40:
Class 'Bcrypt' not found
Please help me to solve this issue

Related

The ajax is not getting the value?

I have the issue when clicking on the register button ,
is not getting the value,
I have made to many researchs i but cant find the answer,
Also i have all the libraries on the menu and
footer pages ,including jquery.
PHP CODE:
<?php
include 'Auth.php';
include 'partials/menu.php';
$usersObj = new Auth();
?>
<h2 class="mt-5 text-center mb-5">View Records
<button data-target="#Registration" data-toggle="modal" class="btn btn-primary" style="float:right;">Add New User</button>
</h2>
<!--Registration Modal-->
<div class="modal" id="Registration">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h3 class="text-dark">Add User Form</h3>
</div>
<div class="modal-body">
<p id="message" class="text-dark"></p>
<form>
<input type="text" class="user form-control my-2" placeholder="User Name" id="us">
<input type="text" class="form-control my-2" placeholder="User Email" id="fn">
<input type="password" class="form-control my-2" placeholder="User Password" id="pw">
</form>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-success" id="btn_register">Register Now</button>
<button type="button" class="btn btn-danger" data-dismiss="modal" id="btn_close">Close</button>
</div>
</div>
</div>
</div>
AJAX CODE:
<?php include 'partials/footer.php'; ?>
<script>
var username = $('#us').val();
var full_name = $('#fn').val();
var password =$('#pw').val();
$(document).on('click','#btn_register',function() {
console.log(username + full_name + password);
})
</script>
You are getting the values before the user enters them, you have to get them when the button is clicked, put your logic in the event handler.
<script>
$(document).on('click','#btn_register',function() {
var username = $('#us').val();
var full_name = $('#fn').val();
var password = $('#pw').val();
console.log(username + full_name + password);
})
</script>

Ajax call error not allowing me to view missing details from a form

I don't know what I've done but I think I have some error in my ajax query. I am expecting to have been able to see the missing details not entered in the signup form. I get 500 server error and undefined $("#signupform") errors.
I have tried searching here and tried inserting various comments into my code to try and see if it helps. I have only been coding for 10 weeks so it's all new to me. I took an online course that promised the earth but has zero support whatsoever.
I think the error may be coming from the connection.php file I have not allowing the code to progress to check out my input fields in the table?
I have these pages hosted on a subdomain here is the link http://welcomer.offyoucode.co.uk/WEBSITES/9.Notes%20App/ in case its easier to figure it out from there.
<!--connect to the database-->
<?php
$link = mysqli_connect("#", "#", "#", "#");
if(mysqli_connect_error()){
die("ERROR: Unable to connect:" . mysqli_connect_error());
echo "<script>window.alert('Hi!')</script>";
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Online Notes</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<link href="styling.css" rel="stylesheet">
<link href="https://fonts.googleapis.com/css?family=Arvo&display=swap" rel="stylesheet">
<style>
</style>
</head>
<body>
<!--navbar-->
<nav role="navigation" class="navbar navbar-custom navbar-fixed-top">
<div class="container-fluid">
<div class="navbar-header">
<a class="navbar-brand">Online Notes</a>
<button type="button" class="navbar-toggle" data-target="#navbarCollapse" data-toggle="collapse">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
</div>
<div class="navbar-collapse collapse" id="navbarCollapse">
<ul class="nav navbar-nav">
<li class="active">Home<span class="caret"></span></li>
<li>Help</li>
<!--<li>Sign-Up</li>-->
<li>Contact Us</li>
</ul>
<ul class="nav navbar-nav navbar-right">
<li>Login</li>
</ul>
</div>
</div>
</nav>
<!--jumbotron with signup button-->
<div class="jumbotron" id="myContainer">
<h1>Online Notes App</h1>
<p>Your notes with you, wherever you go.</p>
<p>Easy to use, protects all your notes!</p>
<button type="button" class="btn btn-lg green signup" data-target="#signupModal" data-toggle="modal">Sign up - Its free</button>
</div>
<!--login form-->
<form method="post" id="loginform">
<div class="modal" id="loginModal" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button class="close" data-dismiss="modal">×</button>
<h4 id="myModalLabel">Login:</h4>
</div>
<div class="modal-body">
<!--login message from php file-->
<div id="loginmessage"></div>
<div class="form-group">
<label for="loginemail" class="sr-only">Email:</label>
<input class="form-control" type="email" name="loginemail" id="loginemail" placeholder="Email" maxlength="50">
</div>
<div class="form-group">
<label for="loginpassword" class="sr-only">Password</label>
<input class="form-control" type="password" name="loginpassword" id="loginpassword" placeholder="Password" maxlength="40">
</div>
<div class="checkbox">
<label>
<input type="checkbox" name="rememberme" id="rememberme">
Remember me
</label>
<a class="pull-right" style="cursor: pointer" data-dismiss="modal" data-target="#forgotpasswordModal" data-toggle="modal">
Forgot Password?
</a>
</div>
</div>
<div class="modal-footer">
<input class="btn green" name="login" type="submit" value="Login">
<button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
<button type="button" class="btn btn-default pull-left" data-dismiss="modal" data-target="signupModal" data-toggle="modal">Register</button>
</div>
</div>
</div>
</div>
</form>
<!--signup form-->
<form method="post" id="signupform">
<div class="modal" id="signupModal" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button class="close" data-dismiss="modal">×</button>
<h4 id="myModalLabel">Sign up today and start using our Online Notes App! </h4>
</div>
<!--signup message from php file-->
<div id="signupmessage"></div>
<div class="modal-body">
<div class="form-group">
<label for="username" class="sr-only">Username:</label>
<input class="form-control" type="text" name="username" id="username" placeholder="Username" maxlength="35">
</div>
<div class="form-group">
<label for="email" class="sr-only">Email:</label>
<input class="form-control" type="email" name="email" id="email" placeholder="Email" maxlength="50">
</div>
<div class="form-group">
<label for="password" class="sr-only">Password:</label>
<input class="form-control" type="password" name="password" id="password" placeholder="Choose a password" maxlength="40">
</div>
<div class="form-group">
<label for="password2" class="sr-only">ConfirmPassword:</label>
<input class="form-control" type="password" name="password2" id="password2" placeholder="Confirm password" maxlength="40">
</div>
</div>
<div class="modal-footer">
<input class="btn green" name="signup" type="submit" value="Sign up">
<button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
</div>
</div>
</div>
</div>
</form>
<!--forgot password form-->
<form method="post" id="forgotpasswordForm">
<div class="modal" id="forgotpasswordModal" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button class="close" data-dismiss="modal">×</button>
<h4 id="myModalLabel">Forgot Password? Enter your Email address:</h4>
</div>
<div class="modal-body">
<!--forgot password message from php file-->
<div id="forgotpasswordMessage"></div>
<div class="form-group">
<label for="forgotpasswordEmail" class="sr-only">Email:</label>
<input class="form-control" type="email" name="forgotpasswordEmail" id="forgotpasswordEmail" placeholder="Email" maxlength="50">
</div>
</div>
<div class="modal-footer">
<input class="btn green" name="login" type="submit" value="Login">
<button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
<button type="button" class="btn btn-default pull-left" data-dismiss="modal" data-target="signupModal" data-toggle="modal">Register</button>
</div>
</div>
</div>
</div>
</form>
<?php
include "footer.php";
?>
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
<script src="index.js"></script>
</body>
</html>
//Ajax Call for the sign up form
//once the form is submitted
$("#signupform").submit(function(event){
//prevent default php processing
event.preventDefault();
//collect user inputs
var datatopost =
$(this).serializeArray();
console.log(datatopost);
//send them to signup.php using ajax
$.ajax({
url: "signup.php",
type: "POST",
data: datatopost,
success: function(data){
if(data){
$("#signupmessage").html(data);
}
},
error: function(){
$("#signupmessage").html("<div class='alert alert-danger'>There was an error with the Ajax Call. Please try again later.</div>");
},
});
// $.post({}).done().fail();
});
//ajax call successful: show error or success message
//ajax call fails: show ajax call error
//ajax call for the login form
//once the form is submitted
//prevent default php processing
//collect user inputs
//send them to login.php using ajax
//if php files return "success": redirect user to notes page
//otherwise show error message
//ajax call fails: show ajax call error
//ajax call for the forgot password form
//once the form is submitted
//prevent default php processing
//collect user inputs
//send them to login.php using ajax
//ajax call successful: show error or success message
//ajax call fails: show ajax call error
<?php
//<!--start session-->
session_start();
include("connection.php");
//<!--check user inputs-->
// <!--define error messages-->
$missingUsername='<p><strong>Please enter a username</strong></p>';
$missingEmail='<p><strong>Please enter an email address</strong></p>';
$invalidEmail='<p><strong>Please enter a valid email address</strong></p>';
$missingPassword='<p><strong>Please enter a password</strong></p>';
$invalidPassword='<p><strong>Your password should be at least 8 characters long and contain at least 1 capital letter and 1 number!</strong></p>';
$differentPassword='<p><strong>Passwords do not match! </strong></p>';
$missingPassword2='<p><strong>Please confirm your password</strong></p>';
$errors = "";
$username = "";
$email = "";
$password = "";
$myFile = "db.json";
$arr_data = array(); //create empty array
// <!--get username, email, password, password2-->
//get username
if(empty($_POST["username"])){
$errors .= $missingUsername;
}else{
$username = filter_var($_POST["username"], FILTER_SANITIZE_STRING);
}
//get email
if(empty($_POST(["email"]))){
$errors .= $missingEmail;
}else{
$email = filter_var($_POST["email"],
FILTER_SANITIZE_EMAIL);
if(!filter_var($email, FILTER_VALIDATE_EMAIL)){
$errors .= $invalidEmail;
}
}
// get passwords
if(empty($_POST["password"])){
$errors .= $missingPassword;
}elseif(!(strlen($_POST["password"])>= 8 and preg_match('/[A-Z]/', $_POST["password"])and
preg_match('/[0-9]/', $_POST["password"])
)
){
$errors .= $invalidPassword;
}else{
$password = filter_var($_POST["password"], FILTER_SANITIZE_STRING);
if(empty($_POST["password2"])){
$errors .= $missingPassword2;
}else{
$password2 = filter_var($_POST["password2"], FILTER_SANITIZE_STRING);
if($password !== $password2){
$errors .= $differentPassword;
}
}
}
// <!--if there are any errors print errors-->
if($errors){
$resultMessage = '' . $errors . '';
echo $resultMessage;
exit;
}
//<!--no errors-->
// <!--prepare variables for the query-->
$username = mysqli_real_escape_string($link, $username);
$email = mysqli_real_escape_string($link, $email);
$password = mysqli_real_escape_string($link, $password);
$password = md5($password);
//128 bits -> 32 characters
// <!--if username exists in the table print error-->
$sql = "SELECT * FROM users WHERE username = '$username'";
mysqli_query($link, $sql);
if($!result){
echo '<div class="alert alert-danger">Error running the query!</div>';
// echo '<div class="alert alert-danger">' . mysqli_error($link) . '</div>';
exit;
}
$results = mysqli_num_rows($results);
if($results){
echo '<div class="alert alert-danger">That username is already in use. Do you want to log in?</div>'; exit;
}
// <!--else-->
// <!--if email exists in the users table print error-->
$sql = "SELECT * FROM users WHERE email = '$email'";
mysqli_query($link, $sql);
if(!$result){
echo '<div class="alert alert-danger">Error running the query!</div>';
exit;
}
$results = mysqli_num_rows($results);
if($results){
echo '<div class="alert alert-danger">That email is already in use. Do you want to log in?</div>'; exit;
}
// <!--else-->
// <!--create a unique activation code-->
$activationKey = bin2hex(openssl_random_pseudo_bytes(16));
//byte: unit of data = 8 bits
//bit: 0 or 1
//16 bytes = 16*8 = 128 bits
//2*2*2*2*2*2....*2
//16*16.......*16
//32 characters
// <!--insert user details and activation code in the users table-->
$sql = "INSERT INTO users ('username', 'email', 'password', 'activation') VALUES ('$username', '$email', '$password', '$activationKey')";
mysqli_query($link, $sql);
if(!$result){
echo '<div class="alert alert-danger">There was an error inserting the user details into the database</div>';exit;
}
// <!--send the user an email with a link to activate.php with their email and activation code-->
$message = "Please click on this link to activate your account:\n\n";
$message = "http://https://welcomer.offyoucode.co.uk/WEBSITES/9.Notes%20App/activate.php?email=" . urlencode($email) . "&key=$activationKey"; if(mail($email, 'Confirm your Registration', $message, 'From:'.'onlinenotes#gmail.com')){
echo '<div class="alert alert-success">Thank you for registering. A confirmation email address has been sent to $email. Please click on the activation link to activate your account.</div>';
}
?>
Solved! On line 34 in signup.php extra brackets around email input. Solved using ERROR_REPORTING(E_ALL); ini_set('display_errors', 1) to narrow down my fault.

How to redirect the page based on login success and using single form in multiple condition

Here I have two button one is login button and second one is go to rent.php .my requirement is if i am clicking login button means i want to show login form in modal,after that user enter their credential and also succfully logged in means i want to redirect the home page(index.php). Now i coming to main point suppose i am clicking go to rent.php means ,First i have to check this user is already logged in or not , suppose logged in means directly i have to redirect the rent.php.Suppose he is not yet logged means i have to show the login form in modal after that user will enter the email & password suppose successfully logged in means now i have to redirect the rent.php ,How can do this please any one update my answer.
function checkLogin(argument) {
$('#modalForm').modal('show');
}
// Login Check
$(document).ready(function(){
$("#loginbtn").click(function(e){
e.preventDefault();
var loginEmail = $('#loginEmail').val();
var password = $('#password').val();
$.ajax({
type:'POST',
url :"test_session.php",
data: {email: loginEmail, password: password},
success: function(response) {
if(response['status'] =='success'){
console.log(response);
window.location.href = "index.php";
}
else{
$("#update_Failed").show();
}
},
error:function(exception){
alert('Exeption:'+exception);
}
});
});
});
<?php
session_start();
?>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script type="text/javascript" src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
<!-- Modal -->
<div class="modal fade" id="modalForm" role="dialog">
<div class="modal-dialog">
<div class="modal-content">
<!-- Modal Header -->
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">
<span aria-hidden="true">×</span>
<span class="sr-only">Close</span>
</button>
<h4 class="modal-title" id="myModalLabel">Login Form</h4>
</div>
<!-- Modal Body -->
<div class="modal-body">
<p class="statusMsg"></p>
<form id="loginForm">
<div class="form-group" id="loginmobile">
<label for="email">Email address or Mobile:</label>
<input type="text" class="form-control stl" name="email" id="loginEmail" required="" data-msg-required="Please Enter Valid Email or Mobile no." aria-required="true">
</div>
<div class="form-group" id="loginmobile1" style="display:none">
<label for="email"> Mobile:</label>
<input type="number" class="form-control stl" name="email1" id="loginMobile" required="" data-msg-required="Please Enter Mobile no." aria-required="true">
</div>
<div class="form-group" id="otppwd">
<!-- <label for="pwd">Password Or OTP:</label> -->
<label for="pwd">Password :</label>
<input type="password" class="form-control stl" name="password" id="password" required="" data-msg-required="Please Enter Password" aria-required="true">
</div>
</br>
<div style="text-align: center;">
<button type="submit" id="loginbtn" class="btn btn-default contact_us_btn rentListLogin" >SUBMIT</button>
</div>
<div class="alert alert-danger" id="update_Failed" style="display:none;margin-top: 5%">
<br>
<strong>Username or Password is Incorrect</strong>
</div><br>
</form>
</div>
</div>
</div>
</div>
<a type="button" class="btn" onclick="checkLogin()">Login</a>
<?php
if(!empty($_SESSION['userName'])){
?>
Go to rent.php
<?php
}
else
{
?>
<a type="button" class="btn btn-default" onclick="checkLogin()">go to rent.php</a>
<?php
}
?>
You can check set a variable to identify if the user is logged in or not. Then using that decide whether to show log in modal or go to rent.php page.
<script>
<?php
if(!empty($_SESSION['userName'])){
var loggedIn = 1;
}
else {
var loggedIn = 0;
}
?>
function checkLogin(argument) {
if(loggedIn === 0) {
$('#modalForm').modal('show');
}
else {
window.location.href = "rent.php";
}
}
// Login Check
</script>

Bootstrap Modal Form wont work with CodeIgniter

I'm very new to codeigniter.
here's my problem. when i load my MODAL LOGIN form and i press the submit button. It wouldn't do anything. What i would like to happen is just to go to these 4 views (student,instructor,adviser,admin) pages. I don't want to pass values for now. Focus only on switching views.
index.php (the modal part)
<!-- Modal Log in Form-->
<div class="modal fade" id="myModal" role="dialog">
<div class="modal-dialog">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header" style="padding:35px 50px;">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4><span class="glyphicon glyphicon-lock"></span> Login</h4>
</div>
<div class="modal-body" style="padding:40px 50px;">
<form action="" method="POST" id="form" role="form">
<div class="form-group">
<label for="usrname"><span class="glyphicon glyphicon-user"></span> Username</label>
<input type="text" class="form-control" id="usrname" placeholder="Enter id no.">
</div>
<div class="form-group">
<label for="psw"><span class="glyphicon glyphicon-eye-open"></span> Password</label>
<input type="text" class="form-control" id="psw" placeholder="Enter password">
</div>
<div class="checkbox">
<label><input type="checkbox" value="" checked>Remember me</label>
</div>
<input type="Submit" value="LOG IN" class="btn btn-success btn-block">
</form>
</div>
<div class="modal-footer">
<button type="submit" class="btn btn-danger btn-default pull-left" data-dismiss="modal"><span class="glyphicon glyphicon-remove"></span> Cancel</button>
<p>Not a member? Sign Up</p>
<p>Forgot Password?</p>
</div>
</div>
</div>
I didn't put anything on that action="". because of this.(to be dynamic) when the user want to sign in as student he/she go to student page. or as admin he/she go to admin page. and etc.
freelancer.js
$(document).ready(function(){
$("#myBtn").click(function(){
$("#myModal").modal();
document.getElementById("form").action ="<?php echo base_url().'index.php/logbooks/studentsignin'?>";
});
});
$(document).ready(function(){
$("#myBtn2").click(function(){
$("#myModal").modal();
document.getElementById("form").action ="<?php echo base_url().'index.php/logbooks/instructorsignin'?>";
});
});
$(document).ready(function(){
$("#myBtn3").click(function(){
$("#myModal").modal();
document.getElementById("form").action ="<?php echo base_url().'index.php/logbooks/advisersignin'?>";
});
});
$(document).ready(function(){
$("#myBtn4").click(function(){
$("#myModal").modal();
document.getElementById("form").action ="<?php echo base_url().'index.php/logbooks/adminsignin'?>";
});
});
logbooks.php (from the controller)
class logbooks extends CI_Controller{
function __construct(){
parent::__construct();
}
function studentsignin(){
$this->load->view('student_view');
}
function instructorsignin(){
$this->load->view('instructor_view');
}
function advisersignin(){
$this->load->view('adviser_view');
}
function adminsignin(){
$this->load->view('admin_view');
}
}
but if i just input the relative path for example http://localhost/WEB/index.php/logbooks/studentsignin in the browser. I can view the student page.
PLEASE help. I don't know how to solve this problem.
Change your submit button to just button.
In click event after set "form action" you can use this too:
$(document).ready(function(){
$("#myBtn4").click(function(){
$("#myModal").modal();
$("#form").attr('action',"<?php echo base_url('index.php/logbooks/adminsignin')?> ";
});
$("#form").submit();
});
The point is use $("#form").submit();
I hope It Work For you.

HTML Bootstrap PHP connection error

I want to write log in code for my index.html ' s form and i wrote a giris-yap.php file which is at below. i cant access my php file browser get alert only like localhost is waiting .
i tried to put action method in my form's submit button but it was not usefull.
giris-yap.php
<?php
require "connect.inc.php";
require "core.inc.php";
if(isset($_POST['exampleInputEmail1']) && isset($_POST['exampleInputPassword1']) ){
$mail=$_POST['exampleInputEmail1'];
$pass=$_POST['exampleInputPassword1'];
$password=md5($pass);
if($query_run=mysql_query("SELECT * FROM `users` WHERE `e-mail`= '".mysql_real_escape_string($mail)."' AND `sifre`='".mysql_real_escape_string($password)." ' ")){
$query_num_rows = mysql_num_rows($query_run);
if($query_num_rows==0){
echo 'Invalid';
}
else if($query_num_rows!=0){
$ad=mysql_result($query_run,0,'Ad');
$_SESSION['ad']=$ad;
$usersurname=mysql_result($query_run,0,'SoyAd');
$_SESSION['usersurname']=$usersurname;
$username=mysql_result($query_run,0,'e-mail');
$_SESSION['username']=$username;
header('Location: index.html');
}
}
else{
echo mysql_error();
}
}
else{echo 'error';}
/**
* Created by PhpStorm.
* User: bilsay
* Date: 21.05.2015
* Time: 10:35
*/
?>
index.html :
<div class="modal fade" id="login-modal-box" role="dialog" aria-labelledby="gridSystemModalLabel" aria-hidden="true">
<form action="#giris-kontrol" method="POST">
<div class="modal-dialog user-login-box-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
<h4 class="modal-title" id="gridSystemModalLabel">Kullanıcı Giriş Paneli</h4>
</div>
<div class="modal-body">
<div class="container-fluid">
<div class="row">
<div class="form-group">
<label for="exampleInputEmail1">Eposta Adresiniz</label>
<input type="email" class="form-control" id="exampleInputEmail1" placeholder="Enter email">
</div>
<div class="form-group">
<label for="exampleInputPassword1">Şifre</label>
<input type="password" class="form-control" id="exampleInputPassword1" placeholder="Password">
</div>
<div class="checkbox">
<label>
<input type="checkbox"> Beni hatırla
</label>
</div>
</div>
</div>
</div>
<div class="modal-footer">
<button type="submit" class="btn btn-default" value="Giriş">Giriş</button>
</div>
</div><!-- /.modal-content -->
</div><!-- /.modal-dialog -->
</form>
</div><!-- /.modal -->
cant access my php file
You need to update your action as described in the other answer: https://stackoverflow.com/a/30377560/482256.
Then, note that this code here:
require connect.inc.php;
require core.inc.php;
Is the equivalent of doing this:
require 'connectincphp';
require 'coreincphp';
When you don't use quotes, PHP looks for constants, and when it doesn't find those it will assume the string, so connect becomes "connect". The period concatenates, so it combines "connect" with "inc" and you get "connectinc", etc.
The require should be causing a 500 error...and possibly an empty page depending on what your error output settings are.
Your code translated to PDO and BCrypt, because I just can't "fix" code and leave it insecure:
if(isset($_POST['exampleInputEmail1']) && isset($_POST['exampleInputPassword1']) ){
$pdo = new \PDO('mysql:dbname=dbName;host=localhost','username','password');
$mail = $_POST['exampleInputEmail1'];
$pass = $_POST['exampleInputPassword1'];
$userSql = $pdo->prepare("SELECT * FROM `users` WHERE `e-mail`=:email");
$userSql->execute(array('email'=>$mail));
$userData = $userSql->fetch(\PDO::FETCH_ASSOC);
if( $userData !== false && BCrypt::isValidPassword($pass, $userData['sifre']) ) {
$_SESSION['ad'] = $userData;
$_SESSION['usersurname'] = $userData['SoyAd'];
$_SESSION['username'] = $userData['username'];
header('Location: index.html');
}
else {
die("You have entered an invalid username or password");
}
}
else{
die("Username and Password must be submitted");
}
And your modified HTML. I fixed the action, turned your button into a real submit button, and added the name= attributes to your inputs:
<div class="modal fade" id="login-modal-box" role="dialog" aria-labelledby="gridSystemModalLabel" aria-hidden="true">
<form action="giris-yap.php" method="POST">
<div class="modal-dialog user-login-box-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
<h4 class="modal-title" id="gridSystemModalLabel">Kullanıcı Giriş Paneli</h4>
</div>
<div class="modal-body">
<div class="container-fluid">
<div class="row">
<div class="form-group">
<label for="exampleInputEmail1">Eposta Adresiniz</label>
<input type="email" class="form-control" id="exampleInputEmail1" name="exampleInputEmail1" placeholder="Enter email">
</div>
<div class="form-group">
<label for="exampleInputPassword1">Şifre</label>
<input type="password" class="form-control" id="exampleInputPassword1" name="exampleInputPassword1" placeholder="Password">
</div>
<div class="checkbox">
<label>
<input type="checkbox"> Beni hatırla
</label>
</div>
</div>
</div>
</div>
<div class="modal-footer">
<button type="submit" value="1" class="btn btn-primary" id="submit"> Giriş Yap</button>
</div>
</div>
<!-- /.modal-content -->
</div>
<!-- /.modal-dialog -->
</form>
</div><!-- /.modal -->
And the BCrypt class you will need. However, use password_hash and password_verify if you have PHP >= 5.5.
class BCrypt {
public static function hash( $password, $cost=12 ) {
$base64 = base64_encode(openssl_random_pseudo_bytes(17));
$salt = str_replace('+','.',substr($base64,0,22));
$cost = str_pad($cost,2,'0',STR_PAD_LEFT);
$algo = version_compare(phpversion(),'5.3.7') >= 0 ? '2y' : '2a';
$prefix = "\${$algo}\${$cost}\${$salt}";
return crypt($password, $prefix);
}
public static function isValidPassword( $password, $storedHash ) {
$newHash = crypt( $password, $storedHash );
return self::areHashesEqual($newHash,$storedHash);
}
private static function areHashesEqual( $hash1, $hash2 ) {
$length1 = strlen($hash1);
$length2 = strlen($hash2);
$diff = $length1 ^ $length2;
for($i = 0; $i < $length1 && $i < $length2; $i++) {
$diff |= ord($hash1[$i]) ^ ord($hash2[$i]);
}
return $diff === 0;
}
}
change action..
<form action="giris-yap.php" method="POST">
then change a link in your modal-footer
<button type="submit" value="Giriş Yap" class="btn btn-primary" id="submit" />

Categories