Browsing the PHP category

GoDaddy Cron Job PHP include files

As a follow-up to figuring out how to get Cron Jobs to actually run in GoDaddy with a PHP script, I ran into another error:

I have a PHP Cron job that worked on HostGator, but it didn’t work on GoDaddy.  After I figured out the correct command to use, I noticed that it wasn’t actually connecting to the database.

At first, I didn’t notice this because I typically “include” files instead of “require” them.  I read somewhere that it takes up less processing power; so I figure once I know the location of the file, I can just “include” it (instead of “require” it).  Unfortunately, changing hosts caused an unforeseen inssue!  So that was the first mistake…

Going to the URL (in the browser), everything worked (i.e. it would connect to the database).  However that when running the Cron job, it didn’t work.  I found this out by printing out (in the email) the current working directory ( cwd(); ).  On the web server, it was as expected:

 /home/<username>/public_html/<sitename>/file.php

However the Cron job runs it from the root!

/home/<username>

Usually I just reference a configuration file with “include(“../path/relative/to/here”).  But that didn’t work!  The solution was to explicitly state the entire path of the file you want to include.

However then I ran into another problem:

I typically have code that detects the URL to determine if I’m in a sandbox or production environment.  GoDaddy’s Cron job, the PHP function $_SERVER[‘SCRIPT_FILENAME’] doesn’t return anything, so you can’t use that to determine which environment you’re in.  For now, I think I’ll have to just have 2 version of the file and comment/uncomment out the appropriate database connection to serve the correct data source.

By the way: if you thought this blog post was helpful, check out the book I wrote on PHP, geared toward intermediate web developers.

Read the article

GoDaddy PHP Cron Job (shared server)

I recently wrote about writing a (PHP) Cron Job on Host Gator.  Now I had to do one for GoDaddy.  I’m on a shared server (not sure if that makes a difference?).

I’m using CPanel with PHP version 7.1… and at the time of writing, GoDaddy’s documentation only has CPanel for a lower PHP version.  I called tech support, but they only pointed me in the right direction…

Here’s the right syntax:

/usr/bin/php-cli /home/<username>/public_html/<site>/file.php

I reluctantly turned on sending emails; good thing I did, because I found out there was an error.

BIG GOTCHA: you can’t have a period in the directory name; as in, if you’re working in “/username/public_html/yoursite.com/cronjob.php” that won’t work – it doesn’t like the “yoursite.com” part.  Remove that so it’s just “/username/public_html/yoursitecom/cronjob.php” (or some variation that doesn’t have the period).  The script wasn’t even running, and I didn’t realize this until I turned on the email notifications.

Once I did turn on the notifications, I was getting this error:

Failed loading /opt/php54/lib/php/extensions/no-debug-non-zts-20100525/ioncube_loader_lin_5.4.so:  /opt/php54/lib/php/extensions/no-debug-non-zts-20100525/ioncube_loader_lin_5.4.so: cannot open shared object file: No such file or directory

Failed loading /opt/php54/lib/php/extensions/no-debug-non-zts-20100525/ZendGuardLoader.so:  /opt/php54/lib/php/extensions/no-debug-non-zts-20100525/ZendGuardLoader.so: cannot open shared object file: No such file or directory
Content-type: text/html; charset=UTF-8

Thanks to Stack Overflow – https://stackoverflow.com/questions/40109716/cronjob-not-starting-in-godaddy-shared-hosting) for pointing out that you need the “-cli” in the command to avoid that error.

UPDATE: there’s another “gotcha” in GoDaddy’s PHP Cron Job usage; I wrote about it here.

By the way: if you thought this blog post was helpful, check out the book I wrote on PHP, geared toward intermediate web developers.

Read the article

How to set up a PHP Cron Job in Host Gator

“Cron Jobs” are a way to run a script automatically, on a certain schedule.  For example, maybe you want to email all new users the day AFTER they sign up with some information.  Or maybe you want to charge customers on a particular schedule.

You could make your PHP script and just visit it manually every day, but there’s a better way: setting up a cron job.  Here’s Host Gator’s article, but it’s a little out-dated and doesn’t really have any examples.  This article gives examples, but they’re not clear enough (hence this post)

In the Host Gator cPanel, go to Advanced >> Cron Jobs.  Select how often you want the job to run from “Common Settings” and make your custom changes accordingly.

Here’s the part where the Host Gator documentation is lacking

In that second link, they give a lot of examples.  Here’s the command that worked for me:

php -q /home3/myUsername/public_html/mysite.com/home/cronjob.php

Notes/Tips:

  • Start off with php -q … don’t go with their other options
  • While they do bold the “username” (indicating it has to be changed) they do NOT bold  the number in the “home” part of the directory
  • Use the PHP function print getcwd() to find out the exact path (ex. the “/home4/…”)
  • Do NOT have any security checks for user authentication that redirect the user to a new page if they’re not logged in.  This isn’t a user running the script, it’s the computer.  As such, don’t even include the session_start() at the top of your script!
  • Start by testing your script “every minute” to make sure it works.  Then adjust the schedule to the appropriate time.

By the way: if you thought this blog post was helpful, check out the book I wrote on PHP, geared toward intermediate web developers.

Read the article

  • 04.21.2017
  • PHP
  • 04.20.2017
  • PHP

heroku php links go to wrong protocol

I had a PHP issue that turned out to be specific for Heroku:
If you go to www.site.com/test … it’ll really be www.site.com/test/index.php – as, you don’t need to specify “index.php”, the server will know to go there.
But if you’re viewing https version and you go to https://site.com/test then you’re redirected to the “index.php” page, but it’s on the http (not https) version.  So if I’m on https and I have a link or redirect, I need to explicitly state “/test/index.php otherwise it’ll redirect me, by default, to the non-ssl version.
I tried a variety of Google searched and even asked on Stack Overflow:
  • php heroku redirects to non-ssl
  • heroku php links go to wrong protocol
  • heroku redirects when going to root directory
  • heroku php redirects to http when not explicitly specifying index.php
  • heroku php redirects when not specifying exact file name

The real solution was when I contacted Heroku support.  They pointed me to a Stack Overflow post… that didn’t work at first, but they told me how to modify it to make it work.

The solution is basically creating an .htaccess file that forces you to be on SSL.  But there’s a particular line in the .htaccess file that is needed for Heroku:

RewriteEngine On

# Redirect to SSL version
# Note: %{HTTP:X-Forwarded-Proto} is specific for Heroku

RewriteCond %{HTTPS} off
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

Read the article

  • 04.05.2017
  • PHP

Extra blank lines when sending HTML-friendly emails in PHP with Amazon SES

I’m using Amazons SES to send emails, but I wanted to send an HTML-friendly one (with a design, some colors, etc.).  I knew that all styles had to be inline, but I running into an error: there were extra lines (carriage returns) when the email was sent out.

I had two variables that defined the HTML header and footer .  This is part of the template:

$TEMPLATE_TOP = ‘<body>
<table width=”600″ align=”center” cellspacing=”0″ cellpadding=”0″ border=”0″>
<tr>
<td>  ‘;

and

$TEMPLATE_BOTTOM = ‘
</td>
</tr>
</table>’;

 

Of course, for readability, I indent and put the tags on new lines – and this is what was causing the incorrect formatting (extra blank lines).

The solution is to replace chr(10) and chr(13) with a blank.  So after you make the template pieces, add this in after :

$TEMPLATE_TOP = str_replace(chr(10), “”, $TEMPLATE_TOP);
$TEMPLATE_TOP = str_replace(chr(13), “”, $TEMPLATE_TOP);

$TEMPLATE_BOTTOM = str_replace(chr(10), “”, $TEMPLATE_BOTTOM);
$TEMPLATE_BOTTOM = str_replace(chr(13), “”, $TEMPLATE_BOTTOM);

 

I was sending an email from (multiple) functions.  To make it accessible, I put those (2) in separate constants:

define(“TEMPLATE_TOP”, $TEMPLATE_TOP);
define(“TEMPLATE_BOTTOM”, $TEMPLATE_BOTTOM);

So  the body of the email (in each function) becomes:

constant(‘TEMPLATE_TOP’) . $email_body . constant(‘TEMPLATE_BOTTOM’);

(of course, where $email_body has your content).

 

By the way: if you thought this blog post was helpful, check out the book I wrote on PHP, geared toward intermediate web developers.

Read the article

  • 04.03.2017
  • PHP

PHP Heroku Foreign Keys don’t work

I had a SQL script that was creating 2 tables, and populating them.  It worked fine on my local machine, and when I was using a Shared Host.  But when I copied it to Heroku, it didn’t work.

The root of the problem was because I was causing referential integrity errors.  I had (falsely) assumed that all IDs would auto-increment by 1.  But I found out that ClearDB (the version of MySQL I was using) increments by 10.  This was causing the foreign keys not not reference correctly (as, I had hard-coded my values on a base-1 system).
I wrote a post how to resolve this.
But it took me awhile to figure out that was the problem!

By the way: if you thought this blog post was helpful, check out the book I wrote on PHP, geared toward intermediate web developers.

Read the article

  • 03.21.2017
  • PHP

php heroku database autoincrements incorrectly

I have a PHP web application with a MySQL database. I’m using Heroku’s addon of ClearDB (a type of MySQL database). In the scripts I’m using to pre-populate my database, the primary keys (auto-incrementing IDs) are incrementing by 10, instead of 1.

I searched for php heroku database autoincrements incorrectly and Stack overflow was to the rescue!

On solution led me here, which says ClearDB increments by 10 instead of 1.

Using Adminer, I was able to run the following SQL command to reveal it was indeed incrementing incorrectly:

SHOW VARIABLES LIKE ‘auto_inc%’;

adminer_cleardb_auto_increment_by_10

When I ran this SQL statement:

SET @@auto_increment_increment=1;

I could set it to “1”, but that was only for the ONE query that was being run.

Re-Running the “show variables…” SQL, I can see that it was corrected.

I wanted this on EVERY query I ran (despite ClearDB creators having a reason for doing otherwise).

The Stack Overflow post said you can fix it with this:
SET GLOBAL auto_increment_increment=1;

But I didn’t have permissions to do so – it said:

Error in query (1227): Access denied; you need (at least one of) the SUPER privilege(s) for this operation

Start of the solution: I created a variable:
$auto_increment = “SET @@auto_increment_increment=1”;

and then in my query, I did $query = $auto_increment . ” [regular query here]”;

That code will work with Adminer, but didn’t work with my regular web app. This was because I’m using msqli_query to run it – and I had to run TWO queries (one to set the autoincrement, one for the desired query). By default, that’s turned off for security precautions. For this script, I had to use mysqli_multi_query() instead.

BUT then I ran into the problem of, I couldn’t update multiple tables – I got another error: Commands out of sync; you can’t run this command now .

 

The solution: I ended up just copying/pasting the SQL scripts into Adminer – 2 commands per each script (one for the auto-increment, one for the real query)

By the way: if you thought this blog post was helpful, check out the book I wrote on PHP, geared toward intermediate web developers.

Read the article

  • 03.21.2017
  • PHP

PHP Heroku Sessions

I wanted to use Heroku to host my PHP site.  There are a few things that Heroku does not support out-of-the-box, and one of them is PHP session management.

They have a nice article here to get you started, however I couldn’t get it to work.  Specifically, when I an composer, I was getting an error about memcached not being installed.  I tried both a Windows machine and a Mac, and couldn’t figure it out. I even went as far as trying to not use PHP Sessions, and instead use Cookies, but I thought that the Sessions would be more reliable.

I finally came across this Heroku article that said there’s a work-around for running (updating) Composer if you don’t have the necessary software installed on your computer:

 

The solution to all my problems:

 composer update --ignore-platform-reqs

… be sure to also create the “.user.ini” file!

By the way: if you thought this blog post was helpful, check out the book I wrote on PHP, geared toward intermediate web developers.

Read the article

  • 03.21.2017
  • PHP

PHP extension ext-memcached missing

I’m trying to put my PHP app onto Heroku, and I need sessions.  They recommend memcached, but it’s not working.  When I run composer, I’m getting the error “The requested PHP extension ext-memcached * is missing from your system.  Install or enable PHP’s memcached extension.”

I’ve tried to install it – on multiple operating systems, but can’t figure it out.  But I found a solution!

With composer, just run this instead:

 composer update --ignore-platform-reqs

Read the article

  • 03.20.2017
  • PHP