Object Oriented PHP-GTK2 - Configuring PHP for GTK Object Oriented PHP-GTK2 - Configuring PHP for GTK
it's me, bob. lol. Object Oriented PHP-GTK2 OOPS

Configuring PHP for GTK

Setting up the environment for PHP to work right at all times

Topic: Compiling PHP-GTK2 (View All Tutorials)
Keywords: configure php ini extension
Updated: 224 Days Ago, 2007/11/23 21:58

Before you can use PHP-GTK, PHP needs to load the module. This is done via the php.ini file. Windows users should read this instead.

Why a PHP-GTK Config?

On Unix based systems, it is a good idea to have a separate PHP config file just for GTK. Why?

  • Not all command line programs need GTK.
    Why waste time loading the GTK library if the command line program is not even going to to use it? With PHP you need all the speed you can get.
  • Apache Will Die.
    GTK connects to the window manager when loaded. Apache does not have access to any window manager, therefore GTK will kill Apache. Apache = Web. GTK = Local.
  • Your SSH sessions could break. Over SSH you generally are not using GUI applications, even though you can with X11 forwarding. There are two problems with this. They are not PHP-GTK problems persay, they are just generic problems with remote shells.
    • X11 Forwarding Is Disabled.
      If this is the case you cannot use any GUI program over SSH. If PHP is loading GTK it will die because obviously it cannot find a X Session to connect with even if you are just using the CLI and do not even use GTK. When do you not need GTK? Well CRON for one. CRON scripts will explode in this exact same fashion.
[bob@kathryn ~]$ php
Could not startup.
    • The SSH session is not 'trusted'. If the connection is not from what SSHD considers a 'trusted host' then GUI applications will be lagged, and GDK will throw errors out. This is true of any GTK program no matter what language it was written in. Again if you have PHP-GTK loaded for all CLI apps even if you do not need it, you will get the aforementioned GDK errors.
[bob@kathryn ~]$ php
(-:61599): Gdk-WARNING **: Connection to display localhost:10.0 appears to
be untrusted. Pointer and keyboard grabs and inter-client communication
may not work as expected.
    • X11 Forwarding on a trusted session. This is the only time it will work right. Problem is you have to manually configure this in SSHD to make it happen and if you are not admin of the PC then you are stuck.

Moral of the story? Do not use Apache's PHP.INI, do not use the PHP-CLI.INI. Use a special config file just for GTK. It is not hard to setup.

 

Create The Config, and Go

First thing, run `which php`. If it does not say /usr/bin/php as on some systems it might say /usr/local/bin/php instead, then you should create a symbolic link.

[root@kathryn /]# ln -s /usr/local/bin/php /usr/bin/php

Now open your favourite text editor and paste the following into a new file.

; This is a default PHP.INI (/etc/gtk/php.ini) for GTK Applications.
; Bob Majdak Jr 

; all the other options not in this php sets as defaults. these are only the
; highlights which are considered important for client side applications.

engine = Off
error_reporting = E_ALL
default_charset = "utf-8"
user_agent = "PHP-GTK"

; edit this to the path of your extension directory.
extension_dir = "/usr/lib/php/extensions"

safe_mode = Off
register_long_arrays = Off
register_globals = Off
magic_quotes_gpc = Off
magic_quotes_runtime = Off
magic_quotes_sybase = Off

allow_url_fopen = On
register_argc_argv = On
auto_globals_jit = On

; add more extension lines after this one if you have more .so files that should
; be loaded.
extension = "php_gtk2.so"

memory_limit=128M

[php-gtk]
php-gtk.codepage = "UTF-8"
php-gtk.extensions = ""

The highlighted line, extension_dir, this is different from system to system. You will need to put the path to your extension dir in there instead. To find out, you can use this command.

[bob@kathryn ~]$ php -i | grep extension_dir
extension_dir => /usr/local/lib/php/extensions/no-debug-non-zts-20060613

Also, see the line that is extension = "php_gtk2.so"? Look in your extension_dir and see what .so files you have in there. If you have gettext.so add an extension line for it after the PHP-GTK one. Gettext is the important one because that is what allows you to create translations for your applications. Add your other modules if you want, such as MySQL and the like.

Note

Some distributions like Ubuntu have the PHP scan directory feature compiled. If you add lines to this config and it reports errors about already having loaded the module when you use it, remove them from this config only.

Now, save this file as /etc/gtk/php.ini. You might have to create the /etc/gtk directory. You can test if you were successful with this command.

[bob@kathryn ~]$ php -c /etc/gtk/php.ini -i | grep GTK
GTK+ support => enabled
GTK+ v => 2.10.6

This means everything is good to go. If it did not tell you GTK+ support is enabled then you did not install successfully.

 

Using the Correct Config

Now PHP programs on the CLI will use the default CLI config, and only programs which are told they need GTK will use the GTK config. This is very good.

[bob@kathryn ~]$ php -i | grep Loaded
Loaded Configuration File => /usr/local/etc/php-cli.ini

[bob@kathryn ~]$ php -c /etc/gtk/php.ini -i | grep Loaded
Loaded Configuration File => /etc/gtk/php.ini

"But Bob," you are probably saying, "that looks like a pain in the arse to type every time." You are right! Instead of typing this every time we want to run a program, we tell the program to do it. You will notice at the top of all my code codes, one line which actually comes before the opening <?php marker. This line is called a hashbang (or shebang).

#!/usr/bin/php -c/etc/gtk/php.ini

See the first line of code in this tutorial for example.

Notice /usr/bin/php, the symlink I told you to create earlier, and the lack of a space between -c and /etc/gtk/php.ini. This line should be placed at the top of all programs you want to use GTK. With this line if the file has the proper permissions (aka 755) then instead of doing something like this:

[bob@kathryn ~]$ php -c /etc/gtk/php.ini test.phpg

We can do this:

[bob@kathryn ~]$ ./test.phpg

And if test.phpg was located in a PATH directory such as /usr/bin then we could do:

[bob@kathryn ~]$ test.phpg

You could even rename the file to not have the .phpg file extension if you wanted.

What is this dot phpg extension anyway?
.phpg is the file extension you should give files that need PHP-GTK. Read more here.

Was this document helpful? I appreciate your feedback.
What are the rules about reusing this code?


i can has web two point ooh // copyright © 2007-2008 bob majdak jr
[ xhtml css | firefox ie7 opera ]