Pages

Showing posts with label PHP. Show all posts
Showing posts with label PHP. Show all posts

Fixed: MailChimp Success Message Missing With Conflict jQuery Issue

There is a problem with Mailchimp, when you use embed HTML code to show mailchimp subscription form on your website. The error message does show, but the successful or require email confirmation (double opt-in) message is missing.

This happens due to that jQuery library is loaded more than once in the page. For example, if you are running a WordPress blog (version 3.9.1 or later), the jQuery is loaded by the theme in most cases. The jQuery library may load more than twice, if you also install third-party plugin that is using jQuery or jQuery UI.

You can see the error in the browser debugger. "$ is not a function" at following line.
function mce_success_cb(resp){
    $('#mce-success-response').hide();

It means $ isn't an object or function after the jQuery was loaded.

The easy solution is to add the following code after "function mce_success_cb(resp){"


jQuery(document).ready( function($) {

Remember to close the function call at the end of "function mce_success_cb(resp){" bracket.

});

Shared SSL SuPHP 500 Error Fixed

It took me hours and finally figure out what went wrong after install cheap GoDaddy SSL on shared IP on VPS at Hostgator.

Issues: I bought the SSL certification and install on my VPS, which already have 2 dedicated IP (2 other SSL already installed). So my only option is to install the new SSL on the shared IP. While install the SSL, WHM tells me to install the SSL as nobody due to the IP is shared IP.

After restarting the Apache after installing the SSL, the HTTPS works fine on any file that is NOT PHP. When I load the WordPress, it shows me the 500 server error.

It took me hours to figure the Shared SSL with nobody user group is conflicting with suPHP due to the permission.

Solutions:
The idea of fixing this is to assign the SSL certification from nobody to the right cpanel user that owns the add-on/parked domain name.  So here are the steps:

Codeigniter Shows Blank Page PHP + MySQL + Apache + Windows 7

Recently, I started a new development on one of my laptop running Windows7. I install PHP 5.3.6, Apache 2.2, MySQL 5. I move all the source code that was previously developed based on CodeIgnitor 1.7.

Problem

It shows blank page when I load the site on my laptop's localhost. 

Solution

Well. The solution is farily easy. If Codeigniter can not detect the mod_mysql module in PHP when the page is loaded, it doesn't return any error message.
  • So you need to make sure you enable the mod_mysql in your PHP.ini file.
  • Also make sure your .htaccess file is redirect correctly, if you are using Search Engine Friendly URL. Make sure mod_rewrite module is enable in your Apache httpd.conf file.

It's just that easy! A simple check will save you lots time. 

CodeIgniter image_lib bug in the image process loop

This bug has me scratch my head for hours and finally found the solutions to fix it.

In CodeIgniter, you can upload multiple images and do resize, crop and watermark stuff. It's cool that you can use one library to do many awesome stuff. However there is problem when using image_lib in the loop.

Problem:

I upload few image and want to loop through each one of them and resize them differently based on their original size. So I put the resize function in the loop. However, the dimension is based on the previous image.

For example. the first image is resized to 100x200. The second image should be resize to 50x100, but is resized to 100x200. The third one should be resized to 400x300, but is resized to 100x75.

In other words, the second image was resized using the dimension from the previous resized image based on the second image's longer side (W or H). The first image was resized to 100x200, so the second image was resized to 100x200 because the original dimension of the second image was 50x100 (Height is larger than width). So it take the first image's height to resize the second images. Crap!


Solutions:
In the image_lib in CodeIgniter, there is a function to clean the setting ($config). However, there is missing parameters in the function.

So I add two lines in the function clear().

$this->width = '';
$this->height = '';
Add the above two lines before closing the function.

Problem solved!

Facebook Social Plugin load page twice on Firefox and Google Chrome

There is weird issues with FireFox and Google Chrome. The page will be loaded twice, if Facebook Social Plugin such as LIKE button installed on your site.

There is a solution post on Terence Chang's blog!

Prevent Facebook Social Plugin to load page twice – CodeIgniter

PHP Header Location vs Header Refresh

This is very interesting tips about using PHP to do Redirect.

PHP Header with Location seem to redirect faster than using Refresh. Looks like Refresh will create the <meta> refresh tag and depends on the Browser to redirect. In FireFox, it will actually show a blank page before it redirects to destination page.

With Location option, it seems to redirect directly without showing the blank page.

header("Refresh:0;url=". $this->config->item('PF_HTTPS_ROOT') . '/member/signin/');


header('Location: ' . $this->config->item('PF_HTTPS_ROOT') . '/member/signin/', TRUE, 301);

Either ways, it will have a round trip between Server and Client's browser.

Handle PHP multiple choice select dropdown and checkbox

This took me hours to figure out that something was wrong in the PHP.

is_array() and count() can't really distinguish array() or string, if there is only one element.

For example.

$test = array('123');
is_array($test) will return true;
count($test) return 1

$test = '123';
is_array($test) will still return true.
count($test) will still return 1.


So there is no point to check a submitted field from PHP form as an array or a string.

This will cause problem when handling multiple choice select dropdown box or checkbox.

In PHP multiple choice field will need to be name with [] after the field.

For example:

state_id[]
In normal case, if the select box only accept one selection, the field name will be just state_id.

This confuse the PHP form validation. It returns just one string. So you will get error message when handling it with foreach loop.

for example
foreach($_POST['state_id'] as $state_id)
You will get an error like this.

Severity: Warning

Message: Invalid argument supplied for foreach()

Solutions:

No matter the select dropdown box allows to choice one or multiple options, always use multiple format. In other words, always use [] after the field name in the html form.

It will work with the validation process and treat the field as array as always.

CodeIgniter Cache Issues

Well! This is actually not an issues of CodeIgniter cache system. It's how I wrongly use the cache function.

Whenever you use db->cache_on() function to cache the current query. It will cache all the executed queries from this point for the rest of queries in the whole PHP page.

In other words, it will cache everything query that do not have db->cache_off(). WOW! What a big mistake.

So if you just want to cache the current query execution, remember to put db->cache_off() after executing the query. Always remember that!

for example:
$this->pf_db->db->cache_on();
$result = $this->pf_db->db->query($query);
$this->pf_db->db->cache_off();
Without the cache_off(), all the queries after this one will be cached!

Run PHP5 on Apache2.2.x as Module or CGI

This is a notes on how to run PHP5 on Apache 2.2 with either Module or CGI.

Open Apache http.conf file and make the following changes.

The following modification is only required for run PHP as CGI.
1. Find the default directory section
<Directory />
Options FollowSymLinks
AllowOverride None
#Order deny,allow
#Deny from all
Satisfy all
</Directory>


Comment out the following two lines
Order deny,allow
Deny from all

2. Add the following lines for Module or CGI
# RUN PHP as Module

PHPIniDir "D:/php5/"
LoadModule php5_module "D:/php5/php5apache2_2.dll"
AddType text/html .php .php5
AddHandler application/x-httpd-php .php . php5

# RUN PHP as CGI
ScriptAlias /php/ "C:/php/"
AddType application/x-httpd-php .php
Action application/x-httpd-php "/php/php-cgi.exe"
<Directory />
Order allow,deny
Allow from all
</Directory>

Annoying Magic Quotes in PHP on Shared hosting

In some case, the PHP4 configuration on Linux shared hosting will be default "ON" for magic_quotes_gpc. This cause the single and double quote will be escape with backslash (\). It doesn't matter if you do addslashes or not.

So the magic_quotes_gpc has to be turned off. Tried the following in the .htaccess file. Put this file under the web root.


<ifModule mod_php4.c>
php_flag magic_quotes_gpc off
php_flag magic_quotes_runtime off
</ifmodule>


If it doesn't work, them put the following php.ini file in every folders that is root of the script.

magic_quotes_runtime=off
magic_quotes_gpc=off
magic_quotes_sybase=off