Pages

Showing posts with label PHP5. Show all posts
Showing posts with label PHP5. Show all posts

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 Issues with .htaccess to display No input file specified. error

When running CodeIgniter on Apache 2.2 with PHP5, it display the following error when access url.

No input file specified.

System state:
Apache 2.2
PHP5.2 running as CGI mode with cgi.fix_pathinfo=1 according to PHPINFO().

Problems:
This problem is primary due to running PHP in CGI mode on Apache.

There are two solutions. No matter which one you choose, you will always have the same step 1.

Step 1: Obviously, PHP running under CGI mode has cgi.fix_pathinfo default to 1. Simpley open the /system/application/config/config.php file and make sure the following two options are set.

$config['index_page'] = "";

$config['uri_protocol'] = "AUTO";

Solutions 1:
Step 2: Create a customized PHP.INI file under the root directory of your site with the following line.

cgi.fix_pathinfo = 0;


Solutions 2:
Step 2:
Go to .htaccess file and add question mark after index.php and before slash.
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/system.*
RewriteRule ^(.*)$ index.php?/$1 [L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ index.php?/$1 [L]

Personally, I choose the Solution 1 with PHP.INI. It seems to run faster.

Problem solved!

PHP5 + Apache2.2 + MySQL5 Installation issue solved

I have tried many ways to get this working.

When install PHP5 + Apache2.2 + MySQL5, it shows that MySQL extension can not be loaded.

I followed every instruction to enable the extension in PHP.ini and point the Extension_dir to the right directory (ex. C:\PHP\ext). I also add the right model in Apache2.2 conf file to load the PHP5.

Problem:
PHP5 is unable to find the libmysql.dll and related MySQL extension. According to new PHP5 installation manual, you simply add your PHP5 installation path to the windows environment variables. Even you add your PHP5 installation path to the windows PATH environment variable, it still doesn't work.

Solution 1:
Copy libmysql.dll to your Windows System32 folder and restart your Apache2.2.

Warnning: This is not an ideal ways to solve the issues. You will run into situation that you need copy more dll to System32 folder. It is a night maire to upgrade the future versioin of PHP.

Solution 2:
After you set the PATH environment and restart your computer, it should just work without copying files to System32 folder. This is the reason, why it doesn't work.

Apache does not update the PATH environment until you reboot the server.
After you set the PATH environment variable, turn PHPINFO() and see if the Apache Environment variable is updated with the new path variable. If not, you will need to reboot the machine.

Problem solved! It's as simple as rebooting the machine.

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>

Apache2.2 + PHP5

The purpose of this post is to run Apache2.2.x + PHP5 on Windows XP.

The goal is be able to run mod_rewrite module and load PHP5 by deafult. Due to the file system. use .htacess is not good idea. So I will be using htaccess.txt for Windows system.


Add the following to the http.conf under Apache 2.2 installation:
LoadModule php5_module "D:/php5/php5apache2_2.dll"

# configure the path to php.ini
#PHPIniDir "D:/php5" <-- This line seem to be unnecessary

AccessFileName htaccess.txt
<files>
order allow,deny
deny from all
</files>


Alias /wwwroot "D:/wwwroot/"
<directory>
Options Indexes MultiViews
AllowOverride FileInfo <--- Can simply set to ALL to allow custom htaccess.txt
Order allow,deny
Allow from all

AddType text/html .php .php5
AddHandler application/x-httpd-php .php . php5
</directory>
===============
Add the following line to htaccess.txt to automatically add trailing slash /

<ifmodule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</ifmodule>