Categories
Uncategorized

How do I use the ternary operator ( ? : ) in PHP as a shortcut for “if / else”?

PHP 5+

(condition) ? /* value to return if condition is true */ 
            : /* value to return if condition is false */ ;

syntax is not a “shorthand if” operator (the ? is called the conditional operator) because you cannot execute code in the same manner as if you did:

if (condition) {
    /* condition is true, do something like echo */
}
else {
    /* condition is false, do something else */
}

PHP 7+

As of PHP 7, this task can be performed simply by using the Null coalescing operator like this :

 <?php
// Fetches the value of $_GET['user'] and returns 'nobody'
// if it does not exist.
$username = $_GET['user'] ?? 'nobody';
// This is equivalent to:
$username = isset($_GET['user']) ? $_GET['user'] : 'nobody';

// Coalescing can be chained: this will return the first
// defined value out of $_GET['user'], $_POST['user'], and
// 'nobody'.
$username = $_GET['user'] ?? $_POST['user'] ?? 'nobody';
?> 
Categories
Uncategorized

ftp connection via Midnight Commander

Midnight Commander has ability to connect to ftp in a pane, so it’s possible to use it to connect to a remote FTP server.

Go to Shell, execute mc and press F9 to call the menu. Select Left/Right window, select FTP Link

Enter FTP server details in one of the following formats:

username:password@host           #for non-anonymous login;
host                             #for anonymous login;
!username:password@host          #for servers behind the firewall, through proxy servers;
username:password@host:port      #for servers using non std port;
username:password@host/directory #to go to specific directory. 

Once you’ve connected to your remote FTP server, you can copy any files or folders between the panes in your Midnight Commander and any directory on your remote FTP server.

Categories
Uncategorized

How to delete emails from qmail queue on H-Sphere

First check the status of your queue:

# /var/qmail/bin/qmail-qstat
messages in queue: 6751
messages in queue but not yet preprocessed: 56

After you find and stop the spammer, you now have a bunch of email on your QMail queue. Here’s how to remove it

take a “user@domain.com” that happens in emails from the spammer (usually the sender) and run this command:

grep -R user@domain.com /var/qmail/queue/mess/* | awk -F”:” ‘{print “rm -f ” $1}’ | sh

make sure the quotes and single quotes are straight, not round upon copy/paste

That will delete the emails that contain that user@domain.com

but it would leave behind various indexes and later on your users would start receiving bouncebacks for emails they’ve never sent!

BETTER USE:
qmHandle/qmhandle-1.3.2/qmHandle -Fuser@domain.com

this removes not only messages, but all the mentioning of those messages from everywhere. (sasa4uk)

Categories
Uncategorized

Hsphere file paths

Mysql:
======
Socket file: /var/lib/mysql/mysql.sock
Database file: /var/lib/mysql
mysql password file: /var/lib/mysql/.my.cnf
Error log: /var/log/mysql/error.log

Ftp:
=====

Proftpd is default with hsphere.

Configuration file: /hsphere/shared/config/ftpd/proftpd.conf
Log file: /hsphere/local/var/proftpd/xferlog

Php:
=====

Categories
programming

Fix PHP Fatal error: Cannot redeclare “function name” (previously declared in…

The fatal error above will appear if your code contains two or more PHP functions that have the exact same name.

PHP Fatal error:  Cannot redeclare function_name() (previously declared in /path/to/domain.com/file.php:12) in   /path/to/domain.com/another_included_file.php on line 73

Check if the function name has already been used.

If you find yourself in a situation where the function name may or may not exist, then you can check to see if the name of the function has already been defined.

function function_name(){
     //do something
 }
if(!function_exists('function_name')){
     function function_name(){
         //do something
     }
 }

In the code snippet above, I used the function_exists function to check if function_name already exists as a function. Because it does exist in the example above, the second function is never created.

Categories
networking Uncategorized

Cannot Map NAS in Windows 10 Pro x64

I have purchased a D-link DNS 321, and after several hours of trying I could not map the NAS as a drive nor access it using NFS

Objective: use NAS as network file server.

Go to Programs and Features in Windows 10 Control Panel. Click Turn Windows features on or off link.
Scroll down to SMB 1.0/CIFS File Sharing Support and check all of the features. Click OK.
Once all of the installation is done, click Restart now.
In Windows Explorer go to Network => Storage (or whatever your NAS’ name is) and you will be presented with all the Volumes available.
Categories
Uncategorized

CURL ERROR: SSL certificate problem: self signed certificate in certificate chain (Authorize.net)

Recently there was a spike in Authorize.net customers that reported their sites credit card processing errors which, according to log files resulted in the following error produced:

CURL ERROR: SSL certificate problem: self signed certificate in certificate chain

Fix:

Testing (oneliner):

echo | openssl s_client -connect apitest.authorize.net:443 -showcerts -verify 10 -CAfile /home/path/to/cert.pem

Make sure to replace “/home/path/to/cert.pem” with the real path. If all is good you’ll get something like this at the end:

Verify return code: 0 (ok)

Otherwise you’ll see:

Verify return code: 19 (self signed certificate in certificate chain)
Categories
hosting webhosting

Hsphere file paths

Mysql:
======
Socket file: /var/lib/mysql/mysql.sock
Database file: /var/lib/mysql
mysql password file: /var/lib/mysql/.my.cnf
Error log: /var/log/mysql/error.log

Ftp:
=====

Proftpd is default with hsphere.

Configuration file: /hsphere/shared/config/ftpd/proftpd.conf
Log file: /hsphere/local/var/proftpd/xferlog

Php:
=====

Categories
linux webhosting

How to restart Linux services

Starting Services

Linux:

/etc/rc.d/init.d/ start

FreeBSD:

/usr/local/etc/rc.d/ start

Stopping Services

Linux: