View Questions
How to find out if you’re using HTTPS without $_SERVER[‘HTTPS’]
$isSecure = false;
if (isset($_SERVER[‘HTTPS’]) && $_SERVER[‘HTTPS’] == ‘on’) {
$isSecure = true;
}
elseif (!empty($_SERVER[‘HTTP_X_FORWARDED_PROTO’]) && $_SERVER[‘HTTP_X_FORWARDED_PROTO’] == ‘https’ || !empty($_SERVER[‘HTTP_X_FORWARDED_SSL’]) && $_SERVER[‘HTTP_X_FORWARDED_SSL’] == ‘on’) {
$isSecure = true;
}
$REQUEST_PROTOCOL = $isSecure ? ‘https://’ : ‘http://’;
$root=$REQUEST_PROTOCOL.$_SERVER[‘HTTP_HOST’];
I’ve seen many tutorials online that says you need to check $_SERVER[‘HTTPS’] if the server is connection is secured with HTTPS. My problem is that on some of the servers I use, $_SERVER[‘HTTPS’] is an undefined variable that results in an error. Is there another variable I can check that should always be defined?
How to setup env file on docker container get in PHP, compose.yml environment variable to php environment variable???
#cat .env
MYSQL_ROOT_PASSWORD=RootPassword
MYSQL_USER=gnf_user
MYSQL_PASSWORD=UserPassword
MYSQL_DATABASE=gnf_noah
MYSQL_HOST=db
Try using env_file in the compose file to reference your .en
├── compose.yml
├── Dockerfile
├── .env
└── index.php
compose.yml
—————-
version: “3.2”
services:
www:
build: .
ports:
– “30001:80”
– “30443:443”
env_file:
– .env
==========================
.env
—————-
MYSQL_ROOT_PASSWORD=RootPassword
MYSQL_USER=gnf_user
MYSQL_PASSWORD=UserPassword
MYSQL_DATABASE=gnf_noah
+++++++++++++++++++++
RUN
—–
docker compose up –build
========================
index.php
————
<?php
$db_host = getenv(‘MYSQL_HOST’, true) ?: getenv(‘MYSQL_HOST’);
$db_name = getenv(‘MYSQL_DATABASE’, true) ?: getenv(‘MYSQL_DATABASE’);
$db_user = getenv(‘MYSQL_USER’, true) ?: getenv(‘MYSQL_USER’);
$db_pwd = getenv(‘MYSQL_PASSWORD’, true) ?: getenv(‘MYSQL_PASSWORD’);
echo “db_host: {$db_host}<br>”;
echo “db_name: {$db_name}<br>”;
echo “db_user: {$db_user}<br>”;
echo “db_pwd: {$db_pwd}<br>”;
?>
HOW TO CREATE CUSTOMS HELPER FUNCTION IN LARAVEL 11?
Hello developer, In this guide, I’ll walk you through the process of creating custom helper functions in Laravel 11.
Laravel is an amazing PHP framework that simplifies web development, and creating custom helper functions can make your coding experience even smoother.
In this article, we’ll create a custom helper function in laravel. So, you can easily use the anywhere in the laravel 11 application.
Step 1: Install Laravel 11
In this step, we’ll install the laravel 11 application using the following command.
composer create-project laravel/laravel laravel-11-example
Step 2: Create helpers.php File
After that, we’ll create a helpers.php file and create functions to that file.
app/Helpers/helpers.php
<?php
use Carbon\Carbon;
/**
* Write code on Method
*
* @return response()
*/
if (! function_exists('convertYmdToMdy')) {
function convertYmdToMdy($date)
{
return Carbon::createFromFormat('Y-m-d', $date)->format('m-d-Y');
}
}
/**
* Write code on Method
*
* @return response()
*/
if (! function_exists('convertMdyToYmd')) {
function convertMdyToYmd($date)
{
return Carbon::createFromFormat('m-d-Y', $date)->format('Y-m-d');
}
}
Step 3: Register File Path In composer.json File
In this step, we’ll specify the path to the helpers file. To do this, open the composer.json
file and add the following code snippet.
composer.json
...
"autoload": {
"psr-4": {
"App\\": "app/",
"Database\\Factories\\": "database/factories/",
"Database\\Seeders\\": "database/seeders/"
},
"files": [
"app/Helpers/helpers.php"
]
},
...
Then, run the following command to load the helper.php file.
composer dump-autoload
Step 4: Add Route
Now, we’ll define the routes into the web.php file.
routes/web.php
<?php
use Illuminate\Support\Facades\Route;
Route::get('date-convert', function(){
$mdY = convertYmdToMdy('2024-03-27');
var_dump("Converted into 'MDY': " . $mdY);
$ymd = convertMdyToYmd('03-27-2024');
var_dump("Converted into 'YMD': " . $ymd);
});
Step 5: Run the Laravel App
Then, run the laravel application using the following command.
php artisan serve
Laravel 11 Cron Job Task Scheduling Example
.
Hello developer, we’ll learn about laravel 11 cron job task scheduling. Laravel’s command scheduler offers a fresh approach to managing scheduled tasks on your server.
Task scheduling is typically defined in your application’s routes/console.php
file.
Cron Job Task Scheduling in Laravel 11
Step 1: Install Laravel 11
To install Laravel 11, execute the following command.
composer create-project laravel/laravel example-app
Step 2: Create a Command
To create a new custom command that will be executed with task scheduling using a cron job, run the following command.
php artisan make:command TestCron --command=test:cron
app/Console/Commands/TestCron.php
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Http;
use App\Models\User;
class TestCron extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'test:cron';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Command description';
/**
* Execute the console command.
*/
public function handle()
{
info("Cron Job running at ". now());
$response = Http::get('https://jsonplaceholder.typicode.com/users');
$users = $response->json();
if (!empty($users)) {
foreach ($users as $key => $user) {
if(!User::where('email', $user['email'])->exists() ){
User::create([
'name' => $user['name'],
'email' => $user['email'],
'password' => bcrypt('123456789')
]);
}
}
}
}
}
Step 3: Register Task Scheduler
In this step, we’ll define our commands in the console.php file along with the scheduled time for running each command. We’ll use functions like ->daily()
, ->hourly()
, etc.
routes/console.php
<?php
use Illuminate\Support\Facades\Schedule;
Schedule::command('test:cron')->everyFiveMinutes();
Step 4: Run Scheduler Command
Now, we’ll run the custom create command using the following laravel artisan command.
php artisan schedule:run
After running, the above command you will see an output like this.
storage/logs/laravel.php
[2024-04-10 23:45:03] local.INFO: Cron Job running at 2024-04-10 23:45:03
[2024-04-10 23:50:05] local.INFO: Cron Job running at 2024-04-10 23:50:05
[2024-04-10 23:55:04] local.INFO: Cron Job running at 2024-04-10 23:45:04
To view an overview of your scheduled tasks, use the schedule:list
Artisan command.
php artisan schedule:list
Laravel 11 Cron Job Setup on Server
In this step, we’ll set up a cron job command on the server. If you’re using Ubuntu Server, crontab is likely already installed. Run the command below to add a new entry for the cron job.
crontab -e
* * * * * cd /path-to-your-project & php artisan schedule:run >> /dev/null 2>&1
In this article, we’ll see how to create a cron job scheduler in laravel 11 and also see how to create a command in laravel 11. You set a cron job for minutes, hourly, daily, weekly, etc. as per your requirements.
[quiz-cat id="5147"]