Monday, August 14, 2006

PHP unexplained session behavior

THE PROBLEM:

I'm currently experiencing a problem using sessions under php 4.4.2. I store variables and objects inside session variables, and all works well under php 5.x, but when I upload those pages to my hosting (tiscali italia business) the whole site and session variables seem to work at the beginning, but after a variable number of request (sometimes the second, sometimes the fifteenth page requested) all the site get stuck, every request from the site domain 'hangs up' from this moment.

But the site isn't really stuck, any request to a page (plain html or php+html) that not use sessions (without session_start()) works well and these type of pages are sent to browser.

If I make requests from another browser, but from the same host, or if I switch to another client host, a new session is created, I can navigate through a few pages and after some requests I experience the problem again.

Notice that after about thirty minutes (a sort of timeout?) I can navigate again from this host+browser.

So, after these tests, I'm pretty sure that the problem is caused by some stranges behaviours of sessions, but I don't know if I didn't consider some hosting provider's php.ini session settings, or if it could even be a bug or an unexpectedly weird handling of session variables.

THE WORKAROUND:
I wrote from scratch a little class for session handling.
It stores serialized variables on files in a temporary path, using a cookie to store the sessionid generated at the initialization of my class. That's quite exactly how does php manage session. You can find quick usage infos inside the class file itself.

here's the source, if you might find it useful post a comment (no registration needed).

Monday, April 10, 2006

Setting up a CVS server on Linux

After a long pause from posting, here I am writing down some experiences with cvs server. CVS, Concurrent Versions System, is a powerful software that helps software developers - in team or even individual - to keep track of development process. You can find any further information about its features at the site linked above.

I work almost daily (..let's take this as true) with cvs as a client, but I configured a cvs server only once, and it was a looong time ago. I need now to get a cvs server working for my new "free time" project, a site for my group of friends. Let's go now!

get the latest version of CVS, and then extract it:
-- on the server host--
# wget http://ftp.gnu.org/non-gnu/cvs/source/feature/1.12.13/cvs-1.12.13.tar.gz
# tar xzf cvs-1.12.13.tar.gz

configuring and installing it:
# cd cvs-1.12.13
# ./configure
# make
# make install

happiness! no problem so far.
now we need to create the repository, I suppose..
# mkdir /var/lib/cvsroot
# chmod 1777 /var/lib/cvsroot
# export CVSROOT=/var/lib/cvsroot
# cvs init

we did it! let's test it now from a remote host, importing a first test project:
-- from another machine --
$ export CVS:pserver:user@cvshost.myserver.com:/var/lib/cvsroot
$ cvs login
Logging in to :pserver:user@cvshost.myserver.com:2401/var/lib/cvsroot
CVS password:
[enter your password, user must be a local user for the machine cvshost.myserver.com]
$ cd myproject
$ cvs import -m "first project" test myproject START
N myproject/README
N myproject/AUTHORS
N myproject/TODO
N myproject/index.php

No conflicts created by this import

the last line shows us that we didn't get any problem during the last operation. PHeeew. :) It was easy, wasn't it ?

Let's try to checkout the project directly from the cvs server:
$ cd /tmp/
$ cvs co myproject
cvs checkout: Updating myproject
U myproject/README
U myproject/AUTHORS
U myproject/TODO
U myproject/index.php

It worked! Now I have to write down all the code. And I have no more excuses like "ehhh I need a cvs server online somewhere". I need time. And some coffee, maybe.

Finally, notice that I didn't edit any configuration file, just used the default ones provided with the package. It seems they fit very well my basic needs.

You can find a good howto about CVS server: http://www.linuxfromscratch.org/blfs/view/cvs/server/cvsserver.html

Wednesday, December 28, 2005

Session saving and Firefox 1.5

I use mainly Firefox 1.5 as internet browser, and my friends call me "the thousand tabs man" for joke because I often have many tabs open. So, it's important for me to keep these tabs even when I close my pc or when I experience a firefox crash.

So I tried to look for an extension that provides this feature, and I installed SessionSaver 2. All ok with old Firefox < 1.5, but with the latest version I get some problems: the worst one is the "unresponsive script" warning that I get during operation such as file uploads, even when the file is small. It seems as the SessionSaver script computes some operations that stick it in an infinite loop or something similar, because if I click on "stop script" button the upload completes correctly.

I removed SessionSaver extension, and asked in the official forum (it's not properly a forum, is a post on mozillazine forum with actual 122 pages, not very well organized in my opinion), and found there a link to another extension: TabMixPlus.

The image on the left shows you part of session saving options you can choose. It has also a lot of other useful features, such as multi level undo close tab - I used to install a separate extension for this, called UndoCloseTab - and page loading progress bar on the tab and, if you want, in statusbar too. I still have to explore all its features, but the only one I really needed works perfectly, and I have my tab session always present even in case of system crash.

You can download TabMixPlus here:
http://tmp.garyr.net/beta%200.3/

Official MozillaZine thread about TabMixPlus is here:
http://forums.mozillazine.org/viewtopic.php?t=327222

It's still a beta version, but it seems to work well. Many thanks to the author(s).

Monday, December 12, 2005

Forcing postgreSQL 8.0 default encoding

If anyone knows a better way to set the default encoding please let me know it.
I also tried the -E option to specify SQL_ASCII encoding when running initdb, but it gave me error messages, complaining about compatibility with my locale (ISO8859-1).

So I logged in as root, switched to postgres user, then launched psql console connected to template1 database.

# su - postgres
$ psql template1
Welcome to psql 8.0.3, the PostgreSQL interactive terminal.

Type: \copyright for distribution terms
\h for help with SQL commands
\? for help with psql commands
\g or terminate with semicolon to execute query
\q to quit


then I performed the following command to see which encoding is set for the template database:

template1=# select datname, encoding from pg_database;
datname | encoding
-----------+----------
template1 | 6
template0 | 6
(2 rows)


I discovered that 6 stands for UNICODE. Don't tell me why, cause I don't know, the encoding attribute is shown here as an integer but everywhere its type is a varchar.

So, let's set it as SQL_ASCII (integer 0):

template1=# update pg_database set encoding = 0;
UPDATE 2
template1=# select datname, encoding from pg_database;
datname | encoding
-----------+----------
template1 | 0
template0 | 0
(2 rows)


Then I created a new database:

template1=# create database pippo;
CREATE DATABASE
template1=# select datname, encoding from pg_database;
datname | encoding
-----------+----------
template1 | 0
template0 | 0
pippo | 0
(3 rows)


Hoorray! I got it. Party time now.

Wednesday, December 07, 2005

Writing a program plugin in C++

Today I found myself trying to imagine how to call a function (or more in general to load a library) at runtime. I know shared object, also called DLL on windows world, can be useful for this, but I never wrote a program that uses dynamic linking.

To do so, I read something about dlfcn.h, a standard library for loading shared object at runtime. Let's see how to use it.

At first, create the shared object:

----hello.cpp----

#include <iostream>

using namespace std;

extern "C" void hello()
{
    cout << "hello world!" << endl;
}


compile it as a shared object:

$ g++ hello.cpp -o hello.so -shared


now create the main program file:

----main.cpp----

#include <iostream>
#include <dlfcn.h>

using namespace std;

int main( int argc, char *argv[] )
{
    void * handle = dlopen( "./hello.so", RTLD_LAZY );

    if( !handle )
    {
        cerr << "Cannot open library: " << dlerror() << endl;
        return 1;
    }

    typedef void (*hello_t)();

    hello_t hello = (hello_t) dlsym(handle, "hello");

    if( !hello )
    {
        cerr << "Cannot load symbol 'hello': " << dlerror() << endl;
        dlclose(handle);
        return 1;
    }

    hello();

    dlclose( handle );

    return 0;
}


compile it as usual:

$ g++ main.cpp -o main -ldl


now we have a shared object, hello.so with hello() function compiled inside it. We also have the main program.
The (almost) obvious output will be:

$ ./main
hello world!


source:
http://www.tldp.org/HOWTO/C++-dlopen/index.html

more informations:
http://www.faqs.org/docs/Linux-HOWTO/Program-Library-HOWTO.html

Friday, November 04, 2005

Vim - highlight and capitalize SQL words

As many programmers I use VIM as editor because I think it's the best. You can customize it to the extreme, and get it to do simply whatever you need or want. A way to extend its capabilities is using scripts: in the main site (www.vim.org) you can find a large collection of scripts with relative descriptions.

These days I'm designing a simple database schema, and I'm using vim to write it. So I wanted to search for a script that beautify and simplify me the result script by capitalizing sql (in my case, PostgreSQL) keywords.

First I found the syntax highlight schema for PostgreSQL, because this great open source dmbs extends the standard SQL syntax with some own keywords and constructs, and the standard vim sql hightlight don't parse them. You can download it at this location:

http://www.vim.org/scripts/script.php?script_id=952

After downloading it, you should copy the script (psql.vim) under your syntax files location, /usr/share/vim/vim63/syntax for my linux box, then add these lines to your filetype.vim file, I found it in the path /usr/share/vim/vim63/filetype.vim (or create a personal filetype.vim file under $HOME/.vim/ directory, the same is possible even for the syntax file):


" PostgreSQL
au BufNewFile,BufRead *.psql          setf psql


The highlight section is done :) Let's go for keywords capitalization.

The script I used is stored here: http://www.vim.org/scripts/script.php?script_id=305

after downloading it, I copied under my $HOME/.vim/plugin/ directory. All ok, but it capitalizes some (frequent) words even in non-sql files! If I'm using vim to write a plain text file I don't want it to make words like "in" to "IN" automatically. How to fix this ?

I moved the script from the .vim/plugin/ directory to .vim/other/ (just created), and added this line to filetype.vim (system wide or personal as described above):


if exists("did_load_filetypes")
    finish
endif
augroup filetypedetect
    au! BufRead,BufNewFile *.psql *.sql          source /home/stc/.vim/other/sql_iabbr.vim
augroup END


Now only in case the file has a .psql or .sql extension the script is loaded! Exactly what I wanted.. great. Many thanks to:

Devdas Bhagat for psql.vim
Hari Krishna Dara for sql_iabbr.vim

Sunday, October 23, 2005

Trial #2

Tryin' to post by email.. ! Is it really so powerful ?

Vim and phpm

I've been using linux for a few years, and since the first time I installed it on my computer I learned using VIM. Its name stands for VI iMproved, and it is a very powerful editor, especially for programmers. Often I find myself seeking on www.php.net for php functions prototypes, I can't always remember them all: so, surfing some blogs I found this useful "vim extension", phpm. What's that ?

phpm is a little command line tool that uses a XML file that stores php function prototypes, returning it when you run the command this way:

$ phpm <part_of_function_name>


- how to install it

· download the package http://eide.org/files/phpm/phpm_0-3.tar.gz, untar it;
· edit phpm/phpm file (bash script), and replace the second line with a path where you have write permission (i.e. $HOME/phpm/phpm);
· download the xml pack (from the same site), untar it where you like;
· edit phpm/settings.xml, at line 27 change the "install path" to the path where you untar'd the xml pack;
· add the next line at the end of your $HOME/.vimrc (vim initialization file):

inoremap <C-H> <ESC>:!phpm <C-R>=expand("<cword>")<CR><CR>


all done :)

from now on, where you're writing a function name in a vim session, simply press CTRL+H when the cursor is on the function name string, and the function prototype will appear in a vim shell window.


final considerations:

phpm is a useful tool, the integration with vim is easy. The solution here provided ("stolen" around the web) is good, but I think it'd be better if the function prototype appeared on the same vim editor windows, and not in a "Hit ENTER or type command to continue" shell window. For example, it could be written on a sort of status bar in the main vim window: I think that's possibile and easy to realize, I think a bit of hack on the vim resource file will do the trick. I'll let you know.

Update:

I'm starting to understand vimrc syntax, and the next line makes the php function prototype on the window statusline. I think now phpm vim integration is nicer than the example reported above in this post:

inoremap <C-H><ESC>:let phpm=system("phpm ".expand("<cword>"))<CR><ESC>:set statusline=%{phpm}<CR><ESC>i

Thursday, October 20, 2005

First try

trial #1..
some dummy text here.

Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.

Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan et iusto odio dignissim qui blandit praesent luptatum zzril delenit augue duis dolore te feugait nulla facilisi. Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat.

ehm. this is not so latin as I thought. I think they should revisit their "lorem ipsum dolor generator"