Kiran's blog

Session Concept PHP

HTTP being a stateless protocol - every request that comes from a browser to the server cant be identified by the server as a subsequent request of that user/IP/browser or a complete new request.

HTTP doesn't really understand who had made request. In such cases sessions manage to make HTTP look intelligent? The Answer lies in the request-response model with data.

Generall when a normal request is made to server the minimalistic data passed by the client/browser is this

GET / HTTP/1.1
Host: kiran.org.in

The server responds by giving the output. But however, when we do session_start();, What actually happens is, the PHP engine sets a PHPSESSID cookie. This data is sent from the Server as Set-Cookie header. So the response goes somewhat like this

HTTP/1.x 200 OK
Date: xxxx
Set-Cookie: PHPSESSID=<32charhexvalue>; expires=xxxx
...

Refresh Index Statistics MySQL

If frequent changes happens on a table, it may develop some inefficiencies in its indexes. Fragmentation due to blocks moving around on disk and inaccurate index statistics are the two most common problems you're likely to see. However, one cay easily optimize index data for MyISAM tables.

For reindexing a table we can use OPTIMIZE TABLE command. In doing so, MySQL try reading the records again in the table and reconstruct all of its indexes. This will result in indexes with good statistics.

However, reindexing the table takes lot of time if the table size is huge. During that time, MySQL also has a write lock on the table, so data can't be updated.

For offline analysis you can try myisamchk command-line tool:

$ cd your-database-name

$ myisamchk table-name

You need to make sure that MySQL isn't running when you try this, or you run the risk of corrupting your indexes.

The Heredoc Syntax PHP

Heredoc, in case you’ve never heard the term before, is an alternative way for encapsulating strings. It’s used and seen much less often than the standard single or double quotes, but it fulfills the same role. Heredoc is like putting peanut butter on bananas: you either grow up doing it or you don’t. The heredoc
method works just like a double quote in that the values of variables will be printed, but you can define your own delimiter, which is particularly nice when printing oodles of HTML (with its own double-quotation marks).

The only catch to heredoc is that its syntax is very particular!
The heredoc syntax starts with <<<, immediately followed by an identifier. The identifier is normally a word in all caps. It can only contain alphanumeric characters plus the underscore (no spaces), and it cannot begin with a number. There should be nothing on the same line after the initial identifier, not even a space! So usage of heredoc might begin like

echo <<

Character Type Functions PHP

Here I am listing a couple of Character Type functions, added to PHP in version 4.3.These functions test a given value against certain constraints for the current locale (established by the setlocale() function).

The Character Type functions provide validation specific to the given environment(i.e., the locale setting).

Function

Checks If Value Contains

ctype_alnum() Letters and numbers
ctype_alpha() Letters only
ctype_cntrl() Control characters
ctype_digit() Numbers
ctype_graph() Printable characters, except spaces
ctype_lower() Lowercase letters
ctype_print() Printable characters
ctype_punct() Punctuation
ctype_space() White space characters

Running Server Commands in PHP

Here I will discussed how to run commands on the server. There are many PHP functions available for executing server commands.

For starters, there is exec():

exec(command, $output);

This function takes a command and assigns to $output an array where each
element is a line of the generated output.

There is also system(), which just returns the output (so that it could be immediately sent to the Web browser):

system(command);

The passthru() function is similar, but it can also return binary output:

passthru(command);

Finally, you could use shell_exec() or the backticks, both of which just return the output:

$var = shell_exec(command);
$var = 'command';

For security purposes, you should use escapeshellarg() or escapeshellcmd()
to sanctify any command that isn't hard-coded.

Cumulus Tag Cloud

Kiran Says

I love work environment which is:

Informal yet professional
Demanding yet rewarding
Challenging yet inspiring
Mediocrity is not an option
Having fun is serious business
Making mistakes is human
Forgiveness is Company Policy

Travelling Sucks