Tag Archives: php

PHP 5.6 on CentOS 7

This is how I managed to have PHP 5.6 on a fresh CentOS 7.

yum install epel-release
rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm
yum clean
yum update

Then…

yum install php56w php56w-opcache php56w-mcrypt php56w-pdo php56w-mysql php56w-mbstring php56w-gd php56w-dom

 

ImageMagick

yum install ImageMagick php56w-imagick

 

Restart httpd

service httpd restart

php-fpm – upstream: “fastcgi://127.0.0.1:9000”

 

2016/11/22 10:01:01 [error] 18314#0: *1 connect() failed (111: Connection refused) while connecting to upstream, client:XX.XX.X.XXX, server: www.domain.com, request: "GET / HTTP/1.1", upstream: "fastcgi://127.0.0.1:9000", host: "www.domain.com"

Lets edit /etc/php-fpm.d/www.conf and uncomment the line of 127.0.0.1:9000 and comment listen = /var/run/php-fpm/php-fpm.sock.

; Note: This value is mandatory.
listen = 127.0.0.1:9000
#listen = /var/run/php-fpm/php-fpm.sock

And restart PHP-FPM & nginx

/bin/systemctl restart  php-fpm.service
/bin/systemctl restart  nginx.service

Simple PHP contact form with Google reCaptcha

So, *everyone* needs a simple form to send emails, via PHP, but we also need a protection agains bots/spammers.
This is code that i’v grabbed from codeforgeek and improved since the original had a few errors that made it impossible to run/work/send email.

It uses Google reCaptcha and you need to register you site/domain on it and grab the site key and the secret key.

The form already has twitter bootstrap classes… but you can remove them! 🙂

The form.php – at the begin
<?php
	$to      = '[email protected]';
	$subject = 'Support Message';

	// keys from Google reCaptcha https://www.google.com/recaptcha/admin
	$sitekey = 'recaptcha_site_key';
	$secretkey = 'recaptcha_secret_key';

	$alert = '';
	if ($_SERVER['REQUEST_METHOD'] == 'POST')
	{
		$email;$message;$captcha;
		if(isset($_POST['email']))
        			$email=$_POST['email'];
		        if(isset($_POST['message']))
        			$message=$_POST['message'];
			if(isset($_POST['g-recaptcha-response']))
        		$captcha=$_POST['g-recaptcha-response'];

	        	if(!$captcha)
        		$alert = '<div class="alert alert-warning" role="alert">Please wait until the captcha protection give you a check mark.</div>';

        	$response=json_decode(file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret='.$secretkey.'&response=".$captcha."&remoteip=".$_SERVER['REMOTE_ADDR']));

		if($response->success==false)
		{
			/* lets set the error message for the alert... */
			if ($alert=='')
				$alert = '<div class="alert alert-danger" role="alert">Some how you have been detected has a spammer.</div>';
        	}
		else
		{
			/* the email to you */
			$headers = 'From: '.$to.'' . "\r\n" .
                	'Reply-To: '.$email.'' . "\r\n" .
                	'X-Mailer: PHP/' . phpversion();
            		mail($to, $subject, $message, $headers);
			/* the copy of the email to the *client* */
			$headers = 'From: '.$email.'' . "\r\n" .
				'Reply-To: '.$to.'' . "\r\n" .
				'X-Mailer: PHP/' . phpversion();
			mail($email, 'COPY - '. $subject, $message, $headers);
			/* lets set the success message for the alert... */
			$alert = '<div class="alert alert-success" role="alert">Your email has been sent.</div>';
        	}
	}
?>
The html form
<?=$alert?>
<form id="comment_form" action="" method="post">
	<input name="email" type="email" placeholder="Type your email" size="40" class="form-control" value="<?=@$email?>" ><br><br>
	<textarea name="message" rows="8" cols="39" class="form-control"  placeholder="Your message to us..." ><?=@$message?></textarea><br>
	<div class="g-recaptcha" data-sitekey="<?=$sitekey?>"></div><br>
	<input type="submit" name="submit" value="Send message" class="btn btn-default"><br>
</form>

Some where at the bottom/footer…

<script src='https://www.google.com/recaptcha/api.js'></script>

 

Hope it helps anyone! 🙂

PHP – check if a file was changed under 10 mins ago

I need to cache results of a PHP glob of a folder…
This folder gets files uploaded via FTP from time to time and I need to prevent the script to perform a glob every time that the page is accessed.

 

$file = 'content.json' ; 
$time = 600 ; //in seconds 

if(!file_exists($file)) 
{ 
  echo 'does not exist' ;
  // lets call the function that makes the glob and create the content.json 
} 
else 
{ 
 if( time() - filemtime( $file ) <= $time ) 
 { 
 echo 'file last changed under 10 mins ago' ; 
 } 
 else 
 { 
 echo 'file last changed more than 10 mins ago' ;
 // lets call the function that makes the glob and create the content.json 
 } 
}

PHP exec in background

So, in one of my projects/products/services, I need to execute ffmpeg via PHP…
Usually I use at do send the job to a queue list.. unfortunately in two different clients’ servers somehow PHP + exec and at doesn’t work well….

This is how we send a PHP exec to background

exec(command > /dev/null 2>/dev/null &);

 

As seen on http://stackoverflow.com/questions/8961470/how-to-make-shell-exec-run-in-background-while-php-continues

PHP – 500 internal server error – cPanel

I’v been struggling for hours to solve a weird problem with PHP in a cPanel environment.

This is how my log (/usr/local/apache/logs/error_log) looks like

[Tue Dec 30 21:13:09 2014] [error] [client 88.94.5.xxx] SoftException in Application.cpp:601: Directory "/home/dee/subdomains/www2.domain.com/members" is writeable by group
[Tue Dec 30 21:13:09 2014] [error] [client 88.94.5.XXX] Premature end of script headers: index.php
[Tue Dec 30 21:13:09 2014] [error] [client 88.94.5.XXX] File does not exist: /home/dee/subdomains/www2.domain.com/500.shtml

I thought that was some bad configuration on .htaccess … but that was wrong!
On some environments we need to have the right file/directory permissions so it work…

  • files: 644
  • directories: 755

Continue reading PHP – 500 internal server error – cPanel

PHP DropBox Backup Uploader: Backup local files as a ZIP archive to Dropbox

 

This class can backup local files as a ZIP archive sent to Dropbox

It handles the OAuth authorization process to obtain a token to access an API on behalf of the current Web user.

The class create a ZIP archive with files from a directory to backup.

The ZIP archive is sent to the DropBox server using requests sent to is API.

http://www.phpclasses.org/package/8922-PHP-Backup-local-files-as-a-ZIP-archive-to-Dropbox.html