Programming & Scripting
Force Composer to use a specific PHP version
to Force the composer to use a specific PHP version you can add this script to the composer.josn
"config": { "optimize-autoloader": true, "prepend-autoloader": false, "platform": { "php": "8.0.13" } },
or you can use the command line to add it
cd /path/to/project/dir ls -l composer.json composer config platform.php {PHP_VERSION_HERE} composer config platform.php 8.x.x
Password Protect CMS Page
Password Protect CMS Page
to protect any page with a password in an easy way you can add this script to the top of the page
<script type="text/javascript">
var password = 'password123'
var retVal = prompt("Enter your password : ", "your password here");
if(retVal !== password)
{
location.assign('');
}
</script>
Sort a multidimensional array in PHP?
the most challenge question in the past was soring a multidimensional arrays in PHP, now lets make it in an easy way
You have your array like this
$array = [];
$array[] = array('name' => 'name1', 'age' => 85);
$array[] = array('name' => 'name2', 'age' => 15);
$array[] = array('name' => 'name3', 'age' => 10);
$array[] = array('name' => 'name4', 'age' => 59);
the easy way is to use these command
Clone repository with the same server user
Clone repository with the same server user
- create new php file with this name it.php
<?php exec('git clone https://@github.com/{username}/{repository}.git');
after that go to the command line and execute this command
php it.php
Exclude Property from json during Serialization in newtonsoft
This is a tiny helper class to exclude a property from Json Serialization.
After you add newtonsoft as reference you need to create this class
Merge text files using PHP code
the easiest and best way is to use the below code
<?php $txt1 = file_get_contents('1.txt'); $txt1 .= "\n" . file_get_contents('2.txt'); $txt1 .= "\n" . file_get_contents('3.txt'); $fp = fopen('newText.txt', 'w'); if(!$fp) die('Could not create / open text file for writing.'); if(fwrite($fp, $txt1) === false) die('Could not write to text file.'); echo 'Text files have been merged.'; ?>
Javascript add/update parameter to url string
One of the best code I saw is the below code which is a good way to change the URL without refresh the page, here is the code
use file_get_contents with POST and GET
usually in PHP we are using cURL to execute GET/ POST/ DELETE/ methods but now I will mention here how to use it in file_get_contents
PhpStorm disable auto-save
How to disable auto-save:
- Go to File > Settings (Ctrl+Alt+S).
- Go to Appearance & Behavior > System Settings.
- unchecked: the both fields
- Save files on frame deactivation
- Save files automatically if application is idle for x sec.
- Go to Editor > General > Editor Tabs
- Put a checkmark on
- "Mark modified files with asterisk"
Click Apply > OK.
That's it.
How to detect a mobile device in jQuery?
You can use this script to detect the screen size as you want