I was writing a a very simple script to check if a visitor was a bot. Therefore I made an array of words used by bots, and soon found out that PHP got a function to check a string in an array, but not an array in a string. So I wrote a simple function, maybe someone else needs it?
// PHP Function to check if any value in an array is in a string
//
function isin_string($string, $array) {
foreach ($array as $value) {
if (!stristr($string, $value) === FALSE) {
// value is found
return true;
} else {
// value not found
return false;
}
}
}
Or in a simpler way just implode() the array and use strrpos()
function string_in_array($string, $array) {
$array = implode(" ", $array);
if strrpos($array, $string) {
// String is found
return true;
} else {
// String was NOT found
return false;
}
}
I have a email server using IMAP, and as you probably know you can mark a mail as deleted but it will still be on the server occupying space. There are usually a option to purge them, but most users doesn’t care..
To solve this problem, I will notify all my users that mails will automatically be deleted after 14 days, and set up a cron job for the task.
To find and delete the mails I’ll use the following command
find /var/vmail/ -name '*,ST' -ctime +14 | xargs rm -f
Using crontab -e I will make it run every day at 15:00. So this is how my crontab looks after adding the line
# m h dom mon dow command
0 15 * * * find /var/vmail/ -name '*,ST' -ctime +14 | xargs rm -f
If you have one or more servers you log in to very often like my self, you might get a bit bored of typing the same passwords when connecting through SSH. The solution is really simple, and as an bonus it’s even more secure than regular login. The following steps assumes you are using Linux.
-
On you workstation you will have to make a directory and generate a key pair. (This is only needed once, you can use the same key on more servers)
mkdir ~/.ssh
ssh-keygen -t rsa
This will create two files in your .ssh folder named id_rsa and id_rsa.pub. The last one is the public file and is what we are going to store on the server.
The other file is your private key. Never show, give away or keep this file on a public computer.
-
Copy the public file id_rsa.pub to the server using scp (secure copy).
scp .ssh/id_rsa.pub user@domain.tld:/home/user/
-
Then we log in to the server and setup everything with the right permissions.
ssh user@domain.tld
-
Create a .ssh directory if it doesn’t exist. And copy the content of the public key file into the authorized_keys. And delete the uploaded public file. The reason I copy the content and not the file itself is because you may have other keys stored in there.
mkdir -p ~/.ssh
cat id_rsa.pub >> ~/.ssh/authorized_keys
rm id_rsa.pub
And set the right permissions on the file and folder. (Remember to change user:user to reflect your username!)
chown -R user:user ~/.ssh
chmod 700 ~/.ssh
chmod 600 ~/.ssh/authorized_keys
-
Now logout from the server using exitand try logging in again
And remember that the file id_rsa is your private key. Never show, give away or keep this file on a public computer. I cannot stress this enough!
I just wanted to post a simple rewrite rule for all the people that is using the WordPress plugin Download Monitor on Nginx.
# Download folder rewrite
#
location /downloads/ {
rewrite ^/downloads/(.*)$ /wp-content/plugins/download-monitor/download.php?id=$1 last;
}
The code assumes you have set the “Custom download URL” to downloads in the plugin configuration.