Thursday, 25 October 2018

How to add google captcha in laravel


First, install anhskohbo/no-captcha Package for Google reCaptcha code. Using this Package, we can generate reCaptcha code for our Application.

composer require anhskohbo/no-captcha

Type below command to Publish Config File
php artisan vendor:publish --provider="Anhskohbo\NoCaptcha\NoCaptchaServiceProvider"


In Env file add these lines

NOCAPTCHA_SECRET=6LfAxnYUAAAAAMFIDoHDyukSi4yHcYZp8q5WdB2m
NOCAPTCHA_SITEKEY=6LfAxnYUAAAAAH5JZ5xlK_iTjjkbdupMuvKeszYZ

Add this lines to your register or login page.

<div class="row">
          <div class="col-md-4"></div>
          <div class="form-group col-md-4">
          <label for="ReCaptcha">Recaptcha:</label>
          {!! NoCaptcha::renderJs() !!}
          {!! NoCaptcha::display() !!}
            </div>
        </div>
 
Add in LoginContorller.php 
protected function validateLogin(Request $request)
    {   
         $this->validate($request,[
            'email' => ['required''string''email'],
            'password' => ['required''string'],
            'g-recaptcha-response' => ['required','captcha'],
        ]);
    }





Friday, 19 October 2018

How To Protect The .ENV File In Laravel From Public Access

Put this code in .htaccess to hide from access

     <Files ~ "\.(git|env|json|config.js|md|gitignore|gitattributes|lock|example)$">
        Order allow,deny
        Deny from all
    </Files>
    <Files ~ "(artisan)$">
        Order allow,deny
        Deny from all
    </Files>

How to hide .env passwords in Laravel when error occurs

Add this below line in  config/app.php


$envKeys = [];
$serverKeys = [];
$cookieKeys = [];
foreach ( $_ENV as $key => $value ) { if(is_string($value)) $envKeys[] = $key; }
foreach ( $_SERVER as $key => $value ) { if(is_string($value)) $serverKeys[] = $key; }
foreach ( $_COOKIE as $key => $value ) { if(is_string($value)) $cookieKeys[] = $key; }

return [

    // ...

    'debug_blacklist' => [
        '_COOKIE'   => $cookieKeys,
        '_SERVER'   => $serverKeys,
        '_ENV'      => $envKeys,
    ],

Monday, 8 October 2018

Database and files system backup

Database and files system backup

https://docs.spatie.be/laravel-backup/v5/installation-and-setup

composer require spatie/laravel-backup

php artisan vendor:publish --provider="Spatie\Backup\BackupServiceProvider"
 
In your config/database.php file, edit the mysql database config and add:

  'dump' => [
               'dump_binary_path' => 'C:/xampp/mysql/bin/', // only the path, so without `mysqldump` or `pg_dump`
               'use_single_transaction',
               'timeout' => 60 * 5, // 5 minute timeout
            ],

Create a folder in storage\app  site-backup.
 
Open config/backup.php file, 
'name' => 'site-backup',//env('APP_NAME', 'laravel-backup'), 

php artisan backup:run

Server up and down (Maintenance) Mode

Create a route

Route::get('/maintenance_down', 'OptimizeController@maintenance_down');
Route::get('/maintenance_up', 'OptimizeController@maintenance_up');


public function maintenance_down()
    { 
        $ip = \Request::ip();
          Artisan::call('down',['--allow'=>$ip]);
        return back();
    }   
    public function maintenance_up()
    {         
          Artisan::call('up');
        return back();
    }   

Database optimize

Create a route

Route::get('/database-optimize/','OptimizeController@database_optimize');

Add in a controller
 
use DB
public function database_optimize()
    {
        $users = DB::select('OPTIMIZE TABLE `users`');
        $orders = DB::select('OPTIMIZE TABLE `orders`');
        $orders_details = DB::select('OPTIMIZE TABLE `orders_details`');       
        $products = DB::select('OPTIMIZE TABLE `products`');
        return back();
    }

Optimize site

Create a route

Route::get('/site-optimize/','OptimizeController@site_optimize');

Add in a controller

public function site_optimize()
    {        
        Artisan::call('config:clear');
        Artisan::call('cache:clear');
        Artisan::call('view:clear');
        Artisan::call('route:clear');
        Artisan::call('config:cache');
        return back();
    }

How to store IP address of user after login?

How to store IP address of user after login?

https://packalyst.com/packages/package/yadahan/laravel-authentication-log

Follow below url to store IP address of s a user.

composer require yadahan/laravel-authentication-log

php artisan vendor:publish --provider="Yadahan\AuthenticationLog\
AuthenticationLogServiceProvider"

php artisan migrate

App\user.php
Add below lines

use Illuminate\Notifications\Notifiable;
use Yadahan\AuthenticationLog\AuthenticationLogable;
use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable
{
    use Notifiable, AuthenticationLogable;
}

 


CORS

To use CORS follow below url details;



https://packalyst.com/packages/package/barryvdh/laravel-cors

How to set Sweet Alert.

To use sweet alert follow below url details;


https://packalyst.com/packages/package/uxweb/sweet-alert


List of online users

Follow these Steps

1. In route file.

Route::get('/useronline', 'HomeController@useronline')->name('useronline');

2. In HomeController

<?php

namespace App\Http\Controllers;
use App\user;
use Illuminate\Http\Request;

class HomeController extends Controller
{
   public function __construct(){ $this->middleware('auth'); }
      
    public function useronline()
    {
        $users = User::all();
        return view('useronline',compact('users'));
    }

public function isOnline()
    {
        return Cache::has('user-is-online-'. $this->id);
    }
}

3. View: useronline.blade.php

@extends('layouts.app')

@section('content')
<div class="container">
  <div class="row justify-content-center">
    <div class="col-md-8">
      <div class="card">
        <div class="card-header">Online users</div>
        <div class="card-body">
          <table class="table">
            <thead>
              <tr>
                <th>Name</th>
                <th>Email</th>
                <th>Status</th>
              </tr>
            </thead>
            <tbody>
           
            @foreach ($users as $user)
            <tr>
              <td>{{ $user->name }}</td>
              <td>{{ $user->email }}</td>
              <td> @if ($user->isOnline())
                <li class="text-success"> Online </li>
                @else
                <li class="text-muted"> Offline </li>
                @endif </td>
            </tr>
            @endforeach
            </tbody>
           
          </table>
        </div>
      </div>
    </div>
  </div>
</div>
@endsection

Machine Learning - Potato Leaf Disease Prediction

Step 1: import numpy as np import pandas as pd import splitfolders import matplotlib.pyplot as plt import tensorflow as tf from tensorflow i...